User define Functions:
- In Python, a user-defined functions declaration begins with the keyword def followed by the function name.
- To access the functions we need to call them, without calling the function we cannot access the functions.
Syntax:
def function_name(parameters) :
Statements
- - -
- - -
return values
Note: Here, return is an optional statement.
Example: Write a function to print the addition of two variables.
1) def addition() :
2) a,b = 10,20
3) print("Addition of two variables: ", (a+b))
4) addition() #Function calling
output:
Addition of two variables: 30
Parameters:
A parameter is a special kind of variable they are used to input the value to the function. If a function contains parameters, then at the time of the function call, we should provide values otherwise, it will get an error.
Example: Write a function to print the addition of two variables using parameter input.
1) def addition(a, b) :
2) print("Addition of two variables: ", (a+b))
3) addition(10, 20)
4) addition(2.5, 5.2)
output:
Addition of two variables: 30
Addition of two variables: 7.6
Return Statements:
A return statement is used to return a value from a function.
Example 1:
1) def add(a, b) :
2) c = a+b
3) return c
4) x = add(10, 20)
5) print(x) #30
Example 2:
1) def num() :
2) a = 10
3) b = 20
3) return a,b
4) x, y = num()
5) print(x+y) #30
Function Arguments:
Syntax:
def add(a, b) :
- - -
- - -
add(10, 20)
Here a, b are formal arguments whereas 10, 20 are actual arguments.
There are 4 types of actual arguments in Python.
1) positional arguments
2) keyword arguments
3) default arguments
4) arbitrary arguments (Variable length arguments)
These are the arguments passed to function in the correct positional order.
Eg:
def add(a, b) :
print(a+b)
add(10, 20) #30
add(10, 20, 30) #TypeError:
The function must be called with the correct number of arguments, not more and not less.
2) keyword arguments:
We can pass argument values by parameter names.
Eg:
1) def sub(a, b) :
2) print(a-b)
3) sub(a=20, b=10) #10
4) sub(b=20, a=10) #-10
5) sub(b=10, a=20) #10
6) sub(20, b=10) #10
7) sub(a=20, 10) #SyntaxError:
3) default arguments:
We can provide default values to function arguments.
Eg:
1) def add(a=10, b=20)
2) print("Sum: ", a+b)
3) add() #Sum: 30
4) add(2, 3) #Sum: 5
5) add(5, b=15) #Sum: 20
6) add(a=5, 8) #SyntaxError:
4) arbitrary arguments/variable length arguments:
- If you do not know how many arguments that will be passed into your function then in that case we will use these arbitrary arguments.
- We can declare an arbitrary argument by adding the * symbol before the parameter name in the function definition.
- We can call the function by passing any number of arguments including zero number. Internally all these values are represented in the form of tuple.
Eg:
1) def add(*n) :
2) total = 0
3) for i in n :
4) total = total + i
5) print("Addition : ", total)
6) add() #Addition : 0
7) add(10) #Addition : 10
8) add(10, 20) #Addition : 30
Eg:
1) def tc(a, *n) :
2) print(a)
3) for i in n :
4) print(i)
5) tc(10) #10
6) tc(10, "A", "B", "C") #10, A, B, C
Eg: After an arbitrary argument, if we are taking any other arguments we should provide values as keyword arguments.
1) def tc(*n, a) :
2) print(a)
3) for i in n:
4) print(i)
5) tc("A", "B", "C", a=10) #A B C 10
6) tc("A", "B", "C", 10) # Error
* * * * * * * * * * * * * * * * * * * * *
- We can also declare an arbitrary argument by adding the ** symbol before the parameter name in the function definition.
- We can call the function by passing any number of keyword arguments. Internally all these values are represented in the form of a dictionary.
Eg:
1) def testing(**tc) :
2) for x,y in tc.items() :
3) print(x, "=", y)
4) testing(a=10,b=20, c="colleges")
#output:
a = 10
b = 20
c = colleges
No comments:
Post a Comment