Creating functions in Python

In the last post you saw how to call functions. But how to create them?

It’s pretty straightforward you simply have to tell the compiler the name of the function and the parameters. It looks like this for a simple multiplication function:

The name is “mult_function” and the parameters are “a,b”. We tell the compiler to multiply them.

Keep one thing in mind: You can’t have two functions with the same name. Think of it like a rehearsed play which is given a name which the coach screams when it is time to play it. Wouldn’t it be super confusing to take the same names for different plays? Nobody knew what to do then. Neither would Python do (= no function overloading), but it solves this dilemma by only using the latest defined function.

You can do a lot with functions. For example you can calculate Fahrenheit to Celcius as I did it here:

#Mastery08

Leave a comment

Design a site like this with WordPress.com
Get started