All programming languages have the concept of function. A function is used to do some specific tasks. It holds a group of related statements that need to be performed in the code. It will run when the function name will be called in the program. We can pass any value to the function, called parameter. It saves time by not writing the same code again and again. Hence, it avoids repetition. It breaks the code into smaller parts and makes the code more manageable and organized.
The function can be two types: library function and user-defined function. In Python, the example of a library function is print(). Returning value is the main feature of a user-defined function. For creating a user-defined function, we have to focus on various points. All the points will be discussed in this tutorial.
def function(): #write your code here #return result function() # function call
We have to write the def keyword for defining a function. def is a library keyword in Python. After def, we have to write our function name. It can be any name because it is a user-defined function. After the function name, we must give open and end parenthesis (()) followed by a colon(:) then press enter. An example is given below,
def function(): print("This is my First Function Code")
After defining the function, we have to call the function name as per our requirement. When we call the function, it will execute and return the result. If we will not call a function, then it will be a waste of memory. To call a function, we just have to call the function name along with the opening and closing parenthesis. An example is given below,
def function(): print("This is my First Function Code") function() # This is my First Function Code
We can use the library statement return to return the result of a function. An example is given below,
def re_func(a): return 10 * a print(re_func(2)) print(re_func(7)) print(re_func(10)) # Output: # 20 # 70 # 100
We can pass all types of collection objects in the function like list, tuple, dictionary, etc. By using a loop, we can print all the items as well. An example is given below,
def Cfunc(items): for i in items: print(i) col = [10,20,30,40] Cfunc(col) # print all the items
The pass keyword is used to skip or avoid the statement/function. It will not give any error.
def func(): pass
A function recall itself is known as recursion. Every programming language has the same concept. It has a benefit that until we reach out to the result, the function will call itself by any loop. It is a very useful concept in programming, but the programmer has to be careful when writing this block or function. Because depending on the mathematical and logical concept, this system will work and may have the change to fall in an infinite loop. It will be easy to understand by using an example. Look carefully the below code
def recursion(val): if val <= 1: return val else: return(recursion(val-1) + recursion(val-2)) limit = 7 print("Fibonacci Series are:") for r in range(limit): print(recursion(r)) # Output: # Fibonacci Series are: # 0 1 1 2 3 5 8
The above code prints the Fibonacci series up to 7. The output of the code is 0 1 1 2 3 5 8. We have done this code by using the concept of recursion. Until the limit reaches, the loop will execute. In the loop, the function is called. In the function, it is called itself. When the value is less than zero, it will return the value. For more than one, it will call the function again and do the necessary operation and return the result.