0% found this document useful (0 votes)
18 views10 pages

Computer AI 2

This document discusses functions in Python. It defines a function as a subprogram that acts on data and can return a value. Functions help organize code by breaking programs into smaller, modular chunks. They increase readability, reduce code length through reusability, and allow for parallel work. There are built-in, module, and user-defined functions. User-defined functions are defined using the def keyword and can take optional parameters. Functions execute code through the flow of arguments passed into parameters. Parameters can have default values and arguments can be passed positionally or through keywords.

Uploaded by

Jerin Williams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views10 pages

Computer AI 2

This document discusses functions in Python. It defines a function as a subprogram that acts on data and can return a value. Functions help organize code by breaking programs into smaller, modular chunks. They increase readability, reduce code length through reusability, and allow for parallel work. There are built-in, module, and user-defined functions. User-defined functions are defined using the def keyword and can take optional parameters. Functions execute code through the flow of arguments passed into parameters. Parameters can have default values and arguments can be passed positionally or through keywords.

Uploaded by

Jerin Williams
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

WORKING WITH

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

An argument is a value An argument is a


passed to the function value recieved to the
during the function call function during the
function call
DEFAULT PARAMETER:-
A PROGRAM HAVING DEFAULT
VALUE IN FUNCTION HEADER IS
CALLED DEFAULT 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

You might also like