Python Functions

Python functions
A function is simply a block of code that runs only at the time when it is called.

You can pass the data to the functions in the form of parameters.

A function may return the data as a result.

Creating a Function
In python programming language you can define the function with the help of def keyword. For better understanding consider the following example:

def my_function():

     print("Hello from a function")

my_function()


Parameters
You can pass the information to the functions in the form of parameter.

Parameters are passed to the function inside the parantheses after the name of the function. There is no limitation on the number of parameters i.e. you can pass as much parameters as you want. The only condition that is to be satisfied is that the all the parameters should be separaed by comma.

In the following example the function "my_function" has only on eparameter i.e. fname. When this function is called anywhere in the program, then we pass the fname at that time when the function is called to print the full name.

def my_function(fname):

     print(fname + " Refsnes")

my_function("Emil")

my_function("Tobias")

my_function("Linus")


Default Parameter Value
If we call any function without any parameter then it will use the default parameter(if given). So, this is the working of the default parameter.

The following example demonstrate the use of the default parameter that how we can use the default parameter in any function.

def my_function(country = "Norway"):

     print("I am from " + country)

my_function("Sweden")

my_function("India")

my_function()

my_function("Brazil")


Passing a List as a Parameter
The parameter of any type can be sent to the function like string, number, list, dictionary etc. amd it will remain same inside the function.

For e.g. if list type of parameter is passed to a function then when it will reach to the function then it will stiil remain a list. Foe better understanding consider the following example:

def my_function(food):

     for x in food:

         print(x)

fruits = ["apple", "banana", "cherry"]

my_function(fruits)


Return Values
If we want a function to return a value then we must use the return statement for that. For better understanding consider the following example:

def my_function(x):

     return 5 * x

print(my_function(3))

print(my_function(5))

print(my_function(9))


Recursion
The concept of recursion is also accepted in the python programming language i.e. the defined function can call itself.

Recursion is a concept that is common in both mathematical and programmin. Recursion means that the function can call itself. The benefit of this is thaat you can loop through the data to reach to a result.

We should be very careful while dealing with the concept of recursion as it may happen that if the recursive program is not written properly then it may happen that the program may result into an infinite loop that means that it will never terminate or it may also happen that the program may use more memory or the speed of the processor. However, if you write a recursive program in correct way then it will be very efficient and mathematical elegant approach to programming.

In the example below, the function tri_recursion() is defined that will call itself. We have used k variables as the data, that will decrement every time the recusion is performed. The process of recursion will end when the condition arise that it is not graeter than 0 or we can say that that the process of recursion ends when it is 0.

If anyone is a new developer then it may take sometime for him to understand that how exaxtly it will work and a better way for this is that he should regularly test and modify the program.

Recursion Example

def tri_recursion(k):

     if(k>0):

         result = k+tri_recursion(k-1)

         print(result)

else:

     result = 0

return result

print("\n\nRecursion Example Results")

tri_recursion(6)