Formula Builder3

ADVANCED FORMULA BUILDING USING FUNCTIONS

One of the things coding languages do is to allow you to create code that you can use over and over again.

In Python these are called functions. They always start with def.

You can feed data into a function and get data out again by returning it.

You feed it in inside the brackets and get it out by returning it.

You can even feed in more than one piece of data but you can only return one thing.

Have a look at this example

The function is called perisquare and all the code indented after it is part of the function (lines 1, 2 & 3)

perisquare is placed in the computers memory but is not run until later.

The program starts with the user inputting a number into the variable oneside on line 4

On line 6 the function perisquare is run with the variable oneside being fed into it (inside the brackets)

In line 2 inside the function oneside is multiplied by 4

In line 3 oneside is returned back (green arrow) and goes inside the variable finalanswer on line 6

Finally finalanswer is reported to the user inside a print command on line 7

Try creating a program that calculates the diameter of a circle from the user inputted radius by building a function.