Before learning a functions in python, you must be familiar with the scope of variable as it plays a significant role.
Functions in Python is a self contained independent block of logical code, which perform a specific task. They can not be run independently and are always called by the main program or by some other function. Whenever function called is made flow of central automatically transfer to the function definition and process statement in function body. After execution of flow of control return back to the position where function call was made.
Functions played a very important role in any programming language. In this article we will see:
- Syntax for creating a function
- How to call a function
- Multiple example of functions in Python Advantage of a function
Python function Syntax :
def functionname(parameterlist): #function body
In above syntax, def is a keyword.
functionname is a name of function.
How to call function in Python :
To use a function, you will have to call that function to perform the defined task. When a program calls a function, the program control is transferred to the called function.
To call a function you simply need to pass the required parameters along with the function name, and if function returns a value, then you can store the returned value.
Example [Without parameters]:
def func():#Step2 print("You are inside a fucntion") #step3 return "BYE.." #Step4 a = func() #Step1 step5 print(a) #Step6
You are inside a fucntion BYE..
Here, you made a function name “func” . Initially our control is on “a = func()” line. So it will call a func() and go inside the function body. Therefore, first it will print “You are inside a function” then it will come on to the next line and return “BYE..” . Now our control is come back to the “a = func()” line and stored return value in variable ‘a’ . Now, it will come to the next line “print(a)”, therefore it wil print”BYE..” .
Example [With parameters]:
def sum(a,b): print("Inside a fucntion") return(a+b) print("Start calling a function") result= sum(4,5) print("Outside a function") print(result)
Start calling a function Inside a fucntion Outside a function 9
Here, you made a function name sum with two argument a, b. so whenever we want to call a function sum, we must have to take two argument (Here we take 4,5).
Example [Function name stored in different variable] :
def sum(a,b): print("Inside a fucntion") return(a+b) print("Start calling a function") result= sum print(result(2,7))
Start calling a function Inside a fucntion 9
Here, you stored a function name “sum” with another variable “result”. Then call this function with the help of “result” variable and it will also do the same thing.
Python functions Usage and Advantage :
- It increases the readability of program.
- Functions make efficient use of computer memory.
- One function can be used in many program or in multiple place in a program
- We break large program into small program with the help of functions
- Function are useful when the program size is too large or complex.