Page 1 of 1

Functions in python

#1 atik97  Icon User is offline

  • ???
  • member icon

Reputation: 143
  • View blog
  • Posts: 715
  • Joined: 16-September 08

Posted 16 December 2008 - 03:33 AM

Function

Function is a block of statements. A name is given to the statement block and this block of statements are executed when the function is called. Simply functions are reusable pieces of programs.

In python, functions are defined using the def keyword, which is followed by an identifier name. This followed by a pair of parentheses which may enclose some variables and finally the line ends with a colon. This is followed by statement block which form the body of the function. Take a look at the following example-

#defines a function
def sayHi():
	#body of the function
	print "Hello World!"

#call the defined function
sayHi()



Above is the example of a simple function. First we define a function named sayHi(). The function contains only only one executable statement. When we call the function, the body of the function will be executed i.e. "Hello World!" statement will be printed to the user screen.


Function Parameters

A function can take parameters. Parameters are nothing but some values supplied to the function so that the function can do something by utilizing those values.Parameters are similar to variables with only difference that their values are supplied when we call the function.

Parameters are specified within the pair of parentheses in the function definition and separated by commas. The values supplied in the function call are called arguments. Here is how to use parameters...

#This function takes two parameters a & b
def printMax(a,b):
	if a>b:
		print a,"is maximum"
	else:
		print b,"is maximum"

#two arguments are given at the time of function call
printMax(5,2)



The output of the program will be-
5 is maximum


Recursion of Function

It is allowed for a function to call another function. It is also legal for a function to call itself. See how can be it turn out a magical thing!

def countdown(n):
	if n<=0:
		print "Blastoff!"
	else:
		print n
		countdown(n-1)

countdown(5)



The output of the program will be-
5
4
3
2
1
Blastoff!


Default Argument Value

For some functions, you may want to make some parameters as optional and
use default values. This is done with the help of default argument values. You can specify default argument values for parameters by following the parameter name in the function definition with the assignment operator (=) followed by the default argument.

def sayHello(s,times=1):
	print s*times

sayHello("Hello")
sayHello("World ",5)



The output of the above program will be-
Hello
World World World World World

This is because at the 1st call of the function we don't have specified the value of 2nd argument and by default it is 1. So the word "Hello" is printed just for a single time. On the other hand , when we call the function 2nd time we specified both the arguments.


The return statement

The return statement is used to return from a function. We can optionally return a value from the function.

def printMax(a,b):
	if a>b:
		return a
	else:
		return b




Scope of the variables(Local & Global)

Local variables
A function declared inside a function definition is not related in any way to the other variables with the same name outside the function. This type of variables are called local variables.

def value(x):
	print "Local x is",x
	x=5
	print "Changed value of x is",x

x=2
value(x)
print "x is still",x



Output of the program is-
Local x is 2
Changed value of x is 5
x is still 2


Global variable

If you want to assign to a variable defined outside the function, then you have to use the global statement. This is used to declare that the variable is global. It is impossible to assign to a variable defined outside a function without the global statement.

def value():
	global x
	print "x is",x
	x=3
	print "Changed x is",x

x=5
value()
print "The value of x is",x



The output of the above code will be-
x is 5
Changed x is 3
The value of x is 3

Just look at the difference. At the 1st example x was local while at the 2nd example x is global. Thus the scope of variable plays an important role.

Is This A Good Question/Topic? 0
  • +

Page 1 of 1