Sub-Programs

CT03e

  • Create functions
  • Create procedures
  • Use 'separation of concerns’

Subprograms, what’s the point?

CT03e

Over the last couple of lessons, you’ve learned how to write subprograms in code.

These are useful tools in a programmer’s toolbox and it is best practice to use them whenever you can..

In fact, in industrial code, subprograms make up the vast majority of the code that exists.

But why?

Subprograms, what’s the point?

CT03e

The biggest reason is modularity or ‘separation of concerns’.

Each subprogram has one small job to do.

For example, user input is not done in the same function as calculations.

This makes the development and maintenance of large programs much simpler.

Flowchart Example

CT03e

We need an algorithm that will generate a username.

For user Robert Jones who joined in June 2019 the username would be:

Each subprogram would be defined in its own flowchart.

RobJon0619

Flowchart Example

CT03e

Each of these boxes is a subprogram symbol.

You use this symbol when you want to call a subprogram.

Activity 1

CT03e

A program is needed to take a user’s input and determine if it is even or odd. When the number is odd, a further test is required to see if it is a prime number. In each case a different output is required. The program ends by displaying ‘Goodbye’.
The algorithm can be expressed as a flowchart. The symbols for this are all jumbled up below.
Arrange the symbols to express this algorithm, which uses subprograms. Use each symbol only once. Use as many arrows and Yes/No labels as required.
Extension: Colour the procedures green and the functions yellow.

Activity 1

CT03e

Working as a team

CT03e

Software development is rarely a solo job.

Most software projects have large numbers of programmers working on them. Different programmers will work at different times and on different parts of a project.

This is where ‘separation of concerns’ becomes so useful.

In a computer game for example, you might have a team devoted to the physics engine, and other teams working on the 3D rendering, story, sound design, art, marketing, etc.

The point at which each part of a program interacts with another part is known as an interface.

Remember that subprograms are black boxes – what happens inside them is not visible to the rest of the program.

Work as a team: group project

CT03e

You are going to do some software development as a team.

You need to develop a program that can generate a random mix of playing cards and perform some other operations with those cards.

The program will require are at least 4 functions.

Each person in a team will write one function, then the four functions will be inserted into a higher-level program to bring them together.

You will need to use functionality from the Python random library. 

Try: random.choice(<list>) or random.randint(0, len(<list>)) 

Working as a team...

CT03e

Get into teams of 4. Each person in the team should be responsible for producing one subprogram only.

Once each subprogram has been developed, put them together in a single program to see if they work together.

Hint: You can add a line to the very top of the main program to directly call whichever subprogram you’re working on. You don’t need the other subprograms to be working to write and debug your own. That’s what team working is all about.

Working as a team...

CT03e

Here is a sample console session from a complete program:

['Hearts', 'Clubs', 'Hearts', 'Diamonds', 'Clubs'] 
[1, 3, 9, 12, 5]

----- Current Hand ----- 
1 of Hearts 
3 of Clubs 
9 of Hearts 
12 of Diamonds 
5 of Clubs 

Total value in hand is 30 

Face value counts: [1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 1, 0] 
Hearts: 2 
Clubs: 2 
Diamonds: 1 
Spades: 0 

Goodbye

Review

CT03e

Create functions and procedures.

  • You’ve learned how to do both of these in code and in a flowchart.
  • Remember that a function returns a value and a procedure does not.

Use 'separation of concerns’.

  • Decomposing a large problem down into smaller parts means that more than one person can work on its solution at the same time. 
  • Subprograms are black boxes, so these parts of a program do not interfere with each other.

ct03e Sub-programs

By David James

ct03e Sub-programs

  • 392