Skip to main content

How to call a function in Python

Python function call

In the last post introducing Python, I demonstrated how to make a simple app using variables and conditional statements. In order to do anything really powerful in a given programming language though, you need to understand functions! In this post, we’ll discuss the Python function call.

What is a Python function call?

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

Also read: Best online Python courses

Functions are used throughout programming as a way to group certain tasks together. This becomes useful in a variety of circumstances, particularly when a repetitive task needs to be carried out multiple times.

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

For example, if you built an app that drew hundreds of triangles on the screen to generate a kaleidoscopic effect, you could do this in one of two ways:

  • Without functions: by repeatedly writing the code to draw a triangle with.
  • 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 squares instead of triangles; you could change just a few lines of code and the entire output would be different!

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

Python call function example

Here is an extremely simple example of a Python function that will print “Hello World!” onto the screen:

def HelloPrint():

    print("Hello World!")

    return;

HelloPrint()

That is 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 place any code we want to be a part of it directly beneath. The return statement simply instructs the interpreter to return to whatever point in the code it was at before it carried out the function.

Note that I’ve capitalized each word in my function name. This is a good practice as it helps to distinguish a Python function call from statements.

Now, any time we want to say “Hello World!” we can simple write HelloPrint() and it will happen!

For example:

def HelloPrint():

    print("Hello World!")

    return;

HelloPrint()

HelloPrint()

Run this code and you’ll now see the “Hello World!” message appear twice!

Because this code is grouped separately, it won’t run until you use the Python function call. That also means that this code will do the precise same thing:

def HelloPrint():

    print("Hello World!")

    return;

HelloPrint()

HelloPrint()

This also means you should be able to figure out how to call a function from another function:

def GreetingsPrint():

    print("Hello World!")

    NiceDayToday()

    return;

def NiceDayToday():

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

    return;

GreetingsPrint()

And that, in a nutshell, is how to call a function in Python! But we still haven’t tapped into the real power of Python functions yet!

How to pass information to a Python function call

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

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

def SayHello(Name):

    print(“Hello ” + Name)

    return;

SayHello(“Adam”)

This is means that the same function can perform slightly different actions depending on the variables we give it.

How to manipulate data

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

To do this, we need to pass information into the function, perform an action, and then return that information.

Here’s one way that we might perform this with a Python functional call:

def Multiplier(Number):

    return = Number * 10;

print(Multiplier(5))

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

There are countless ways we can use this feature. Here is another little example that requires just three lines of code:

def Counter(Name):

    return len(Name);

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

print(Counter(NamePlease))

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

That’s including spaces but hey, no one is perfect.

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



source https://www.androidauthority.com/python-function-call-1121793/

Comments

Popular posts from this blog

5 tips to Voice Speech Recognition in Android Marshmallow

Android Marshmallow landed on our Android devices. The opportunity for us to offer a small compilation of tricks to try immediately. The latest version of the Google OS starts (very gently, unhurriedly) to be offered on other devices as Nexus. You will find on Android TM, in the coming weeks, a compilation of the best tips for Android 6.0 Marshmallow. It starts slowly with a video featuring the 10 essential tips & tricks of this new version of the little green robot OS. To continue, we offer a selection of five "tricks" on the management of the battery on Android 6.0 Marshmallow. To enjoy longer your newly updated mobile. Follow the guide. then continue with 5 tips to tame the super-assistant Google Now on Tap. Here you will find 5 "tips" to manage in the best way your applications. We then discuss the quick tips to navigate more easily on this version of the Google OS. We enchanters with features focused on safety and the protection of personal data. We co...

Energy Android TV Play turns your TV into a Smart TV

ENERGY SISTEM Android Energy TV Play, you have a smart TV with Android operating system allows you to convert any traditional TV has announced the launch of a new product. Energy Android TV Play can be connected to the TV to enjoy f the size of a flash drive, a smart phone, a tablet and a computer unconsolidated is a lightweight device. System 1.6 GHz, DDR3 1GB of RAM and a dual-core processor can be expanded using external USB devices, which is the internal memory of 8 GB. It also integrates WiFi and a USB port for connecting external devices. One of its outstanding features, it is easily connected to the TV screen by screen cast application to display the contents of any terminal, making any phone or tablet is synchronized with iOS or Android. All ENERGY SISTEM products one click In addition, through streaming media service applications, images, video or other multimedia content, and game play is the ability to share. With integrated WiFi, the device you want from t...

How to run Python apps on any platform

Credit: Adam Sinicki / Android Authority Want to know how to run Python? It sounds simple, but it can actually be tricky to figure this out. In this post, we’ll discuss how to test your Python code, as well as how to run Python in other contexts: online for example, or as a packaged app. Sometimes, the thing holding you back from learning to code can be extremely simple. I remember wanting to learn to program when I was younger – or learning to take what I’d learned from BASIC on the ZX Spectrum and apply that to a modern environment. My problem? I didn’t know “where” to program. Once I understood C# or Java, where would I enter the code and how would I run it? And whenever I asked someone, they would look at me blankly. What kind of a question is that? Thing is, I had never needed an IDE or an interpreter before. Machines like the ZX Spectrum and Tatung Einstein (any other Einstein users out there?) simply booted up with a prompt to code into! Many people have a similar iss...