functions in python

Post on 22-Jun-2015

132 Views

Category:

Education

3 Downloads

Preview:

Click to see full reader

DESCRIPTION

Functions in python

TRANSCRIPT

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

FUNCTIONS IN PYTHON

Shafeeque

shafeequemonp@gmail.com

www.facebook.com/shafeequemonp pambodan

twitter.com/shafeequemonp

in.linkedin.com/in/shafeequemonp

9809611325

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}

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);

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.

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!

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

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: info@baabtra.com

top related