How to Use Function in Python

Posted on

Introduction

After defining function in the previous article. In this article, the focus of the content is going to focus further to the usage of the function in Python programming language. Just refer to the article with the title of ‘Function in Python’ in this link. So, in that previous article, after knowing what is a function and also how to define or to create it, this article will just focus for using it in the source code implementation.

Actually, there are many types of function available in Python programming language. Depends on the format of the function, there are three types of function available in Python programming language. Those are an empty function, a function without any return value and the last one is a function with a return value. So, this article will discuss every single of different format of functions.

How to Use Function in Python

In this part, the content will be focusing on the characteristics of the function in Python programming language. So, below are the characteristics of functions in Python programming language :

  1. Empty Function.

    This kind of function is just a simple function without any implementation yet. Basically, it does not have any statements describing any specific processes for further execution. So, this kind of function is just a function created without having a clue for any implementation of the functions yet. Generally, it is just utilizing pass statements before defining the actual statements for the real processes.

    def myfunction():
        pass;
    

    Using the above pattern template as an example for showing how to use it, below is the execution of the above script in a Python command console :

    Microsoft Windows [Version 10.0.22000.856]
    (c) Microsoft Corporation. All rights reserved.
    C:\>python
    Python 3.10.5 (tags/v3.10.5:f377153, Jun 6 2022, 16:14:13) [MSC v.1929 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> def myfunction():
    ... pass;
    ...
    >>> myfunction();
    >>>
    
  2. Returning Value Function.

    As it exist as part of the name. This function is a function which is returning value when the function is being executed.

    def myfunction():
        x = value;
        statement;
        return x;

    With the same context as in the empty function, the following is an execution for the returning value function using the above pattern template as follows :

    >>> def myfunction():
    ... x = 1;
    ... print("This function is returning a value");
    ... return x;
    ...
    >>> y = myfunction();
    This function is returning a value
    >>> print(y);
    1
    >>>
    

    As exist in the above function execution, it actually returns a value. The above execution where the returning value is passed to a variable with the name of ‘y’. In order to prove it, in the above command execution has a statement for printing that variable. It is obvious since it is displaying number 1 as it have been assigned previously in the function.

  3. Non-Returning Value Function.

    Similar with the returning value function, as it exist in part of the name, it is a function which is running without returning any value.
    Indeed, it is when the function is being executed.

    def myfunction():
        statement_1;
        statement_2;
        statement_3;
    

    Finally, another example using the above template pattern which is for the non-returning value function as follows :

    >>> def myfunction():
    ... print("This function is not returning any value");
    ...
    >>> myfunction();
    This function is not returning any value
    >>>

So, the above description are for every types of available function in Python programming language. When to use which type of the function ?. Actually, it depends on the situation or the condition. In other words, it depends on the requirement or need of the process execution.

Leave a Reply