Tutorialslot.com

youtube facebook twitter gmail pinterest

Functions In Python


Functions can defined as a block of reusable statements that performs single or related actions.It helps to break programs into smaller chunks and generates result in a organized manner.

Rules for defining Function:
->Function should always begin with the keyword "def" followed by function name and paranthesis.
->values should be passed using paraments or arguments which are defined within the paranthesis.
->A colon (:) should be used to mark the end of function header.
->The documentation string or docstring is optional in function.
->Statements defined in function shpuld have same identation levels.
->It is option to return value from function.

Syntax:
def functionname(parameters):

statements
return[expression]

Example:
def printme( str ):
"This prints a passed string into this function"
print str
return

Calling a Function:
Defining a function only gives it a name, specifies the parameters that are to be included in the function and structures the blocks of code.

Once the basic structure of a function is finalized, you can execute it by calling it from another function or directly from the Python prompt. Following is the example to call printme() function -

def print(string):

print string
return;

print("1st call for function")
print("2nd call for function")
When the above code is executed, it produces the following result -

Output:
1st call for function
2nd call for function

Pass by reference vs value:
All parameters (arguments) in the Python language are passed by reference. It means if you change what a parameter refers to within a function, the change also reflects back in the calling function. For example -

def change( mylist ):
list.append([7,8,9,0]);
print "Values inside: ", list
return

mylist = [100,200,300];
change( list );
print "Values in outside: ", list

Here, we are maintaining reference of the passed object and appending values in the same object. So, this would produce the following result -

Values inside: [100, 200, 300, [7, 8, 9, 0]]
Values in outside: [100, 200, 300, [7, 8, 9, 0]]

There is one more example where argument is being passed by reference and the reference is being overwritten inside the called function.


def change( list ):

list = [10,20,30,40]; # new reference in list
print "Values inside: ", list
return

# Now you can call changeme function
list = [9,8,7];
change( list );
print "Values outside: ", list
The parameter mylist is local to the function changeme. Changing mylist within the function does not affect mylist. The function accomplishes nothing and finally this would produce the following result:

Values inside: [10,20,30,40]
Values outside: [9,8, 7]

Example Program:
We can create a function that writes the Fibonacci series to an arbitrary boundary:

def fibonacci(n): #Fibonacci series up to n

a= 0
b=1
while a < n:
print(a, end=' ')
a, b = b, a+b
print()


fibonacci(2000)

Output:

0 1 1 2 3 5 8 13 21 34 55 89 144 233 377 610 987 1597 2584 4181 6765 10946 17711 28657 46368 75025



Advertisement Powered By Google Adsense






IF YOU LIKE THIS TUTORIALS PLEASE SHARE

Share on whatsapp

POPULAR TAGS

python python tutorial learn python learning python python code learning java python language python documentation python tutor python basics python download python online python for dummies how to learn python