Functions 2024 25
Functions 2024 25
Formal Parameter
The formal arguments
are the
parameters/arguments
Function Definition in a function
declaration.
Actual Parameter
The arguments that
are passed in a
Function Call function call are called
actual arguments
In Python, as per our requirements, we can have the
function in either of the following ways:
Function with no argument and no return value
Function with no argument and with return
value(s)
Function with argument(s) and no return value
Function with argument(s) and return value(s)
Function with no argument and no return value
Function with no argument and with return value(s)
Function with argument(s) and no return value
Function with argument(s) and return value(s)
Python allows assigning a default value to the
parameter. A default value is a value that is
predecided and assigned to the parameter when the
function call does not have its corresponding
argument.
myFunc1()
print("Accessing num outside myFunc1 ",num)
print("Accessing y outside myFunc1 ",y)
NameError: name ‘y’ is not defined .y
generates error when it is accessed outside
myfunc1()
Note:
Any modification to global variable is permanent and affects all
the functions where it is used.
If a variable with the same name as the global variable is
defined inside a function, then it is considered local to that
function and hides the global variable.
If the modified value of a global variable is to be used outside
the function, then the keyword global should be prefixed to the
variable name in the function.
num = 5
def myfunc1():
#Prefixing global informs Python to use the updated global
#variable num outside the function
global num
print("Accessing num =",num)
num = 10
print("num reassigned =",num) Output:
#function ends here Accessing num = 5
num reassigned = 10
myfunc1() Accessing num outside myfunc1 10
print("Accessing num outside myfunc1",num)
Namespaces :
A namespace is a container where names are mapped to
objects, they are used to avoid confusions in cases where
same names exist in different namespaces. They are
created by modules, functions, classes etc.
• Local environment
• Enclosing environment
• Global environment
• Built-in environment
Name Resolution (Resolving Scope of a name)
In Python, the LEGB rule is used to decide the order in which the
namespaces are to be searched for scope resolution.
The scopes are listed below in terms of hierarchy(highest to
lowest/narrowest to broadest):
•Local(L): Defined inside function/class
•Enclosed(E): Defined inside enclosing functions(Nested function
concept)
•Global(G): Defined at the uppermost level
•Built-in(B): Reserved names in Python built in modules.
The Python standard library is an extensive
collection of functions and modules that help the
programmer in the faster development of programs.
While a function is a grouping of instructions, a
module is a grouping of functions.
import Own
modulename.funcname()
Note:
import statement can be written anywhere in the
program
Module must be imported only once
In order to get a list of modules available in Python,
we can use the following statement:
>>> help("module")