Python Function

In this tutorial you will learn about Python function.

Functions are a great way to support programs that consists of many lines of code. They go off and perform a task and then return control to your program.

Creating your own functions offers you many advantages and features. It allows you to break up your code into manageable and small chunks. Some python programs that are long series of instructions with no logical breaks are hard to maintain understand and write. Functions, if included in a program can help the programmers to easily develop it and maintain it properly.

Python provides built-in functions such as print(), range(), len() and many more. However, if you need to develop your own functions, it is quite possible to create it in Python like in other programming environments, also known as user-defined functions.

 

Python Function Definition

Syntax

def function_name():
	statement 1
	statement 2
	statement n

 

Example

def addition():

 

This statement would tell the python interpreter that a function named addition is being defined at the prompt and following this would be the actual function block definition.

Note: It is important to note that the indentations are required to be proper because if it is not then the Python interpreter would not be able to identify the scope or body of the function and hence it will raise an error.

 

Python Function Calling

Without Parameters

Syntax

Function_Name()

 

Example

addition()

 

In case you don’t want to pass any value to a user defined function, you can directly write the name of the function and the control will go to the function definition, execute the statements within the function and the moment all the statements have been executed by the interpreter, the control will come out the function and will return to the next line where the calling of function was done.

 

With Parameters

Syntax

Function_Name(Parameter 1.., Parameter n)

 

Example

addition(var1,var2)

 

Suppose you want to pass a value to the function, you can directly write it in the function name during the calling of a function and it will work for you. For multiple values passing, you can separate it by including a Comma (,).

 
Example

def addition(var1,var2):   #Defining the Function
	var3=var1+var2
	print("You are in Addition Function")
	print(var3)

var1=5
var2=4
addition(var1,var2)        #Calling the Function
print("You are back to Main Function")

 

Output

Python Function 1

 

In this program, we have pre-declared the user-defined function i.e., before the actual main function. We have declared an addition() that takes in two parameters. A new variable is declared that stores in the addition of var1 and var2.

When the program is interpreted, it first checks in the value of var1 and var2. Then, it encounters the addition(var1,var2) statement which then takes the control of the program to def addition(var1,var2) and hence the following three statements are executed by the Python interpreter. After print(var3), control comes back to calling function and the remaining statement print(“You are back to Main function”) is executed.

 

Passing Default Values

def addition(var1,var2): #Defining the Function
	var3=var1+var2
	print("You are in Addition Function")
	print(var3)

var1=5
var2=4
addition(var1=7,var2=10)
print("You are back to Main Function")

 

Output

Python Function 2

 

Here, we have used passing default values which override the previous values that have been assigned to variables var1 and var2. The values that var1 consist of previously will be overwritten by 7 and that of var2 by 10 and the same values will be passed into the user defined function.

 

Python return Statement

The return statement helps to return a value from the function to the calling function.

 

Example

def addition(var1,var2):   #Defining the Function
	var3=var1+var2
	print("You are in Addition Function")
	return var3   #return statement

var1=5
var2=4
var3=addition(var1=7,var2=10)
print("This is return Function addition():",var3)

 

Output

Python Function 3

 

In the above example, we have created an addition function which after executing the statement var3=var1+var2 stores the value in the variable var3 and returns this value into whatever function has called it.

Leave a Comment

Your email address will not be published. Required fields are marked *