A 'Function' in Python is a chunk of Code that is used to conduct a particular task. And that chunk is only executed when it is called.
Say if you want to add two numbers, you can have an add 'Function' that would be dedicated to add the numbers.
Similarly, if you want to divide two numbers, you can have a divide 'Function' that would divide the numbers.
So, instead of placing all the codes in a single place. You can distribute the work among different 'Functions'. So, that your code looks more structured and clean.
Below are the rules to create a Function :
def myfunction(myarg): print("This is my first function")
And thats how we define a 'Function'.
But it was mentioned in the 'Function' description that a 'Function' only executes only when it is called.
So, how a 'Function' is called?
Let us see in the below example.
def myfunction(): print("This is my first function") myfunction()
So, in the above code, there are two parts,
def myfunction(): print("This is my first function")
myfunction()
So, in the above code we have defined a 'Function', 'myfunction( )'. You can give any name to the 'Function'(In this case we have given the name 'myfunction( )'). Just don't forget to write the 'def' keyword before it.
def myfunction(): print("This is my first function")
And the work of the 'Function', 'myfunction( )' is, just to print 'This is my first function'.
Now, just remember one thing, the above 'Function', 'myfunction( )' will never execute until it is called.
And thus we have called the 'Function', 'myfunction( )' in the next line.
myfunction()
And the 'Function', 'myfunction( )' is called, printing,
Although, the 'Function', 'myfunction( )' is defined in the first line in the code.
def myfunction(): print("This is my first function")
But it will never be executed. Python will ignore the the above 'Function' definition.
And come to the next line, where it finds the 'Function', 'myfunction( )' is called.
myfunction()
And only then the Function executes.
Now, let us look at another example of adding two numbers and returning the result using 'Functions'.
def add(first_number, second_number): result = first_number + second_number return result first_num = 5 second_num = 4 value = add(first_num, second_num) print("The added result is ",value)
Let us explain the above example with a practical scenario.
Say you are given a task of adding two numbers. And you would follow the below steps to achieve it :
def add(first_number, second_number):
result = first_number + second_number
return result;
So far, we have seen how a 'Function' works. Now, let us see who is the caller?
When the program execution begins, at first Python comes to the 5th line (As Python ignores the 'Function' unless it is called).
And we initialise the first variable 'first_num' with the value '5'.
Then in the next line, we have initialised the second variable 'second_num' with the value '4'.
Then we call the 'add(first_num, second_num)' Function.
And Python searches for a Function with two arguments.
When it finds the Function, it assigns the value of 'first_num' to 'first_number' and 'second_num' to 'second_number'.
And gets into the block of the 'def add(first_number, second_number):' Function.
This is where the numbers are added,
And the result is stored in a variable 'result'.
And in the next line we 'return' the added value(i.e. 'result').
And the returned value goes back to the line, where the Function was called.
And the variable 'value' gets the added result(i.e. '9') from the variable 'result'.
And in the next line the added value is printed as output.
'Nested Function' is a 'Function' inside a 'Function'.
Let us see 'Nested Function' in the below example.
def outer_func(): def inner_func(): print("We are inside the inner function") inner_func() outer_func()
So, in the above code, we have defined two functions 'outer_func( )' and 'inner_func( )'.Since, a 'Function' only executes only when it is called. The control comes to the fifth line directly, where the 'outer_func( )' is called.
And the control goes to the 'outer_func( )' for execution.
def outer_func(): def inner_func(): print("We are inside the inner function") inner_func()
And the control finds a Function named 'inner_func( )' defined inside the 'outer_func( )'.
def inner_func(): print("We are inside the inner function")
Now, the Function 'inner_func( )' doesn't gets executed but goes to the next line, where the call to 'inner_func( )' is made.
inner_func()
And then the Function 'inner_func( )' executes.
def inner_func(): print("We are inside the inner function")
Printing the below output.