Subroutines & Structured Programming Flashcards

All 16 cards in this deck

What are the steps to complete a missing line in a given subroutine?
e.g. a subroutine addBonus(score) that must add 10 to the score and return the new score

  1. Use the parameters already given, with no extra inputs: use score
  2. Write the missing statement: score ← score + 10
  3. Return the updated value: RETURN score

Give two advantages of the structured approach (splitting a program into subroutines).

Any two of: each subroutine can be written and tested on its own; a team can share the work; subroutines can be reused; the program is easier to understand and maintain.

What is the difference between a function and a procedure?

A function is a subroutine that returns a value to the calling routine; a procedure is a subroutine that carries out a task but returns no value.

How is a subroutine that takes a parameter and returns a value written in AQA pseudo-code?

e.g. calculatePrice, giving the cost of tickets at £5 each

  1. Define it with a parameter: SUBROUTINE calculatePrice(tickets)
  2. Calculate using the parameter: price ← tickets * 5
  3. Return the value and close: RETURN price then ENDSUBROUTINE

How do you call a subroutine and keep the value it returns?

e.g. use calculatePrice for 3 tickets and store the result in totalPrice

totalPrice ← calculatePrice(3). The parameter goes in the brackets and the returned value is assigned with ←.

How is a subroutine that returns no value (a procedure) written and called in AQA pseudo-code?

e.g. a subroutine greet that outputs a welcome for a name

  1. Define it with any parameters: SUBROUTINE greet(name)
  2. Write the task, then close it: OUTPUT 'Welcome ' + name … ENDSUBROUTINE
  3. Call it by name: greet('Sam')

What is a local variable?

A variable declared inside a subroutine. It usually only exists while the subroutine is executing and can only be accessed within that subroutine.

True or false? A variable declared inside a subroutine can still be used in the main program after the subroutine ends.

False. It is a local variable, so it only exists while the subroutine is executing.

How do you pass an array into a subroutine?

e.g. pass the array scores into a subroutine printAll

Use the array's identifier as the parameter in the call: printAll(scores). Inside the subroutine, the parameter is used like an array.

Give two advantages of using subroutines in a program.

Any two, e.g.: the code is written once and called whenever it is needed instead of being repeated, so the program is shorter; a change or fix only has to be made in one place; a subroutine can be reused in other programs.

How does the structured approach make a program easier to maintain?

The program is split into subroutines, each doing one task through a clear interface (parameters and return values), so each part can be understood, tested and updated without changing the rest.

What is a subroutine?

A named 'out of line' block of code that is executed (called) by writing its name in a program statement. Functions and procedures are both subroutines.

What happens when a subroutine is called?

The program runs the subroutine's code, then returns to the point where the subroutine was called and carries on from there, using any value the subroutine returned.

What is a parameter?

e.g. the 3 in calculatePrice(3)

A value passed into a subroutine when it is called, so the subroutine can use data from the calling routine. AQA uses 'parameter' for both the value passed in and the variable that receives it.

Why is it good practice to use local variables in subroutines?

Any two of: changes inside the subroutine can't accidentally affect variables elsewhere; the same name can be reused in other subroutines; the subroutine is self-contained, so it is easier to test and reuse; its memory is freed when it ends.

What is the structured approach to programming?

Writing a program as modules (subroutines), each doing one task, with clear, well-documented interfaces: parameters pass data in, return values pass data out, and local variables are used inside.