4.2 USER-DEFINED FUNCTIONS IN PYTHON
What is a Function?
A function is a block of organized and
reusable code designed to perform a specific task.
Functions help reduce repetition and make programs easier to manage.
Advantages of Functions
v Reuse
code multiple times.
v Reduce
code duplication.
v Improve
program readability.
v Make
debugging easier.
v Support
modular programming.
Modularity
Modularity means dividing a large program into smaller
and manageable parts called functions or modules.
Types of Functions in Python
There are two types
of functions:
v Built-in
Functions
v User-defined
Functions
1. Built-in Functions (Library Functions)
Functions
already provided by Python are called built-in functions.
These functions are automatically available.
Common Built-in Functions
|
Function |
Purpose |
Examples |
|
print( ) |
Displays output |
print("Hello") |
|
input( ) |
Takes input |
|
|
len( ) |
Returns length |
len("Python") |
|
int( ) |
Converts into integer |
int("5") |
|
sum( ) |
Calculates total |
sum([1,2,3]) |
2. User-defined Functions
A function created by the programmer using
the def
keyword is called a user-defined function.
Example
def greet( ):
print("Hello")
Difference Between Built-in and User-defined
Functions
|
Feature |
Built-in
Function |
User-defined
Function |
|
Creator |
Python
Developers |
Programmer |
|
Availability |
Available
by default |
Must
be created |
|
Loading |
Loaded
automatically |
Loaded
when defined |
|
Example |
print(
) |
add(
) |
Creating a User-defined Function
Syntax
def function_name(parameters):
statements
return
value
Example Program
def add_numbers(x, y):
total = x + y
return
total
print(add_numbers(5, 6))
Output
11
Important Rules of Functions
v Function
definition starts with def
v Function
header ends with colon :
v Indentation
is compulsory
v Function
name should be meaningful
v Parameters
are optional
v Return
statement is optional
Return Statement
A statement used to send a value back from a
function.
Syntax
return value
Function Returning Value
Example
def area(length, width):
return
length *
width
result
=
area(5,
8)
print(result)
Output
40
Parameters and Arguments
Parameter
A variable written inside the function
definition is called a parameter.
Example
def area(radius):
radius
is a parameter.
Argument
The actual value passed during the function
call is called an argument.
Example
area(5)
5
is an argument.
Difference Between Parameter and Argument
|
Parameter |
Argument |
|
Placeholder |
Actual
value |
|
Used
in definition |
Used
in function call |
|
Variable |
Value |
Example
def
add(x, y):
return
x +
y
add(5, 3)
|
Item |
Type |
|
x, y |
Parameters |
|
5, 3 |
Arguments |
Scope of Variables
Scope determines where a variable can be
accessed.
Types of Scope
v Local
Scope
v Global
Scope
Local Scope
Variables
declared inside a function and accessible only within that function.
Example
def test():
num = 5
print(num)
test(
)
Global Scope
Variables
declared outside functions and accessible throughout the program.
Example
num = 10
def show( ):
print(num)
show(
)
Passing Arguments
Python supports three types of arguments.
v Positional
Arguments
v Default
Arguments
v Keyword
Arguments
1. Positional Arguments
Arguments are passed in the same order as
parameters.
Example
def check(a, b, c):
print(a,
b, c)
check(2, 5, 7)
Output
2 5 7
2. Default Arguments
Parameters are assigned default values.
Example
def power(base, exp=2):
return
base **
exp
print(power(4))
print(power(2,3))
Output
16
8
Important Rule for Default Arguments
✅
Required parameters first
✅ Default parameters last
Legal Function
def interest(prin, time, rate=0.10):
Illegal Function
def interest(prin, time=2,
rate):
❌ Error occurs because default arguments must
come after required arguments.
3. Keyword Arguments
Arguments are passed using parameter names.
Order does not matter.
Example
def interest(prin, time, rate):
pass
interest(rate=0.10,
prin=2000, time=2)
Types of Functions Based on Return Value
v Void
Function
v Non-Void
Function
Void Function
A function that does not return any value.
Example
def greet( ):
print("Hello")
Python
automatically returns None.
Example
def greet():
print("Hello")
a
=
greet( )
print(a)
Output
Hello
None
Non-Void Function
A function that returns a value.
Example
def add(x, y):
return
x +
y
result
=
add(5,3)
Returning Multiple Values
Python can return multiple values using one
return statement.
Example
def squared(x, y, z):
return
x*x,
y*y,
z*z
print(squared(2,5,7))
Output
(4, 25, 49)
Example with Variables
def squared(x, y, z):
return
x*x,
y*y,
z*z
a,
b, c =
squared(2,3,4)
print(a, b,
c)
Output
4 9 16
Common Exam Mistakes ❌
v Forgetting
colon :
after function definition
v Wrong
indentation
v Confusing
parameter and argument
v Writing
default arguments before required arguments
v Forgetting
to call the function
Most Important Programs for SEE Exam
Program
1: Add Two Numbers
def add(a, b):
return
a +
b
print(add(5, 3))
Program
2: Find Area of Rectangle
def area(length, width):
return
length *
width
print(area(5, 8))
Program
3: Default Argument
def power(base, exp=2):
return
base **
exp
print(power(4))
Program
4: Keyword Argument
def student(name, age):
print(name,
age)
student(age=15, name="Ram")
Program 5: Local and Global Variable
x = 10
def show( ):
y = 5
print(x)
print(y)
show(
)
No comments:
Post a Comment