A function is a block of code that performs some specific task and returns back to the caller. It has a unique name.
They are helpful when need to perform the same task with different-different values or require to use the same block of code in other places within the program.
If any changes require in the future then only need to update the function code and it will automatically make changes where the function it been called.
The main aim of a function is –
- To make the program more readable, and organized,
- Reduce code repetition.
Contents
- Defining a function
- Calling a function
- Function with parameter
- Function with return
- Default value
- Multiple parameters
1. Defining a function
To create function def
keyword is use in Python. Define a unique name for the function and in parenthesis, you can specify your parameters.
Function block starts with colon(:)
symbol.
Syntax –
def function-name(paramenter): # statement
Example
def callMe(): print("Function is called")
In the above example, I have created a callMe()
function which not take any arguments. It prints text on the screen when it is called.
2. Calling a function
Specify the function name and specify the parameters in parenthesis () if its takes. After completing the task it returns back to the program.
Syntax –
def function-name(parameter): # defining function # statement function-name(parameter) # calling function
ExampleÂ
def callMe(): print("Function is called") callMe() callMe()
Output
Function is called Function is called
3. Function with parameter
Specify the parameter in the function if you want to perform the task with different-different parameters.
Syntax –
def function-name(parameter): # statement function-name(parameter)
Example
def checkevenOrodd(num): if num%2 == 0: print("Even number") else: print("Odd number") checkevenOrodd(2) checkevenOrodd(5) checkevenOrodd(8)
Output
Even number Odd number Even number
4. Function with return
Use return
statement to return value from the function to the caller.
Syntax –
def function-name(paramenter): # statement return value
Example
def maxlist(list1): max = list1[0] for l in list1: if l > max: max = l return max list1 = [1,55,23,2] list2 = [4,24,78,6,21] print("max value in list1 is ",maxlist(list1)) print("max value in list2 is ",maxlist(list2))
Above I create function maxlist()
which returns max value from the list.
Output
max value in list1 is 55 max value in list2 is 78
5. Default value
Default parameter automatically supplies value even if you don’t supply value while calling the function. Default argument declaration starts from right to left.
Syntax –
def function-name(variable=value,variable=value,...): # statement
ExampleÂ
def displayInfo(fname,lname="Singh",age=22): print("first name : ",fname,", last name : ",lname,", age : ",age) displayInfo("Yogesh") displayInfo("Vishal",age=24) # Changing age default value displayInfo("Mohit","Sharma")
Above I specify the default value in lname
and age
variable. If these variable values not defined while calling then it will use the default set value.
Output
first name : Yogesh , last name = Singh , age : 22 first name : Vishal , last name = Singh , age : 24 first name : Mohit , last name = Sharma , age : 22
6. Multiple parameters
For handling an unknown amount of items to function we use a asterisk (*)
symbol in function. It holds as many values in a single argument.
Syntax –
def function-name(*argument): # statement
Example
def display(name,*friends): print("name : ",name) print("friends : ",friends) display("Yogesh singh","Sonarika","Vishal","Vijay") display("Jiten singh","Mohit","Ajay","Abhilash","Ganesh","Aditya")
I defined asterisk (*) on the second argument. When the function is called then the first parameter goes to name
variable and all other parameters goes to friends
variable.
It converts the friends
argument into a tuple.
Output
name : Yogesh singh friends : ('Sonarika','Vishal','Vijay') name : Jiten singh friends : ('Mohit','Ajay','Abhilash','Ganesh','Aditya')If you found this tutorial helpful then don't forget to share.