python functions

Python Functions with Examples

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.

python functions

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


Output:

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)


Output:

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))


Output:

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.

Discover Our Exciting Courses and Quiz

Enroll now to enhance your skills and knowledge!

Python Online Quiz

Level up your coding skills with our interactive programming quiz!