Function

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

Main Topic: Computer Science with Python

Sub Topic: Function


Function or Method: A function is an independent, individual block of code
that are designed to perform specific task.
Advantage of function / method:
 Helps to implement the concept of modularity.
 Reduces code redundancy i.e. minimizes repetition of code.
 Easy error detection and correction.
 Code optimization: No need to write lot of code.
Type of Functions in Python:-
 Built-in function: the function that are pre-designed and available with
the programming language.
Eg. int( ), float( ), type( ), len( ), input( ) etc.
 Function defined in modules: These are also pre-defined functions in a
particular modules and can be used when the corresponding module is
imported.
E.g. math.pow(x,y)
 User defined function: the functions that are designed by the
programmer as per the requirement.
Function Prototype: The first line of function definition is known as function
prototype, in python it describes the function name, parameter/ argument list
and it is also known as function header.
Syntax of Declaring Python function/ method:-
def Function Name (Argument/ parameter) :

Body of Function

Eg:
def SUM( x,y):
print (“Welcome to Python”)
Function Name: It is a type of identifier that identifies a function from other.
Function name is used to invoke/ call a function.
Parameter/ Argument: Parameters are generally used for massage passing or
data passing i.e. if there is a need of passing values form one function to other
then parameters are used.
Parameters can be of two types:
 Formal parameters: Parameters that are declared during function
definition or prototype, such parameters are known as formal parameters.
 Actual parameters: These parameters are the actual values that are
passed to formal parameters during a function call or invocation.
Function Signature: It is a part of function prototype that defines the number
and type of parameter or argument.
Indentation: The blank space in the beginning of a statement within a block.
All statements within same block have same indentation.

Flow of Execution: The flow of execution refers to the order in which


statements are executed during a program run.
def Fun( ):
:
:
return
:
:
print(Fun( ))

Guidelines followed by Python to execute a program:


 Execution begins at the first statement of the program.
 Command lines are ignored.
 In presence of user defined function only the function header is executed
ignoring the body.
 Body of function gets executed on function call.
 Function execution ends with a return statement or the last statement of
function body, whichever occurs earlier.
Types of user defined function:
Depending on return type and parameters user defined function can be
categorised in four categories:
 Non Returning Non Parameterized function
 Non Returning Parameterized function.
 Returning Non Parameterised function.
 Returning Parameterized function.

Non Returning Non Parameterized function:-

def SUM( ): # Non returning non parameterized


a=int(input(“Enter First no.:”))
b=int(input(“Enter Second no.:”))
s=a+b
print(“ The result is=”,s)
SUM( ) # function call

Non Returning Parameterized function:-

def SUM( x , y ): # Non returning parameterized


s=x+y
print(“ The result is=”,s)

a=int(input(“Enter First no.:”))


b=int(input(“Enter Second no.:”))
SUM(a,b ) # function call

Returning Non Parameterised function:-

def SUM( ): # returning non parameterized


a=int(input(“Enter First no.:”))
b=int(input(“Enter Second no.:”))
s=a+b
return s

r=SUM( ) # function call


print(“The result is=”,r)
Returning Parameterised function:-

def SUM( x,y): # returning parameterized


s=x+y
return s

a=int(input(“Enter First no.:”))


b=int(input(“Enter Second no.:”))
r=SUM( a,b) # function call
print(“The result is=”,r)

*Python supports 3 types of formal arguments/ parameters:

 Positional arguments (Required arguments)


 Default arguments
 Keyword or named arguments

Positional/ Required arguments:


During a function call the arguments must be provided for all parameter and the values of
arguments are matched with parameter position wise, this way of parameter and argument
specification is called positional arguments or required argument or mandatory arguments.

Default Arguments:
A parameter having default value in the function header is known as a default
parameter.

e.g def Area_circle(r,pi=3.141):

Note: In a function, any parameter cannot have a default value unless all
parameters appearing on its right have their default values.

A parameter having a default value in function header becomes optional in


function call. Function call may or may not have value for it.

Rules for combining all three types of argument:


 An argument list must first contain positional arguments followed by any
keyword arguments.
 Keyword arguments should be taken from the required argument
preferably
 We cannot specify a value for an argument more than once.
Note: Having a positional argument after keyword arguments will result into
error.
Scope in Python:
It defines the part(s) of program within which a name is legal and accessible.

In python scope is broadly categorized in two types:-


 Global Scope:
Any identifier declared in top level segment of a program is said to have
global scope and is usable inside the whole program and all blocks
contained within program.
 Local Scope:
Any identifier declared within a function block, will have a local scope i.e.
the identifier is accessible within the declared block.

E.g.
x=5 #global variable
def Func(a):
b=a+1 # b is local variable
return b

y= input(“Enter a number”)
z=y + func(x)
print(z)

Resolving Scope of a Name:


Python follows the following name resolution technique known as LEGB.
 It checks within its local environment or local namespace or else moves to next step.
 Then it checks for the Enclosing environment or else moves to next higher
environment.
 Then python checks for Global environment or else moves to next environment.
 Lastly python checks for the Built in environment.

Returning Multiple Values:


Python has the ability to return multiple values, keeping the following rules in consideration:
 Return should have the following format
return <value1/ variable1/ expression1>, < value2/ variable2/ expression2>,….
 The function call statement should receive or use the returned values in one of the
following ways:
o Either receive the returned values in form a tuple variable
def squared(x,y,z):
return x*x, y*y, z*z
t= squared(2,3,4)
print (t)
o By directly unpacking the received values of tuples by specifying the same
number of variables.
def squared(x,y,z):
return x*x, y*y, z*z
v1,v2,v3= squared(2,3,4)
print (v1,v2,v3)

You might also like