Functions
Functions
v When the program is too complex and large they are divided
into parts. Each part is separately coded and combined into single
program. Each subprogram is called as function.
v Debugging, Testing and maintenance becomes easy when the
program is divided into subprograms.
v Functions are used to avoid rewriting same code again and
again in a program.
v Function provides code re-usability
v The length of the program is reduced.
• Types of function:
v Built in functions are the functions that are already created and
stored in python.
These built in functions are always available for usage and accessed
by a programmer. It cannot be modified.
ii)User Defined Functions:
v User defined functions are the functions that programmers
create for their requirement and use.
v These functions can then be combined to form module which
can be used in other programs by importing them.
• Example:
def my_add(a,b):
c=a+b
return c
Function Calling:
Ø Once we have defined a function, we can call it from another
function, program or even the Python prompt.
Ø To call a function we simply type the function name with
appropriate arguments.
• Example:
x=5
y=4
my_add(x,y)
Flow of Execution:
v The order in which statements are executed is called the flow of
execution
v Execution always begins at the first statement of the program
v Statements are executed one at a time, in order, from top to
bottom.
v Function definitions do not alter the flow of execution of the
program, but remember that statements inside the function are not
executed until the function is called.
v Function calls are like a bypass in the flow of execution. Instead
of going to the next statement, the flow jumps to the first line of
the called function, executes all the statements there, and then
comes back to pick up where it left off.
Note: When you read a program, don’t read from top to bottom.
Instead, follow the flow of execution. This means that you will read
the def statements as you are scanning from top to bottom, but you
should skip the statements of the function definition until you reach
a point where that function is called.
Parameters And Arguments:
Parameters(Formal Parameters):
· 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 is more than one value required, all of them will be listed
in parameter list separated by comma.
· Example: def my_add(a,b):
Arguments(Actual Parameters) :
· Arguments are the value(s) provided in function call/invoke
statement.
· List of arguments should be supplied in same way as parameters
are listed.
· Bounding of parameters to arguments is done 1:1, and so there
should be same number and type of arguments as mentioned in
parameter list.
· Example: my_add(x,y)
RETURN STATEMENT:
• The return statement is used to exit a function and go back to
the place from where it was called.
• If the return statement has no arguments, then it will not return
any values. But exits from function.
• Syntax:
return[expression]
• Example:
def my_add(a,b):
c=a+b
return c
x=5
y=4
print(my_add(x,y))
• Output:
9
ARGUMENTS TYPES:
1. Required Arguments
2. Keyword(named) Arguments
3. Default Arguments
4. Variable length Arguments
1. Required Arguments:
• The number of arguments in the function call should
match exactly with the function definition.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details("george",56)
• Output:
Name: george
Age 56
2. Keyword(named) Arguments:
• Python interpreter is able to use the keywords provided to match
the values with parameters even though if they are arranged in out
of order.
def my_details( name, age ):
print("Name: ", name)
print("Age ", age)
return
my_details(age=56,name="george")
• Output:
Name: george
Age 56
3. Default Arguments:
• Assumes a default value if a value is not provided in the function
call for that argument.
def my_details( name, age=40 ):
print("Name: ", name)
print("Age ", age)
return
my_details(name="george")
• Output:
Name: george
Age 40
4. Variable length Arguments
• If we want to specify more arguments than specified while
defining the function, variable length arguments are used.
• It is denoted by * symbol before parameter.
def my_details(*name ):
print(*name)
my_details("rajan","rahul","micheal",ä rjun")
• Output:
rajan rahul micheal ä rjun
• MODULES: