Computer AI 2
Computer AI 2
FUNCTION
PRESENTED BY : JERIN WILLIAMS . F
Introduction to
Function
A Function is a subprogram that
acts on data and often returns
a value
Functions help break our
program into smaller and
modular chunks.
As our program grows larger
and larger, functions make it
more organized and
manageable.
Advantages of functions:-
Increases readability, particularly for longer code as
by using functions, the program is better organised
and easy to understand.
Reduces code length as same code is not required to
be written at multiple places in a program. This also
makes debugging easier.
Increases reusability, as function can be called from
another function or another program.
Work can be easily divided among team members
and completed in parallel.
Python Function Types:-
Built-in Fuction : Functions that are built into
Python
Function defined in modules : Functions that are
imported
User defined : Functions defined by the users
themselves.
Syntax:-
def functionname( parameters ):
"function_docstring"
function_suite
return [expression]
Example:-
def printme( str ):
"This prints a passed string into this function"
print str
return
Creating User Defined Function:-
The items enclosed in "[ ]" are called
parameters and they are optional. Hence, a
function may or may not have parameters.
Also, a function may or may not return a
value.
Function header always ends with a colon
(:).
Function name should be unique. Rules for
naming identifiers also applies for function
naming.
The statements outside the function
indentation are not considered as part of
the function.
Flow of execution in a function
cell:-
ARGUMENTS PARAMETERS
KEYWORD ARGUMENTS:-
KEYWORD ARGUMENTS ARE THE
NAMED ARGUMENTS WITH
ASSIGNED VALUE BEING PASSED
IN THE FUNCTION CALL
STATEMENT
PASSING PARAMETERS:-
Positional arguments
Default parameter
Keyword arguments
POSITIONAL PARAMETERS:-
A positional parameter is an
argument specified on the
command line, used to launch the
current process in a shell
THANK
YOU