aımnova
DashboardMy LearningCrash CoursesStudy Plan

Aimnova site navigation

Stay in the loop

Get the latest study resources and updates

New features, study tips and exam insights — straight to your inbox.

IB Diploma

  • IB Past Papers
  • IB Study Notes
  • IB Question Bank
  • IB Mock Exams
  • IB Revision

IB Subjects

  • IB Math AA
  • IB Math AI
  • IB Economics
  • IB Business Management
  • IB Physics
  • IB Biology
  • View all IB subjects→

IB Past Papers

  • IB Math AA HL Past Papers
  • IB Math AA SL Past Papers
  • IB Math AI HL Past Papers
  • IB Math AI SL Past Papers
  • IB Economics HL Past Papers
  • IB Economics SL Past Papers
  • IB ESS Past Papers
  • View all past papers→

Study Resources

  • Study Notes
  • Question Bank
  • Mock Exams
  • Crash Courses
  • Flashcards
  • Revision Guide
  • Exam Skills
  • Command Terms
  • Grade Calculator
  • Exam Timetable 2026

Aimnova

  • Features
  • Pricing
  • For Teachers
  • For Schools
  • For Parents
  • About Us
  • Blog
  • Contact
aımnova

IB study, practice and feedback built around the real exam.

TermsPrivacyCookies·© 2026 Aimnova. All rights reserved.c1e448f

Aimnova is not affiliated with or endorsed by the International Baccalaureate Organization (IB).

NotesComputer Science HLTopic 6.3Functions and modularity
Back to Computer Science HL Topics
6.3.46 min read

Functions and modularity (Computer Science HL)

IB Computer Science · Unit 6

AI-powered feedback

Stop guessing — know where you lost marks

Get instant, examiner-style feedback on every answer. See exactly how to improve and what the markscheme expects.

Try It Free

Contents

  1. 1One job, written once
  2. 2Why split a program up
  3. 3Scope, and getting values in and out
  4. 4Exam-style question
The big idea: A function is a named block of code that does one job, can be given different inputs, and hands back a result.
Why it pays: Write it once, call it anywhere — and fix it in one place when it turns out to be wrong.
def average(values):          # values is a PARAMETER
    if len(values) == 0:
        return 0              # the empty case, handled once
    return sum(values) / len(values)

print(average(maths_marks))   # maths_marks is an ARGUMENT
print(average(science_marks)) # same code, different data
static double average(int[] values) {   // values is a PARAMETER
    if (values.length == 0) {
        return 0;                       // the empty case, handled once
    }
    int total = 0;
    for (int v : values) { total = total + v; }
    return (double) total / values.length;
}

System.out.println(average(mathsMarks));    // mathsMarks is an ARGUMENT
System.out.println(average(scienceMarks));  // same code, different data
Parameter and argument: The parameter is the name in the definition — the placeholder.

The argument is the actual value passed in when it is called. Definition has parameters; calls supply arguments.
Method (function) signature
Its name and its parameter list: how many parameters, in what order, of what types. The return type is not part of it.
Parameter
The variable named in the definition that receives a value: values in average(values).
Argument
The actual value passed in the call: marks in average(marks).
Sub-program as abstraction
You use it by its name and inputs only; how it works stays hidden inside it.

Watch a call happen: the program jumps in, works with its own variables, and comes back with one value.

A function call: argument in, body runs, value returned
A call passes arguments in and gets one value back

Free preview

This is the free notes preview

You're reading the free notes. Aimnova Pro unlocks the full study experience — and you can try it with your first topic free to keep:

  • FlashcardsLock in vocabulary and key terms with spaced repetition.
  • Practice questionsAnswer exam-style questions and get instant AI marking.
  • Mock exams & past-paper vaultSit full mocks and see exactly how examiners award marks.
  • Personalised study planA daily plan built around your exam date and weak areas.
Start Studying Free Full access to Aimnova Pro · cancel anytime

Reusable

  • One definition serves every place that needs the job done
  • No copied blocks that then drift apart
  • A fix applies everywhere at once

Maintainable

  • Each function is small enough to understand completely
  • A fault is traced to one function, not scanned for across a program
  • It can be changed inside without touching anything that calls it

Testable and shareable

  • A function can be tested on its own, with values chosen to break it
  • Work can be split: different people write different functions
  • Both only work because the boundary is clearly defined
The copy-paste argument: Copy a calculation into four places and you have four places to fix when it is wrong — and you will miss one.

Call a function in four places and there is one.

That is the strongest single argument for modularity, and the one to write down.
Modularisation is bigger than functions: It also means grouping related functions into modules or files — all the file handling in one place, all the calculations in another.

Same idea, one level up.

Never wonder what to study next

Get a personalized daily plan based on your exam date, progress, and weak areas. We'll tell you exactly what to review each day.

Try Free Study PlanYour first topic is free to keep • No credit card required
tax_rate = 0.2                 # global — a genuine setting

def with_tax(price):
    amount = price * tax_rate  # local: amount. Reads the global rate
    return price + amount      # return hands the result back

print(with_tax(50))            # 60.0
print(amount)                  # error — amount only existed inside
static double taxRate = 0.2;           // a field — a genuine setting

static double withTax(double price) {
    double amount = price * taxRate;   // local: amount. Reads the shared rate
    return price + amount;             // return hands the result back
}

System.out.println(withTax(50));       // 60.0
System.out.println(amount);            // error — amount only existed inside
1

In through parameters

A function should be told what it needs, rather than reaching out for it.

That way it works anywhere, can be tested on its own, and cannot be broken by something changing elsewhere.

2

Out through return

return sends a value back and ends the function immediately.

A function that changes a global instead is harder to test and harder to reuse — because its effect is invisible from the call.

3

Local stays inside

Variables created in a function disappear when it ends.

That is a feature: two functions can safely use i or total with no risk of interfering.

4

Globals, sparingly

A genuine setting — a tax rate, a file path — is reasonable.

Using them to pass working values between functions is what makes a program hard to follow, because any function might be the one that changed it.

A function with no return gives nothing back: If it only prints, the value cannot be used for anything else.

return the result and print it at the call if needed — a function that returns can be tested; one that only prints cannot.

How this is tested — you must define with parameters, return a value, and say why it is better than repeating the code. It comes up two ways:

Paper 2 — working with code

  • Construct a function for a stated job, 3-5 marks
  • Explain the benefits of modularisation
  • Identify a scope error in given code

Paper 2 — the algorithmic-thinking question

  • Explain why a program should be split up
  • Reason about scope with no code shown
The classic trap: Writing a function that uses a global instead of a parameter, and prints instead of returning. It may work in the one place it was written, and it cannot be reused or tested anywhere else.
IB-style questionConstruct[5 marks]

A program calculates a percentage in four different places. Write a function for it and explain why this is better.

Model answer plan

See the answer broken into clear points, with guidance on how each point earns marks.

Claim your free topic

Try an IB Exam Question — Free AI Feedback

Test yourself on Functions and modularity. Write your answer and get instant AI feedback — just like a real IB examiner.

the output of this Java code.

static void addOne(int n) {
n = n + 1;
}

// in main:
int x = 5;
addOne(x);
System.out.println(x);
[2 marks]

Related Computer Science HL Topics

Continue learning with these related topics from the same unit:

6.1.1Variables and data types6.1.2Substrings6.1.3Exception handling6.1.4Debugging
View all Computer Science HL topics
Improve your exam techniqueCommand terms, paper structure, and mark-scheme tips for Computer Science HL
Previous6.3.3LoopsNextBig O notation6.4.1

25 exam-style questions ready for you

Students who practice on Aimnova improve their scores by 15% on average. Get instant feedback that shows exactly how to improve your answers.

Practice Now — FreeView All Computer Science HL Topics