Functions in python

9

description

Functions in python

Transcript of Functions in python

Page 1: Functions in python
Page 2: Functions in python

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Functions in python

FUNCTIONS IN PYTHON

Shafeeque

[email protected]

www.facebook.com/shafeequemonp pambodan

twitter.com/shafeequemonp

in.linkedin.com/in/shafeequemonp

9809611325

Page 4: Functions in python

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}

Page 5: Functions in python

Function Header:

{Int a=0;a=x+y;return a}

It has three main partsThe name of the function i.e. sumThe parameters of the function enclosed in paranthesisReturn 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 namei.e:Here,for eg: sum(1,2);

Page 6: Functions in python

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.

Page 7: Functions in python

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 promptFor above example,function can be called as printme(“hai friends!”) output: hai friends!

Page 8: Functions in python

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

Page 9: Functions in python

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]