User-defined Functions





Hello Folks....!!!!
Today let us look at how to write the functions in python.

Functions 

The functions are code blocks that are executed by calling them. The functions can be either parameterized or non-parameterized. The syntax for defining a function is given below. The keyword that specifies that the block below is a function is def.
        def function_name (args) :
                    statement_1
                    statement_2
                        .
                        .
                        .
                    statement_n
                    return

The blocks need not be wrapped with opening and closing braces. Instead they have to be maintained in appropriate indentation level like we did when writing loops. 

Non-parameterized functions 

These functions do not take up any arguments. The list items supplied to the functions are called arguments or parameters. These can be used to do a specific task repeatedly instead placing the piece of code on the program every time. For example look at the code below.

functions in python

Here line is a function which prints a line of '*'. Instead of writing the print("*******")  every time. It can be written once and called whenever required. This does not require any argument to be passed.

Parameterized functions

These functions require arguments to be passed for processing them and returning the value. A function can take n number of arguments. While passing the values, the order of the arguments matter.

parameterized functions in python

In the code snippet given above, the values passed are 7,8 and 9. They are considered for a,b,c respectively. If you wish to change the order of arguments then you have to specify the argument variable for which you are passing the value. For example :

functions in python

Thus one has to mention the argument explicitly if the order of arguments will be shuffled while calling. Any kind of instructions can be placed inside function. Statements for  scanning inputs, printing outputs, any arithmetic processing can be placed inside the functions.

A simple example for parameterized function : 

parameterized functions

Here you can find the word return

'return' keyword

It is the keyword for passing the resultant to the place from where the function was called. The return statement has to return a single value. But it can be made to return multiple values by placing the resultants in a list or tuple. For example:

return keyword in python

In the above program I have combined the addition with subtraction and multiplication with division. Now the function should return the result of both the operations. I have used a list here to return multiple values. When printed they are printed as list elements. But if you want them to be separate and not in a list, then declare n variables where n is number of return values from the function and call the function like given below.


The returned values will be stored in the variables in the order returned. Now you can use them separately as required.


Next Topic๐Ÿ‘‰

Comments