Functions

Functions

A function is a subroutine that takes one or more values from the main program and returns a value back. For example, transferring over a sphere’s radius from the main program for the function to calculate a surface area and then return that value back to the main program. The two key differences between procedures and functions are:

  1. A function uses parameters to transfer data from the main program into the function.
  2. A functions returns a value back to the main program.

When we call a subroutine we need to include the data to transfer to the subroutine as a parameter in brackets. In the procedure example above there were no parameters so the brackets were left blank. But let’s imagine the sphere example; in the main program we want to input the radius of the shape and then call the function to work out the surface area. When we call the function we need to define the radius as a parameter within the brackets:

subroutines8

Now that we have sent the radius value over the subroutine we need to collect that value in the subroutine’s defined parameters. Remember above when we defined a procedure we left the parameters blank? Now we have to state what we are transferring over, it makes sense to call it the same as the original value but you could change the name if you wished. Defining a function could look like this:

subroutines9

Within a function you need to return a value back to the main program. Here we want to calculate the surface area of a sphere based on the radius value that we read in from the main program. To send a value back to the main program you must write return followed by the variable you wish to return. For example:

subroutines10

One last thing! If we want to print the returned value then we need to put the call subroutine line within a print command like so:

subroutines13

Now if we ran this program it would output the following:

subroutines14

Looking good! But there are two ways that we could improve this specific program. Firstly instead of using the int command before the input we could use float instead – float allows decimal numbers to be inputted which is more appropriate for this program. Also, the output line is not very helpful so we can insert a string to the print line and a comma to make it more useful:

subroutines15

And now if we ran this code, the output would be:

subroutines16

The great thing about subroutines is that we can re-use the same code in a program for different jobs. For example the program below uses parameters for moving through a checkpoint system:

subroutines1

When run, the program would output the following:

subroutines2


 Subroutines not working? Tick off these common errors:

  • Make sure that you define your subroutine at the start of your program.
  • You need to write def then the name of the subroutine then any parameters in brackets and then a colon. For example: def Calculation(x,y):
  • You need an indent beneath a subroutine for each line within the subroutine (just like an if statement or loop). Un-indent it back over when you have finished.
  • You need to use parameters correctly. If you are sending a value over to the subroutine make sure that you define what you are transferring within brackets after you call the subroutine. Such as: Sandwiches(fillingtype, “bread”) Also make sure that you have the same number of parameters up in the define line at the start of the program. Such as def Sandwiches(fillingtype, “bread”):
  • Make sure that you use the return command to return a value created in the subroutine over to main program.
  • If you return a value you may want to print it, make sure that you put the call subroutine line within print brackets. Such as: print(Sandwiches(fillingtype, “bread”))   Don’t forget the double brackets. You may also want to make this look better by putting it within a sentence, such as: print(“Your favourite sandwich, the”, (Sandwiches(fillingtype, “bread”), “is now ready to collect”)

    < Back to Section Seven