Functions in PythonSimple Python Function

Most of the programming languages like python allow you to break down long, continuous programs into a series of subprograms also called modules, which continue to be related to each other. Such programs are called functions in Python. If a program contains hundreds of statements, then it became necessary to break it down into smaller modules for ease of understanding. Each module acts as a separate program itself and perform specific task. The act of partitioning a program into individual components is called `Modularity’.

Every function has a name and contains a set of statements. In order to execute a function, it must be invoked by the main program or by some other function. Every function can be invoked by specifying the name of the function. This process is known as calling a function and statement that does this is known as a function call statement. The following example.

Example 1:

def JNV(): # function header

        print(“Jawahar Navodaya Vidyalaya, Chandigarh”) #function body

JNV() # function call statement

It is possible that a function might not return a value, as say JNV( ) was not returning a value. Say JNV( ) prints a message on screen and does not contain a return statement, such functions are called void functions also called non-fruitful functions.

More about return statement: – In function we call void function like R=JNV() instead of JNV() then python does not give error. Python will return legal empty value None to its caller. See the example given below we call JNV() and R=JNV() in both ways

Simple Python function

in example given above when we call JNV() it is printing message “Jawahar Navodaya Vidyalaya, Chandigarh” but when we call R=JNV() ,it is printing message “Jawahar Navodaya Vidyalaya, Chandigarh” and return None to variable R.

Let’s consider another example

Example 2:

def square ( x ): # formal parameter

      return x * x

a = 10

r = square( a ) # actual parameter

print(r)

Here function name square takes x as parameter and returns square of that number         i.e. x * x. Here x is formal parameter and a is actual parameter. Parameter present in function definition are called formal parameters and parameters present in function calling are called actual parameter

The last statement of the function, i.e. return statement returns a value from the function. Return statement may contain a constant/literal, variable, expression or function, if return is used without anything, it will return None. In our example value of a variable x is returned. Function which returns the values are called Non-void functions also called fruitful functions.

Remember unlike other languages like C/C++ ,in Python function can return multiple values to its caller . see the code given below.

Python Function returns multiple values (1)
Output of above code is

(25, 100, 400)

in the above code square of x ,y and z are returns to r. Python take r as tuple automatically. You can call it another way by storing square of x,y and z in separate variables p,q and r . See the example given below.

Python function returns multiple values (2)
Output is

25 100 400

In next Page their are few questions for you. You can run your python code on this page also to check your answers.

4 thoughts on “Functions in Python”

Leave a Reply

Your email address will not be published. Required fields are marked *