This document discusses functions in Python. It defines a function as a block of reusable code that has a name. The function header includes the name, parameters in parentheses, and return type. The function body contains the code within curly braces. Functions are called by their name and parameters are passed within parentheses. Examples are provided of defining, calling, and returning values from functions in Python.
2. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra âMentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
4. What is a Function?
ï§A function is a block of code that has a name and it
has a property that it is reusable
i.e. it can be executed from as many different points
in Program as required
Structure of a Function
ï§There are two main parts of the function.
i.e:The function header and the function body.
int sum(int x,int y)
{
Int a=0;
a=x+y;
return a
}
5. ïFunction Header:
{
Int a=0;
a=x+y;
return a
}
It has three main parts
ï§The name of the function i.e. sum
ï§The parameters of the function enclosed in
paranthesis
ï§Return value type
ï§Eg:int sum(int x,int y)
ïFunction Body:
ï§What ever is written with in { } in the above
example is the body of the function.
ïFunction Call:
ï§Function is called by function name
ï§i.e:Here,for eg:
sum(1,2);
6. FUNCTION IN PYTHON:
Defining a Function
ï§Function blocks begin with the keyword def followed by the function
name and parentheses ( ( ) ).
ï§Any input parameters or arguments should be placed within these
parentheses. You can also define parameters inside these parentheses.
ï§The first statement of a function can be an optional statement - the
documentation string of the function or docstring.
ï§The code block within every function starts with a colon (:) and is
indented.
ï§The statement return [expression] exits a function, optionally passing
back an expression to the caller.
ï§A return statement with no arguments is the same as return None.
7. Syntax:
def functionname( parameters ): "function_docstring" function_suite return [expression]def functionname( parameters ): "function_docstring" function_suite return [expression]
def functionname (parameters):
âfunction docstringâ
function_suite
return [expression]
def functionname( parameters ):
ï§By default, parameters have a positional behavior, and you need
to inform them in the same order that they were defined.
Example:
def printme(str):
âthis prints the passed strings into this functionâ
print str
return
Calling a Function:
ï§we can execute it by calling it from another function or directly from the Python prompt
ï§For above example,function can be called as printme(âhai friends!â)
ï§ output: hai friends!
8. If this presentation helped you, please visit our
page facebook.com/baabtra and like it.
Thanks in advance.
www.baabtra.com | www.massbaab.com |www.baabte.com
9. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 â 495 40 25 550
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 â 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com