A function is nothing but a piece of code that does some task. This piece of code can be reused whenever required. Functions are of 2 types : 1. Built in functions 2. User Defined Functions. Built in functions are those that are inbuilt and come as a part of a python library. User defined functions are those that are written by the user.
WHY DO WE NEED FUNCTIONS?
This is the code to print multiplication tables from 1 to 10 for number 1.
Now if I had to write these 2 lines for multiplication tables for numbers 1,2,3,4,5,6,7,8,9 and 10 it would require a total of 20 lines of code.
Now if I were to use a function to print multiplication tables,it would look like this:
Now I can get multiplication tables for any number by just calling this function.
Pretty simple and small right? Now we will learn how to create user defined functions.
Parameters are nothing but values or data required by the function. In the above example, 10 is the parameter for function multiply(10). This parameter is passed to the function multiply(n) and multiply(n) performs a function or task or work on this parameter(get multiplication tables of 10).
We need to know 2 important terms: formal parameters and actual parameters. Formal parameters are the parameters given in the function definition.Actual parameters are the parameters given in the function call.They need not be same. We will see what function definition and function call are. For example, multiply(n) function used above, has n as its formal parameter but numbers like 10,9 etc are the actual parameters.Each actual parameter is assigned to corresponding formal parameter.
- The keyword def introduces a function definition. Function definition means we describe or elaborate the function. 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.
- Write the arguments or parameters inside the opening and closing parentheses of the function. Arguments means the data or values required for the function. End the declaration with a colon( : ).
- Write the statements.
- Close the function with or without returning any variable. A function can be made to return a variable like a number or a string or a list. Using a list ,a function can be made to return multiple values too.
- Function call : Calling a function is nothing but using a function.
- Calling a function requires you to write the function name followed by actual parameters in parentheses in the right order according to the order specified in the function definition. For example, if the function definition has a string and a number then a function call must pass a string and a number in the same order.
Let us see another very simple example now. This will make the concept clear for us.
Here, the name of the function is sum(observe “def”).The parameters that it needs are a and b. They are the formal parameters. The first 2 lines form the function definition. The function just performs the function of returning the sum of a a and b. Later when we control goes to the print statement, the sum function is called(this is a function call) and parameters 3 and 6 are passed to the sum(a,b) function. These are the actual parameters. They get added and the returned sum is printed out. Hence 9 is printed.