levo

Problem Solving and Programming

Programming techniques: modularity, functions and procedures, and parameter passing

  • 3 things to know
  • Component 02

Overview

Recall questions on modularity and its benefits, functions and procedures, what a caller needs to know, reusable subroutines, side effects, passing parameters by value and by reference with traces, returning two results, and parameters versus arguments.

What you need to know

Answer each one before you reveal it.

Your recallNot started
Key term

What is modularity, and give three real benefits of it.

Key term

What does passing a parameter by reference actually do?

Evaluation

What is the difference between a parameter and an argument?

Questions from this subtopic

Each one with the model answer underneath.

  1. 1

    What is modularity, and give three real benefits of it.

    Model answer

    Modularity means splitting a program into separate self-contained sections — modules or subroutines — each with one clear job and a defined interface. Benefits: a large task can be divided among a team who work on different modules simultaneously; each module can be tested and debugged on its own, so a fault is localised to one small piece instead of hunted for across the whole program; modules can be reused elsewhere in this program and in other projects; and maintenance is easier, because a module's internals can be rewritten without affecting anything else so long as its interface stays the same.

  2. 2

    What is the difference between a function and a procedure?

    Model answer

    A function returns a value to the point at which it was called, so a call to it forms part of an expression: total = total + triple(7). A procedure carries out a task and returns no value, so a call to it stands alone as a statement: greeting("Hamish"). Both may take parameters and both help modularity. In good practice a function just computes and returns an answer without changing anything else, while a procedure exists for its effect — printing, updating a file, redrawing the screen.

  3. 3

    What must a programmer know about a subroutine in order to call it, and why is that the whole point of modularity?

    Model answer

    Its name; what parameters it expects — how many, in what order, of what type, and whether each is passed by value or by reference; and what comes back, either a returned value for a function or a described effect for a procedure. That set of facts is its interface. Nothing else is needed: the caller does not need to know how it works inside. That is the point. As long as the interface is unchanged, the inside can be completely rewritten — swapping in a faster sorting method, say — and every piece of code that calls it continues to work untouched.

  4. 4

    What makes a subroutine easy to reuse in another program?

    Model answer

    It does one clearly defined job rather than several loosely related ones. It receives everything it needs through its parameters instead of reading global variables. It returns its result rather than writing it to a fixed place. It does not depend on anything peculiar to one program — a particular file name, a particular screen layout, a particular data structure defined elsewhere. And its interface is documented, so a new caller knows what to supply and what to expect. A subroutine that reads three globals and prints straight to one part of one screen cannot be lifted anywhere.

  5. 5

    What is a side effect, and why does it make a program harder to debug?

    Model answer

    A side effect is any change a subroutine makes beyond returning its value: altering a global variable, changing a parameter that was passed by reference, writing to a file, changing the display. It makes debugging harder because the result of a call then depends on more than its arguments, so the same call can behave differently at different times, and because a fault can surface in code far away from the subroutine that actually caused it, separating the symptom from the cause. A function with no side effects can be understood completely from its inputs and its returned value, which is why it is easy to test.

  6. 6

    What does passing a parameter by value actually do?

    Model answer

    A copy of the argument's value is made and placed into the subroutine's parameter, which is a separate local variable living in the subroutine's own stack frame. The subroutine works entirely on that copy. Anything it does to the parameter — assigning to it, incrementing it, swapping it — changes only the copy, which is destroyed when the subroutine returns. The caller's original variable is therefore unchanged after the call, whatever the subroutine did.

Other ways to work this

The same content, four other ways.

What is inside Levo

Six ways to work the same material.

1Closed book
2Open book
3Closed book

See a huge jump in your memorisation

Answer cold, learn it, then prove it stuck. Levo tracks the jump between your first attempt and your last.

Processors: Structure, Performance and Types
Input, Output and Storage
Systems Software
Applications Generation

Cram a whole paper in one sitting

Sort every topic into what you know and what you do not, then drill only the weak ones.

If it matches, the search .
succeedsfailsrestarts

Blank Blitz consolidates the facts

Rapid timed gap-fills on the terms and numbers you need to recall without thinking.

A0 + 0 gives 0.
B0 + 1 gives 1.
C1 + 1 gives 0 carry 1.

Build It trains answer structure

Assemble a full-mark answer from its marking points, in the order an examiner wants them.

A global variable means a variable reachable from anywhere in the program.
TrueFalse

Test It catches misconceptions

Spot the fault in a statement. The traps are the mistakes students actually make.

721941163128510

Brain Breaks between sessions

Short focus games to reset attention, so the next set lands better than the last.

Every question in Problem Solving and Programming, on a schedule.

Attempt a question from memory, write the correct version, then attempt it again. Levo records the jump between your first attempt and your last, and brings each question back on the day you are due to forget it.

Or £46 a year, about £3.83 a month. Cancel anytime.