Python Basic – What is the Concept of Function in Python

Code Reusability

By writing a program or instruction for a computer, we can do a task over and over again with the computer. That is all we know. One can write a program for a job one by one. For example, if we have to print 10 lines on the screen, someone will write a program and use 10 print statements in it. Again good programmers will use loops for this work. In the previous section, we have seen the use of for loop, while loop, etc. loops. Specific instructions can be executed more than once using the loop. It’s a kind of code reuse. This means that it is possible to do the same thing using a print statement and a little logic without using the print statement 10 times.

There are two terms used in any type of computer programming. WET and DRY. WET means Write Everything Twice or We Enjoy Writing. This means writing or using unnecessary and additional statements in any program where the same task could be done in a much more optimized way by writing much less code. It’s a bad habit. Again DRY means – Do not Repeat Yourself. That means not using the print statement 10 times to print that line 10 times. Since the work is the same (printing something), it is possible to print 10 times in one way or another by writing a print statement once. It’s a good habit.

The best example of reusing such code is the application of the function. But we have already used the function in the previous sections. As much as there is so much talk about print, this is also a function. It is a built-in function. In other words, we did not code the function of this function ourselves. The function of this function is – if any of the strings are sent, it prints that string to the standard output. 

That is, we do not write a bunch of computer-understandable code every time to print something. Just call the print function and give him what needs to be printed. Thus we are repeatedly using the code generated for the print function. This is the reuse of code.

In the next few sections, we will look at the reuse of code by writing functions for our various tasks.

Function

With the help of functions, we can use the parts of our program that come up again and again as reusable units. As we have seen in mathematics, a function takes an input and gives different types of math output to it, the same thing happens in programming. You will pass one or more parameters in a function, process the function and you will “return” the output. However, in the case of programming, there is no need to always have input or output.
A function is actually a compilation of some statements. Statements inside this function are executed whenever a function is called. In Python, we use the def keyword to declare a function. Let’s look at a function:

   # Defining the function named greetings
   def greetings():
       print("Hello EnableGeeks!")


First, we typed the def keyword. Then I gave the function a name – greetings, and then a pair of first brackets ( ). Then we define the function of the code block or function under this function with a colon or: sign. The function of the function is “Hello EnableGeeks!” Print the sentence.

But if you just define a function in a program like this, the code or instructions in it will not work automatically. For this, you have to call that function by its name. How to make a call? Nothing, just write the name of the function in the form of a statement. For example – we use the print function when needed. The above function should be called as below,

Example:

   # Defining the function named greetings
   def greetings():
       print("Hello EnableGeeks!")
   # Calling the function to use it
   greetings()

Output:

   Hello EnableGeeks!

Once a function is defined in the program, it can be called or used again and again. I.e. as follows –

Example:

   # Defining the function named greetings
   def greetings():
       print("Hello EnableGeeks!")
   # Calling the function to use it
   greetings()
   greetings()
   greetings()
   greetings()

Output:

   Hello EnableGeeks!
   Hello EnableGeeks!
   Hello EnableGeeks!
   Hello EnableGeeks!

It is important to remember one thing. Before you can call or use a function, you must define that function in the program or mark it next to the program. For example – if we write the above program as below,

Example:

   # Calling the function to use it
   greetings()
   # Defining the function named greetings
   def greetings():
       print("Hello EnableGeeks!")

Output:

Traceback(most recent call last):
   File "/home/tuhin/Desktop/Problem-Solving/Python/python_basic_14.py", 
   line 3, in <module >
   greetings()
NameError: name 'greetings' is not defined

It would seem logical to think in a simple way. That is, if I want to call or use a function at the beginning of the program, how can my program know that it has a function called greetings? Isn’t it logical to define the function in the program first and then call it in one or more places in the program?

Custom functions can be compared to a small machine built by you. That means I will make the machine once and use it, again and again, to do something or make things. If you want to make something, you may have to give some input to the machine. How to make it again, you have to set up the equipment in the machine. Creating a machine in this way is like defining a function in a program.

Function Return

So far we have understood what a function is (machine), why or how to use a function (reuse of code) etc. Remember, we compared the function to a machine? That is, a function may have some function in its body and it may be silently done the work or it may give some return. Who will return? That will call or use this function. Where to return? The return value will be reached from where the call will be made. We can see the following example clearly –

def greetings():
   return "Hello EnableGeeks!"
 
returned_value = greetings()
print(returned_value)

Something is returned from the function using the return keyword in the function. Calling the greetings function in the example above returns a string. And so when it is called down by the = greetings () statement, in fact, the returned value of that function is deposited on the right side of the = sign. And since we know that a value is assigned to a variable with the = sign, then that returned value is stored in the returned_value variable.

A very important point is that when a function has a return statement and it is executed, then no code in the function is executed. That is, the function stops at the end of its work by returning something. Such as –

Example:

   def square():
       nums = [1,2,3,4,5,6]
       nums_squar = []
       for it in nums:
           nums_squar.append(it**2)
       return nums_squar
       print("EnableGeek is Awesome!")
 
   print(square())

Output:

   [1, 4, 9, 16, 25, 36]

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.