2 Functions1
2 Functions1
def max(x,y):
if x>y:
return x
else:
return y
print(“Iam not reachable”)
Function not returning value
Function may or may not return a value. Non returning function
is also known as VOID function. It may or may not contain
return. If it contains return statement, then it will be in the form
of:
return [no value after return]
Parameters and Arguments in Function
Parameters are the value(s) provided in the parenthesis
when we write function header. These are the values
required by function to work
If there are more than one parameter, it must be separated
by comma (,)
An Argument is a value that is passed to the function when it
is called. In other words, arguments are the value(s)
provided in function call/invoke statement
Parameter is also known as FORMAL
ARGUMENTS/PARAMETERS
Arguments is also known as ACTUAL
ARGUMENTS/PARAMETER
Note: Function can alter only MUTABLE TYPE values.
Example of Formal/Actual Arguments
FORMAL ARGUMENT
ACTUAL ARGUMENT
Types of Arguments
There are 4 types of Actual Arguments allowed in
Python:
1. Positional arguments
2. Default arguments
3. Keyword arguments
4. Variable length arguments
Positional arguments
Are arguments passed to a function in correct
positional order?
print(name.replace("m","nt").upper()) #function
Scope of Variables
SCOPE means in which part(s) of the program, a
particular piece of code or data is accessible or
known.
In Python there are broadly 2 kinds of Scopes:
Global Scope
Local Scope
Global Scope
A name declared in top level segment ( main ) of
a program is said to have global scope and can be
used in entire program.
Variable defined outside all functions are global
variables.
Local Scope
A name declare in a function body is said to have
local scope i.e. it can be used only within this
function and the other block inside the function.
The formal parameters are also having local scope.
Let us understand with example….
Example – Local and Global Scope
Example – Local and Global Scope
Program with
variable “value” in
both LOCAL and
GLOBAL SCOPE
Predict the output
Program with
variable “value” in
both LOCAL and
GLOBAL SCOPE
Predict the output
Using GLOBAL
variable “value” in
local scope
Predict the output
Using GLOBAL
variable “value” in
local scope
Predict the output
Variable “value”
neither in local nor
global scope
Predict the output
Variable “value”
neither in local nor
global scope
Predict the output
Variable in Global
not in Local
(input in variable at
global scope)
Predict the output
Variable in Global
not in Local
(input in variable at
global scope)
Mutability/Immutability of
Arguments/Parameters and function call
Mutability/Immutability of
Arguments/Parameters and function call
Mutability/Immutability of
Arguments/Parameters and function call
From the previous example we can recall the
concept learned in class XI that Python variables
are not storage containers, rather Python variables
are like memory references, they refer to memory
address where the value is stored, thus any change
in immutable type data will also change the
memory address. So any change to formal
argument will not reflect back to its
corresponding actual argument and in case of
mutable type, any change in mutable type will
not change the memory address of variable.
Mutability/Immutability of
Arguments/Parameters and function call
Most non-python
programmers are having the
habit of writing main()
function where the important
and starter code of programs
are written. In Python we can
also create main() and call it
by checking name to
main and then call any
function, in this case main()
Recursion
It is one of the most powerful tool in programming
language. It is a process where function calls itself
again and again.
Recursion basically divides the big problem into small
problems up to the point where it can be solved easily,
for example if we have to calculate factorial of a 5, we
will divide factorial of 5 as 5*factorial (4), then
4*factorial (3), then 3*factorial (2), then 2*factorial (1)
and now factorial of 1 can be easily solved without any
calculation, now each pending function will be executed
in reverse order.
Condition for Implementing Recursion
It must contain BASE CONDITION i.e. at which point recursion will end
otherwise it will become infinite.
BASE CONDITION is specified using „if‟ to specify the termination
condition
Execution in Recursion is in reverse order using STACK. It first divide the
large problem into smaller units and then starts solving from bottom to
top.
It takes more memory as compare to LOOP statement because with
every recursion call memory space is allocated for local variables.
The computer may run out of memory if recursion becomes infinite or
termination condition not specified.
It is less efficient in terms of speed and execution time
Suitable for complex data structure problems like TREE, GRAPH etc
Example - Recursion
Example - Recursion
Questions based on functions
WAP to create function Lsearch() which takes List
and number to search and return the position of
number in list using Linear Searching method