Python Functions

Python functions: It can be referred to as a block of code that performs a specific task. A function declaration tells the compiler about a function’s name, return type, and parameters. And in Python functions definition provides the actual body of the function. Python functions can also be referred to as a method or a sub-routine or a procedure, etc.

Syntax:
def functionname( parameters ):”function_docstring”
function_suitereturn [expression]

Example

def greet(name):
"""This function greets to the person passed in as parameter"""
print("Hello, " + name + ".Welcome!")

Python Functions



Python Function Parameters

Python function Parameters are part of sub-routines, they provide information to the sub-routines and tells them how to behave.

Python function parameters Example:

def my_function(fname): 
print(fname + "Message") 
my_function("Gmail") 
my_function("Yahoo") 
my_function("Outlook")

 Output:

Gmail Message
Yahoo Message
Outlook Message

Python Default Value of Parameter

If you call the Python function without parameter, it uses a default value.

Example:

def my_function(country="India"):
print("I am from " + country) 
my_function("Australia") 
my_function("USA") 
my_function( ) 
my_function("Russia")

Output:

I am from Australia
I am from the USA
I am from India
I am from Russia

Python Return Values

In python functions, for getting a return value, use the return statement.

Syntax: return [expression_list]

Example:

def my_function(x):
return 6*x
print(my_function(3))
print(my_function(5))
print(my_function(9))

Output
18
35
54

Python Recursion

Python recursion is a process in which a function calls itself as a subroutine. This allows the function to be repeated several times since it calls itself during its execution. Functions that incorporate recursion are called recursive functions.

Example

def tri_recursion(g): 
if(g>0): 
result = k+tri_recursion(g-1) 
print(result) 
else: 
result=0 
return result 
print("\n\nRecursion Example Results") 
tri_recursion(6)

Output
1
3
6
10
15
21

Python Function Calling

In python, function calling can be defined as a call that passes control to a subroutine. Once the subroutine is executed control returns to the next instruction in the main program. If the basic structure of the function is finalized, you can execute it by calling from another function or directly from the python prompt.

 Example

def my_function(): 
print("Hello from a function") 
my_function()

Output

Hello from a function

Python Pass by Reference VS Value

It means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function. While the called Python functions can modify the value of the argument by using its reference passed in. Hence arguments in python are passed by reference. Python Pass-by- references are more efficient than pass-by-value because it does not copy the arguments. While the formal parameter is an alias for the argument.

Example

#!/usr/bin/python
#Function definition is here
def changeme( mylist ):"This changes a passed list into this function"
mylist.append([1,2,3,4]);
print "Values inside the function: ",
mylistreturn# Now you can call changeme 
functionmylist = [10,20,30];
changeme( mylist );
print "Values outside the function: ", 
mylist

Output

Values inside the function: [10, 20, 30, [1, 2, 3, 4]]

Values outside the function: [10, 20, 30, [1, 2, 3, 4]]

NOTE: The difference between pass-by-reference and pass-by-pointer is that pointers can be NULL or reassigned whereas references cannot.