Functions in Python

Functions are the set of statements which does a given charge and gives the output. It is a brick of code which only runs upon calling. We could write a function code as scrap of main program. There are 2 types of functions namely, built-in function and user-defined function where, Python uses built-in functions.

We characterize functions using the ‘def’ statement which takes 3 values as input like function names, parameters, or arguments. It mentions the values which are passed to this function for a given cell, this might have a state called ‘return’. The body is indented as we had for ‘if’, ‘for’, and ‘while’. Return statement if confronts, i.e., at this point, the enactment of function will end and you’ll return to where you called the function from returning the value, where value will be any expression constant.

def f(x, y, z): declaration_1                                               
declaration_2                                                    declaration_3                                                    …. return(c)                               

Recursive Functions

Functions can call itself i.e., they rely on themselves which are known as Recursive Functions. In below example, if n >= 0, then take the recent number and multiply with smaller factorial

def factorial(n):                                     
… if n <= 0:                                  
…    return(1)  else:                                                            num = n * factorial(n – 1)                                        return(num) 

Passing Values to Functions

When we call any function, we have to pass some values which is similar to assigning a value a name.

def power(p, q):                                               
… sol = 1                                         
… for i in range(0, 1):                          
…     sol = sol * p                            
… return(sol)

# here, let p be 2 and q be 6 and run the code. Same is worked under mutable and immutable values. When we assign p = q and if its immutable i.e., value in q cannot be changed in place, then we copy the value and makes a fresh copy in p, so value in p and q are disjoined.                    In mutable values, we do not copy, instead we can point both the names to same copy of value so changes in one list will make changes in the other.                                                                                                                        

def update(l, i, v):                                 
…  if i >= 0 and i < len(l):                              
…    l[i] = v                                                  
…  else:                                       
…      v = v+1                    

# consider this example,

ns = [5, 13, 14]                                   
z = 7                                  
update(ns, 4, z)                            
update(ns, 6, z) 

# ns is [5, 13, 7], we have 2 return statement here which indicates the calling function whether the update is succeeded or not. Sometimes some functions don’t return any useful things at all so they display error or some things which are unable to understand, so after reaching at the last statement, there the function will end so the return value can be ignored.

Defining Functions

The functions must be defined before it is entreated. Below example is a proper form in which we can call a function, it’s useful only if we define all functions. When we intermix the statements, then be careful that the functions are not referring the later things which do not been scanned yet. This is the reason why we always put def_function at the beginning and below them put the statement you want to execute. For example-     

def f(x):                                        
…. return g(x + 1)                               
def g(y):                                      
…. return g(y + 2)

Scope of Names

In python, names within a function disjoints from names outside a function.

def english(p):                     
n = 43              
n = 2                                                                 v = english(67)

# now we called n to be 2, we call this function n because 43 is inside the function. Is n = 43  now or not? n is still 2 because, n inside and n outside are different versions of n. Any name used inside a function is to be thought of as disjoint from name outside. So names outside the functions are not visible inside the function and vice versa. This is not the thing we normally do. So it will be useful if we have things separately, because sometimes we use common things to run through the list. 

Categories: Python

Tags: , , , , , ,

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: