Linux

How to call a function in Python

How to call a function in Python

In the last post that introduced Python, I showed how to make a simple application using variables and conditional statements. To do something that is very powerful in certain programming languages, you must understand function! In this post, we will discuss calling Python functions.

What is a Python function call?

Before we look at how to call functions in Python, we first need to familiarize ourselves with the concept.

Functions are used throughout programming as a way to group certain tasks together. This becomes useful in a variety of circumstances, especially when repetitive tasks need to be done several times.

Functions are used throughout programming as a way to group certain tasks together.

For example, if you create an application that draws hundreds of triangles on the screen to produce a kaleidoscopic effect, you can do it in one of two ways:

  • Without function: by repeatedly writing code to draw a triangle.
  • With a Python function call: by generating lots of coordinates and feeding them to your “draw triangle” function.

The latter is far more efficient, requires less code, and is generally the preferred method. Not only that, but if you ever decide you want to draw a box instead of a triangle; You can change just a few lines of code and the entire output will be different!

One more benefit of using functions is modular and portable functions. If you write another program with triangles in it, you can copy and paste your triangle code wholesale!

Example of a python call function

Here is a very simple example of a Python function that will print “Hello World!” to screen:

python
def HelloPrint():

    print("Hello World!")

    return;

HelloPrint()

That’s how to define a function in Python and call it!

The function here is called HelloPrint. First we “define” this function with the def statement, then we put whatever code we want to be part of it directly below it. The return statement only instructs the interpreter to return to any point in the code before carrying out its function.

Note that I have used every word in my function name. This is good practice because it helps differentiate the function of Python calls from statements.

Now, anytime we want to say “Hello World!” we just write HelloPrint () and it will!

As an example:

python
def HelloPrint():

    print("Hello World!")

    return;

HelloPrint()

HelloPrint()

Run this code and now you will see “Hello World!” message appears twice!

Because this code is grouped separately, this code will not run until you use the Python function call. That also means that this code will do the exact same thing:

python
def HelloPrint():

    print("Hello World!")

    return;

HelloPrint()

HelloPrint()

This also means that you should be able to know how to call a function from another function:

python
def GreetingsPrint():

    print("Hello World!")

    NiceDayToday()

    return;

def NiceDayToday():

    print(“Nice day today, isn’t it!”)

    return;

GreetingsPrint()

And that, in short, is how to call functions in Python! But we still haven’t exploited the real power of Python functions!

How to forward information to a Python function call

While the function is useful for performing repetitive tasks, the real strength lies in the ability to give and receive data. This is what the little brackets are for: they allow us to call functions in Python while also sending data.

For example, the following code will say “Hello Adam”:

python
def SayHello(Name):

    print(“Hello ” + Name)

    return;

SayHello(“Adam”)

This is the meaning that the same function can perform slightly different actions depending on the variable we provide.

How to manipulate data

Even more useful is the ability of a function to transform data.

To do this, we need to forward information into the function, take action, and then return the information.

Here’s one way we do this with Python functional calls:

python
def Multiplier(Number):

    return = Number * 10;

print(Multiplier(5))

Here, the output will be “50” because the number 5 is passed with a Python function call, which returns that value multiplied by 10. Notice how we can write the Python function call as if it were the integer’s name itself. This allows for very fast and flexible coding!

There are many ways we can use this feature. Here is another small example that only requires three lines of code:

python
def Counter(Name):

    return len(Name);

NamePlease = input("Name length counter! Enter your full name ")

print(Counter(NamePlease))

This little application is “name length counters.” This uses the len statement from Python, which returns an integer based on the length of the string. So, this fun application can tell you how many characters in your name!

That includes spaces but hey, nothing is perfect.

Now you know how to use Python function calls! This opens up a world of possibilities, but don’t stop there!

Related posts

How to Run a Cron Job Every 5, 10 or 15 Minutes

Linux

How to Install and Use PHP Composer on Ubuntu 20.04

Linux

How to Install and Use TeamViewer on Linux

Linux

How to Install Drupal 9 with Nginx and Let’s Encrypt SSL on Debian 10

Linux

How to Install and Use a Curl on Ubuntu 20.04

Linux

How to Install Python on a Mac

Apple

How to Manage Nginx Server Blocks on Ubuntu 20.04

Linux

How to compile the ONLYOFFICE Document Server from source code on Ubuntu

Linux

How to Install an Atom Editor on Ubuntu 20.04

Linux