Python Functions
Python Functions
Types of Functions:
1) Built-in Functions: Functions that are predefined. We have used many predefined
functions in Python.
2) User- Defined Functions: Functions that are created according to the requirements.
Defining a Function:
1) Keyword def is used to start the Function Definition. Def specifies the starting of Function
block.
3) Parameters are passed inside the parenthesis. At the end a colon is marked.
4) Before writing a code, an Indentation (space) is provided before every statement. It should be
same for all statements inside the function.
Syntax:
1
Invoking a Function:
Function Definition provides the information about function name, parameters and the
definition what operation is to be performed. In order to execute the Function Definition it is to
be called.
Syntax:
1. <function_name>(parameters)
2. </function_name>
eg:
sum(a,b)
Here sum is the function and a, b are the parameters passed to the Function Definition.
s=x+y
Output:
print ('sum of two numbers:', s)
sum of two numbers: 30
return;
sum of two numbers: 50
# Now you can call sum function
sum(10,20)
sum(20,30)
Return Statement:
Return [expression] is used to send back the control to the caller with the expression.
2
Arguments can be literals, variables and expressions.
Eg:
def addition(x,y):
x=15 >>>
addition(x ,10)
1. 25
addition(x,x) 2. 30
y=20 3. 35
4. >>>
addition(x,y)
Passing Parameters
Apart from matching the parameters, there are other ways of matching the parameters.
2) Default argument.
Positional/Required Arguments:
When the function call statement must match the number and order of arguments as
defined in the function definition it is Positional Argument matching.
Eg:
1. def sum(a,b): 1. Output: >>>
2. c=a+b 30
3. Print(‘c’) >>>
4. return;
2. sum(20)
5. sum(10,20) 3. TypeError: sum() takes exactly 2 arguments (1 given)
6. sum(20) >>>
3
Explanation:
1) In the first case, when sum() function is called passing two values i.e., 10 and 20 it matches
with function definition parameter and hence 10 and 20 is assigned to a and b respectively. The
sum is calculated and printed.
2) In the second case, when sum() function is called passing a single value i.e., 20 , it is passed
to function definition. Function definition accepts two parameters whereas only one value is
being passed, hence it will show an error.
Default Arguments
Default Argument is the argument which provides the default values to the parameters
passed in the function definition, in case value is not provided in the function call.
Eg:
def msg(Id,Name,Age=21): Output: >>>
print('Id',Id)
100
print ('Name',Name)
1. Ravi
print ('Age',Age)
2. 20
return 3. 101
msg(Id=100,Name='Ravi',Age=20) 4. Ratan
21
msg(Id=101,Name='Ratan')
Explanation:
1) In first case, when msg() function is called passing three different values i.e., 100 , Ravi and
20, these values will be assigned to respective parameters and thus respective values will be
printed.
2) In second case, when msg() function is called passing two values i.e., 101 and Ratan, these
values will be assigned to Id and Name respectively. No value is assigned for third argument via
function call and hence it will retain its default value i.e, 21.
Keyword Arguments:
Using the Keyword Argument, the argument passed in function call is matched with
function definition on the basis of the name of the parameter.
4
Eg:
def msg(id,name):
return Raj
msg(id=100,name='Raj') 101
msg(name='Rahul',id=101) Rahul
Explanation:
1) In the first case, when msg() function is called passing two values i.e., id and name the
position of parameter passed is same as that of function definition and hence values are
initialized to respective parameters in function definition. This is done on the basis of the name
of the parameter.
2) In second case, when msg() function is called passing two values i.e., name and id, although
the position of two parameters is different it initialize the value of id in Function call to id in
Function Definition. same with name parameter. Hence, values are initialized on the basis of
name of the parameter.