# PART -3:
Guys today were going to learn how to define a function and to call a function . By now u must have got a hang of things in python , so this our next step towards intense programming . I expect that u guys have read the previous tutorial and have practiced and experimented with the compiler .
Why functions are important let suppose there is a process that u have to repeat a number of times during a single execution , so writing the code again and again will increase our code size . So to deal with this problem we write the code of the repeated process and assign it a name so whenever this function has to be repeated u just have to write the name that u have assigned to the process .
Lets start !!!
Defining a Function
The keyword “ def “ introduces a function definition. It must be followed by the function name and the parenthesized list of formal parameters. The statements that form the body of the function start at the next line, and must be indented.
SYNTAX : def function_name(parameter1 , parameter2 ):
The execution of a function introduces a new symbol table used for the local variables of the function. More precisely, all variable assignments in a function store the value in the local symbol table; whereas variable references first look in the local symbol table, then in the local symbol tables of enclosing functions, then in the global symbol table, and finally in the table of built-in names. Thus, global variables cannot be directly assigned a value within a function (unless named in a global statement), although they may be referenced.
When a function is called a new symbol table is created and the actual para meters are stored over there, thus arguments are passed by value .Passed by value means that a new entity has been created that stores the value and any change to this entity will not be reflected on the original entity.
If u require to send back the Fibonacci series rather than printing it, you can use the “ return “ command to return a list of number’s .
SYNTAX : return name_of_variable
This is same as in any other programming language but the difference is that you don’t have to declare any return type ,hence making it easier for the programmer.
Default Arguments Values
When your function has parameters that require at least a standard value ,this the way by which you implement it . Suppose in the incoming arguments for a function doesn’t contain a value for a argument and the function won’t work without that value. In this if the default argument is initialized , then the argument would automatically take the value of the default argument .
SYNTAX : def function_name(parameter1 =’ whatever ‘, parameter2 =20):
Functions can also be called using keyword arguments of the form “ keyword = value “. For example a function accepts four parameter but you wish to pass any two argumenets, then this is the way to do it :
All the above shown way of calling a function are correct and now some the function call that would give an error:
An argument list must have a positional arguments followed by any keyword arguments, where the keyword/keywords must be chosen from the formal parameter names. It’s not important whether a formal parameter has a default value or not. No argument may receive a value more than once — formal parameter names corresponding to positional arguments cannot be used as keywords in the same calls.
Special type of Formal Parameters
When a final formal parameter of the form “ **name ” is present, it receives a dictionary (meaning of dictionary :“A dictionary in python is a collection of unordered values which are accessed by key.”) containing all keyword arguments except for those corresponding to a formal parameter.
This form of formal parameter “*name ”can be combined with “ **name ”, as formal parameters .This form of ” *name ” receives a tuple (meaning of tuple : “A tuple consists of a number of values separated by commas”) containing the positional arguments beyond the formal parameter list. (*name must occur before **name).
So with this we end this tutorial and in the next tutorial we’ll be learning about data structure and how to use them.