Calling Functions in Python is like performing a rehearsed play in soccer

First of all three important things about functions:

  1. A function is a block of code which only runs when it is called.
  2. You can pass data, known as parameters, into a function.
  3. A function can return data as a result.

In other words: A function helps you to approach a challenge you want to solve in a more efficient way with less error potential. Defining the content of a function once, you can easily apply it to different use cases as well and don’t have to write the code over and over again.

In the sense of soccer a function is like a rehearsed play, which can be repeated over and over again in different situations to solve a challenge.

The cool thing is, that there are already existing functions in Python. But before you can use them it is often the case, that you have to import them.

To import the whole library (in our example “math”) you can do this by libraryName.functionName():

You can import only one function from a library, then you can call it by just using its name:

And if you don’t like the default names of the funtions you can just assign your own names to them e.g. the one of your favorite soccer player:

All the examples shown above will return the value of pi. One more thing:

It is important to understand what the “return”-Statement does within the functions and how it can be seperated from the “print”-Statement. The best resource I could find on the internet on this was this video:

#Mastery07

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s