How to Call a Function in Python

Summary

To call a function in Python you simply have to write it’s name followed by parentheses like this.

You can also call your function with arguments like this:

You can call as many arguments required by your defined function.

In-Depth

When you call a function, you’re just telling Python that you have a block of code somewhere that you want to execute. This function can be defined by you or stored somewhere in a library.

For example, to get the current date and time you can use the function now() stored in the datetime library.

You can see that the function now() doesn’t require any arguments to run, although it can accept some like a timezone.

Let’s look at another custom function that I will define:

This function represents your salary after n years of experience.

You’ll receive an error if you try to call the function without providing the 2 arguments, base and yearsOfExperience:

Let’s now try to input an initial salary of $40,000 and 5 years of experience:

Congrats! After 5 years you’re now making $48,666 per year. What about after 25 years?

I bet you’re starting to be excited with a salary of $106,633 after 25 years of experience.

You have to remember that to call a function in Python you need the name of the function followed by “()” and to provide arguments when required.

Get The Job You Want – Free Content

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.