Code Labs: Subprogrammes

Master Procedures, Functions, Parameters, and Returning Data.

Interactive Engine

Use the interactive terminal below to test Functions and Parameters.

> Waiting to compile...

The Crucible

Prove your knowledge of creating and calling subprogrammes.

Bronze (Procedure calls)

The Initiator

Defining a procedure does absolutely nothing until you Call it.

Task: In the main code section below the definition, call the greet_user() procedure and pass your own name as a string parameter. For example, greet_user("Alan").

> Output will appear here...
Silver (Parameters)

Missing Data

The function calculate_area is meant to multiply a width and a height, but the programmer forgot to define the parameters in the brackets! They also only passed one number when calling it.

Task: 1) Put width, height inside the function definition brackets. 2) Provide a second number when calling the function.

> Output will appear here...
Gold (Return Values)

The Return Command

A true Function does not print things itself! It performs its job invisibly and uses the return keyword to hand the calculation data back to the main programme.

Task: Change the print(discounted) command inside the function to use the return keyword instead. E.g., return discounted. The main programme will handle the printing.

> Output will appear here...