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

Functions

Python
Programming

Dr. Emmanuel S. Pilli


Associate Professor, CSE
Functions
 A block of code that performs a specific and well defined task
 Functions help divide the program into multiple tasks.
 A big task can be defined as many smaller functions making the
programme modular
 Functions provide reuse mechanism and the same function can
be called any number of times
 Functions help in debugging and identifying the location of the
faults / logical errors easily
Types of Functions
 Built-in Functions – functions are predefined and are included as
part of the python compiler
 User define functions – programmer can create functions and
use them in programmes
 Format of a User defined function fun( )
 def fun ( ):
statements
statements
Function Rules
 Function can be called any number of times
 When a function is called, the control is transferred to the
function . The statements in the function are executed and
control is returned to place from where it originated
 Use lower case and underscore (_) is used to connect words
 Function can be redefined. Latest definition will be called.
 Function definitions can be nested. The inner function is able
to access the variables of the outer function. The outer
function has to be called for inner function to be executed.
Communication with Functions (Arguments)
 Communication with functions can be done using parameters
or arguments passed to it and the value(s) returned from it.
 The way to pass values to a function and return value from
the function:
 def fun (p1, p2, p3):
statements
return(r)
 Return statement returns control and value from a function.
Return without an expression returns None
Communication with Functions (Arguments)
 To return multiple values from a function, store them in a list,
tuple, set or dictionary and return them
 If we pass arguments a, b, c to a function and collect them in
x, y, z then changing x, y, z values will not change a, b, c
 Function is always called by value
 A function can return different values through different return
statements
 The function which reaches the end of the execution without
a return statement will always return None
Types of Arguments
 Four types of Arguments
 Positional arguments
 Keyword arguments
 Variable length positional arguments
 Variable length keyword arguments

 Positional and Keyword arguments are called required


arguments and variable length arguments are called
optional
Types of Arguments
 Positional arguments must be passed in correct positional order
 Type of arguments and Number of arguments passed must
match with the type and number received
 Keyword arguments can be passed out of order
 Python interpreter uses keywords (variable names) to match
the values passed with the arguments used in the function
definition
 A function call can have positional and keyword arguments, but
the positional arguments must precede keyword arguments

You might also like