Python 7a - Procedures

Subroutines

A subroutine is a section of code that can be re-used several times in the same program.

 

It is separate from the main code and has to be ‘called’ upon.

 

Subroutines are designed to be repeated and they have three key benefits:

  1. Subroutines make programs easier to read.

  2. They reduce the duplication of code.

  3. Complex problems are broken down into smaller chunks.

There are two types of subroutines, procedures and functions.

 

  • A procedure just executes commands, such as printing something a certain number of times.

  • A function produces information by receiving data from the main program and returning a value back to the main program. For example, a function could take the radius of a sphere from the main program and then calculate a sphere’s area and return the value of the area back to the main program. A function generally requires parameters to work – these are the values to be transferred from the main program to the subroutine.

Procedures

A procedure is a subroutine that just executes commands. It is separate to the main program and does not transfer any data back. For example, if you perform a calculation in the main program you cannot work with those same values over in the procedure, and the other way round.

A subroutine, whether it is a procedure or a function, needs to be defined at the start of the program, before it is called within the main program.

 

To define a subroutine we start with the word def (which stands for define) and then write the subroutine name, closed brackets (which are the parameters – but in a procedure these are empty) and a colon (remember this stands for ‘then’). For example:

You must remember to indent everything that you want contained within the subroutine, beneath it (just like an if, for or while statement). For example, a procedure for printing a song chorus is written out below:

Subroutines must be written at the top of the program.

Now that you have defined your subroutine you need to call it in the main program.

 

You do this by writing the name of the subroutine and then the parameters in brackets (parameters are any variables or data that you wish to send over to the subroutine – but procedures don’t use or return data so the brackets will be left blank).

 

You can call a subroutine as many times as you like in a program.

 

To call a procedure in the main program:

An example of a procedure, in the form of a song, is shown below:

It is a good idea to use comments in your code, this is so that you can identify to yourself or others who read it, what certain sections are doing.

 

To insert a comment use the # symbol. Comments are not displayed when the program is run. Adding comments to the previous program would make it clearer to spot what it happening:

Practice Task

1. Create a procedure that prints “Hello there!” ten times.

 

In the main program create a for loop that calls the procedure 10 times.

 

You must use a procedure.

 

2. Create a procedure that prints “Everyday I’m shuffling!” forever.

 

In the main program create a while loop that never ends, forever calling the procedure and waiting 2 seconds. Remember to import sleep from the time library.

Example solution for #1:

© CSNewbs 2019