Ryan the Data Guy
Published in

Ryan the Data Guy

Functions

A collection of statement collected and executed within what is called a function to be used sometime in the program. Functions always follow this format:

From above def means to define a function or begin the process of creating one.

When selecting a function name, you follow the same rules as when selecting a variable name.

Parameters are basically variables that you’re able to use within the scope of the function and are assigned whatever value or expression when you first called it.

The statement is any piece of code that you want to run.

Return passes back to you whatever is typed after and exits the function.

So to be frank, that won’t make a lot of sense to anyone who hasn’t written python so I’ll write some examples.

The formula for determining perimeter, area and volume of a rectangular shapes.

perimeter = 2 * (length + width)

area = (length * width)²

volume = length * width * height

First, write the first line.

Now, we want the parameters which end up becoming are variables within the function.

Next, second line will use the variables length and width.

As you can see we are assigning the result of length * width to a new variable called perimeter which is going to be returned to you once return is executed.

This is how it’ll look.

To write the functions for area and volume, just follow the same format.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ryan Velasco

I love gaming especially old-school jRPGs, I love them to this day. Now I’ve come to love automating everything and placing things into databases. Super fun.