Difficulty: Introductory | Prerequisites: Chapters 1 to 5 (variables, data types, control structures, loops).
Chapter 6 is where C++ stops being a single block of code in main() and starts looking like a real programme. Functions let you break logic into named, reusable pieces, each with a clear job, inputs and outputs. This is the foundation for every larger topic that follows: classes, file I/O, and data structures all assume you can define and call functions fluently. If you are coming in cold, this chapter is the bridge between writing scripts and writing software.
Functions are named blocks of code you define once and call whenever you need them. Each function has a return type, a name, and zero or more parameters; data flows in through arguments (by value or by reference) and out through a return value. Mastering definitions vs. prototypes, local vs. global scope, and pass-by-value vs. pass-by-reference covers roughly 80% of what this chapter tests.
Modular programming
Breaking a programme into a set of manageable, self-contained functions. In simple terms, this means splitting your code so each piece does one job.
Function definition
The complete code of a function: its header plus the body (the statements between the braces). Think of it as the full recipe, not just the name of the dish.
Function prototype (declaration)
A statement that tells the compiler a function's return type, name and parameter types before the function is defined. In simple terms, it is a promise that the function exists somewhere in the code. Parameter names are optional in a prototype.
Function call
A statement that causes a function to execute. You supply the function name and the arguments (the actual values). No data types are written in the call itself.
Function header (heading)
The first line of a function definition. It includes the return type, the function name, and the parameter list with both types and names.
Parameter (formal parameter)
A special variable declared in a function's header that receives a value when the function is called. Think of it as a labelled slot waiting for data.
Argument (actual argument)
The value or variable you pass into a function call. The argument fills the parameter. Order matters: the first argument maps to the first parameter, and so on.
Return type
The data type of the value a function sends back to the caller. A function may return zero or one value.
void function
A function whose return type is void, meaning it returns no value. It still runs code and can take parameters; it simply has nothing to hand back.
Local variable
A variable defined inside a function body. It is created when the function runs and destroyed when it ends. Not accessible outside that function.
Global variable
A variable declared outside all functions. Every function in the file can see and modify it. Good practice is to limit their use because they make bugs harder to trace.
Static local variable
A local variable declared with the static keyword. Its value is retained between function calls rather than being destroyed each time the function ends. Think of it as a local variable with a memory.
Pass by value
The default in C++. A copy of the argument is given to the function. Changes inside the function do not affect the original variable.
Pass by reference
The function receives a reference (an alias) to the original variable, declared with & in the parameter list. Changes inside the function do affect the original.
Default argument
A value automatically passed to a parameter when the caller omits that argument. Default values are assigned in the prototype or the header, from right to left.
Function overloading
Defining two or more functions with the same name but different parameter lists. The compiler picks the right version based on the arguments in the call.
return statement
Ends a function and sends a value (if any) back to the point where the function was called.
exit() function
Terminates the entire programme immediately, regardless of which function is running. It does not simply return to main().
Stub
A dummy function used during testing. It stands in for a function that has not been written yet, confirming that the call and return mechanism works.
Driver
A programme module whose job is to test other modules by calling them with known inputs and checking their outputs.
A function definition contains the header and the body (the statements between { and }). The header states the return type, the function name, and the parameter list with both data types and identifier names.
A function prototype tells the compiler about a function before its full definition appears. It requires the return type, the function name, and the data type of each parameter. Parameter names are optional.
The prototype eliminates the need to place the function definition above every call site.
A function call executes the function. You supply only the function name and the arguments (values or variables). You do not restate data types in the call.
A function other than main() runs only when it is called, not when it is defined and not when the compiler reads its prototype.
Functions support modular programming: each function handles one logical task, making programmes easier to read, test and maintain.
Functions are especially useful in menu-driven programmes, where each menu choice maps to a separate function.
An argument is the data you send to a function. A parameter is the variable in the function header that receives it.
A function can have zero to many parameters, but at most one return value.
Order matters. The first argument is copied into the first parameter, the second into the second, and so on. Swapping the order gives wrong results even if the count is correct.
The default mechanism in C++. The function receives a copy of the argument.
Changes to the parameter inside the function have no effect on the caller's variable.
Use pass by value when the function only needs to read the data.
Declared with & in the parameter list, e.g. void swap(int &a, int &b).
The function works directly on the caller's variable, so changes persist after the function returns.
Use pass by reference when the function needs to modify the original argument, or when you want to avoid copying a large object.
A default argument is a value supplied in the function prototype (or definition) that is used automatically when the caller omits that argument.
Default values must be assigned from right to left in the parameter list. You cannot skip a parameter in the middle.
A function may mix parameters with and without defaults.
Defined inside a function body.
Created when the function starts, destroyed when it ends.
Not accessible from any other function.
Declared outside all functions, typically at the top of the file.
Visible to every function in that file.
Convenient, but risky: any function can change the value, making bugs hard to track. Good practice is to use them sparingly.
In C++, global numeric variables are initialised to zero by default. Local numeric variables are not, so they contain garbage until you assign a value.
Declared with the static keyword inside a function.
Scope is local (only that function can see the name), but lifetime is the entire programme.
The value is retained between calls. The variable is initialised only the first time the function runs, not on every call.
Useful for counters, caches, or any state that should survive across calls without being global.
The return statement ends a function and sends a value back to the caller. Control resumes at the point where the function was called.
A function can return at most one value. If the return type is bool, it must return true or false.
A void function returns no value. It can still accept parameters and do useful work; the word void only means there is nothing to hand back.
If a non-void function reaches the closing brace without a return, the behaviour is undefined. If a void function reaches the closing brace, control simply returns to the caller (it does not jump to the next function in the file).
The exit() function (from ) terminates the entire programme immediately, regardless of which function is currently executing. It does not merely return to main().
Covered in the Parameters section above. The key exam point: defaults are assigned right to left, and a function can mix defaulted and non-defaulted parameters.
Two or more functions may share the same name provided their parameter lists differ (different number or types of parameters).
The compiler resolves which version to call based on the arguments supplied.
Different return types alone are not enough to distinguish overloaded functions. The parameter lists must differ.
Overloading is useful when the same logical operation applies to different data types or different numbers of inputs (e.g. an area() function for circles and rectangles).
A stub is a dummy function that stands in for a real function during development. It typically prints a message or returns a fixed value, confirming the call and return work correctly. Use stubs when you are building top-down: write main() first, then replace stubs with real functions one at a time.
A driver is a small programme (or function) whose sole purpose is to test another function by calling it with known inputs and checking the outputs. Use drivers when you are building bottom-up: write the worker functions first, then test them before wiring them into the main programme.
Students often think a function runs when the compiler reads its prototype. It does not. A prototype only informs the compiler; execution happens at the call.
Students often think void means the function does nothing useful or has no parameters. It only means the function returns no value.
Students often think local variables in C++ are automatically initialised to zero. They are not. Only global variables get zero-initialised by default.
Students often think exit() returns control to main(). It terminates the entire programme on the spot.
Students often think two overloaded functions can differ only in return type. They cannot; the parameter lists must differ.
⚠️ Know the difference between a function definition, prototype and call, and what each one requires you to supply (types, names, return type).
⚠️ Be able to distinguish pass by value from pass by reference and state when to use each.
⚠️ Expect a question on static local variables and what makes them different from ordinary local variables.
⚠️ The argument vs. parameter distinction is a favourite fill-in-the-blank: an argument is passed in, a parameter receives it.
⚠️ Function overloading requires different parameter lists, not just different return types.
⚠️ exit() terminates the programme; return exits only the current function.
True or false: A function prototype requires parameter names. (False, only types are required.)
Fill in the blank: A ________ variable retains its value between function calls. (static local)
True or false: Argument order does not matter as long as the count matches. (False, order matters.)
Fill in the blank: The ________ function terminates the entire programme immediately. (exit())
True or false: Two overloaded functions may differ only in return type. (False, parameter lists must differ.)
Q: What is modular programming, and why do we use it?
A: Modular programming means breaking a programme into self-contained functions, each handling one task. It improves readability, makes testing easier, and allows code reuse.
Q: What three things must a function header include?
A: The return type, the function name, and the parameter list (with both data types and identifier names for each parameter).
Q: What is the difference between a function prototype and a function definition?
A: A prototype declares the function's return type, name and parameter types so the compiler knows the function exists. A definition includes the full body of the function. Prototypes do not require parameter names; definitions do.
Q: Explain the difference between an argument and a parameter.
A: An argument is the actual value or variable passed in a function call. A parameter is the variable in the function header that receives and holds that value.
Q: When should you use pass by reference instead of pass by value?
A: Use pass by reference when the function needs to modify the caller's original variable, or when copying a large object would be expensive. Use pass by value when the function only needs to read the data.
Q: What happens if a void function reaches the closing brace without a return statement?
A: Control returns normally to the caller. This is fine for void functions. (For non-void functions, reaching the closing brace without returning a value is undefined behaviour.)
Q: How does a static local variable differ from a regular local variable?
A: A static local variable is initialised once and retains its value between function calls. A regular local variable is created fresh each time the function runs and destroyed when it ends.
Q: What is the difference between a stub and a driver?
A: A stub is a placeholder function used to test the calling code (top-down testing). A driver is a module that calls another function with known inputs to test it (bottom-up testing).
Q: Can two overloaded functions have the same parameter list but different return types?
A: No. Overloaded functions must have different parameter lists. Different return types alone do not distinguish them.
Q: What does the exit() function do, and how does it differ from return?
A: exit() terminates the entire programme immediately, from wherever it is called. return ends only the current function and sends control back to the caller.
This connects to arrays (Chapter 7) because you will pass arrays to functions by reference (arrays decay to pointers, so the function always works on the original data). It connects to classes and objects (Chapters 7 to 9) because member functions are simply functions that belong to a class, with the same rules for parameters, return types and overloading. Understanding scope here also prepares you for the distinction between member variables and local variables inside methods.
C++ functions, modular programming, function definition, function prototype, function declaration, function call, function header, parameter, formal parameter, argument, actual argument, pass by value, pass by reference, reference parameter, void function, return statement, return type, local variable, global variable, static local variable, default argument, function overloading, overloaded function, exit(), stub, driver, scope, lifetime, Gaddis Chapter 6, Starting Out with C++ Early Objects, CSE Arizona State University