1. How to Use This Page
1. Solve every exercise on your own first. Attempt each question before reading the solution. 2. Compare your method, not just the final answer. The process matters in the board exam. 3. Pay attention to the explanations — they clarify why an answer is correct, which helps with similar questions. 4. Revisit the "Common Mistakes" section before every test to avoid repeat errors. 5. Use the Exam Checklist on the night before the exam for a quick final review.
2. Key Concepts (Quick Recap)
Parts of a Function
| Part | Description | Example |
|---|---|---|
| Return type | Data type of value returned | int, float, void |
| Function name | Identifier for the function | calculateArea, add |
| Parameters | Input values the function receives | int a, int b |
| Function body | Statements that perform the task | { return a + b; } |
| Return statement | Sends value back to caller | return result; |
Function Syntax
// Function prototype (declaration)
int add(int a, int b);
// Function definition
int add(int a, int b) {
return a + b;
}
// Function call
int result = add(3, 5); // result = 8
Types of Function Calls
| Feature | Call by Value | Call by Reference |
|---|---|---|
| What is passed | Copy of the value | Address of the variable |
| Effect on original | No change | Changes reflect in original |
| Parameter type | Ordinary variable | Reference variable (&) or pointer |
| Memory | Separate copy created | No separate copy |
| Safety | Safer (original protected) | Less safe (original can be modified) |
Call by Value example:
void increment(int x) { // x is a copy
x = x + 1; // only the copy changes
}
int main() {
int a = 5;
increment(a);
cout << a; // Output: 5 (unchanged)
return 0;
}
Call by Reference example:
void increment(int &x) { // x is a reference to a
x = x + 1; // modifies original
}
int main() {
int a = 5;
increment(a);
cout << a; // Output: 6 (changed)
return 0;
}
Function Categories
| Category | Description | Example |
|---|---|---|
| No return, no argument | void func(void) | Just prints something |
| No return, with argument | void func(int a) | Processes input, no return |
| With return, no argument | int func(void) | Reads input, returns value |
| With return, with argument | int func(int a, int b) | Processes input, returns value |
Default Arguments
int add(int a, int b = 10) {
return a + b;
}
// add(5) → 5 + 10 = 15 (b uses default)
// add(5, 20) → 5 + 20 = 25 (b uses passed value)
Rules:
- Default arguments must be specified from right to left.
- A parameter with a default value cannot appear before one without.
Recursion
int factorial(int n) {
if (n == 0 || n == 1) // Base case
return 1;
return n * factorial(n - 1); // Recursive case
}
// factorial(5) = 5 * 4 * 3 * 2 * 1 = 120
- Base case: Stops the recursion (mandatory).
- Recursive case: Function calls itself with a smaller problem.
Scope of Variables
| Scope | Where accessible | Example |
|---|---|---|
| Local | Inside the function where declared | Variables inside main() or other functions |
| Global | Throughout the program, after declaration | Variables declared outside all functions |
3. I. Choose the Best Answer (MCQs)
void return?4. II. Fill in the Blanks
1. A function that calls itself is called a recursive function. 2. The return type specifies the data type of the value a function sends back to the caller. 3. In call by value, a copy of the argument is passed to the function. 4. In call by reference, the address of the argument is passed to the function. 5. The function prototype declares a function before it is used, telling the compiler its name, return type, and parameters. 6. A function that does not return any value has the return type void. 7. The scope of a variable determines where it can be accessed in the program. 8. Default arguments must be specified from right to left. 9. The condition that stops a recursive function is called the base case. 10. When a local variable has the same name as a global variable, the local variable takes precedence. 11. The inline keyword suggests the compiler to insert the function body directly at the call site to reduce overhead. 12. The return statement sends a value back to the calling function. 13. A function that takes no arguments and returns no value is declared as void func(void).
5. III. True or False (with corrections)
1. A function can return more than one value.
False. A function can return only one value using the return statement. To return multiple values, use reference parameters, pointers, or structures.
2. The function prototype must match the function definition in return type and parameter types.
True. The prototype declares the interface; the definition must match it exactly in return type and parameter types. Mismatched types cause compilation errors.
3. In call by value, the original variable is modified.
False. In call by value, only a copy is passed, so changes inside the function do not affect the original variable. Call by reference modifies the original.
4. A recursive function must have a base case.
True. Without a base case, the recursion never stops, leading to infinite recursion and a stack overflow crash.
5. Global variables are accessible only inside main().
False. Global variables are declared outside all functions and are accessible from every function in the program, not just main().
6. Default arguments can be placed on the leftmost parameter if all others also have defaults.
False. Default arguments must always be specified from right to left. A parameter without a default cannot appear after one with a default.
6. IV. Match the Following
| Column A | Column B |
|---|---|
1. void | a. Function calls itself |
| 2. Call by value | b. Address is passed |
| 3. Call by reference | c. Copy is passed |
| 4. Recursion | d. No return value |
| 5. Function prototype | e. Stops recursion |
| 6. Base case | f. Declares function signature |
7. return | g. Sends value back to caller |
7. V. Answer Briefly (2-mark questions)
int factorial(int n) {
if (n == 0 || n == 1) // Base case
return 1;
return n * factorial(n - 1); // Recursive case
}
// factorial(5) = 5 * factorial(4)
// = 5 * 4 * factorial(3)
// = 5 * 4 * 3 * factorial(2)
// = 5 * 4 * 3 * 2 * factorial(1)
// = 5 * 4 * 3 * 2 * 1 = 120
8. VI. Answer in Detail (5-mark questions)
Call by Reference Program:
#include <iostream>
using namespace std;
void swapByReference(int &a, int &b) {
int temp = a;
a = b;
b = temp;
cout << "Inside function: a = " << a << ", b = " << b << endl;
}
int main() {
int x = 10, y = 20;
cout << "Before swap: x = " << x << ", y = " << y << endl;
swapByReference(x, y);
cout << "After swap: x = " << x << ", y = " << y << endl;
return 0;
}
// Output:
// Before swap: x = 10, y = 20
// Inside function: a = 20, b = 10
// After swap: x = 20, y = 10 (originals changed)
9. Common Mistakes to Avoid
1. Forgetting the return type — Every function (except constructors) must have a return type. If it returns nothing, use void.
2. Mismatch between prototype and definition — The return type and parameter types in the prototype must match the definition exactly.
3. Confusing call by value and call by reference — In call by value, changes don't affect the original; in call by reference (using &), they do. Always check whether the parameter has &.
4. Missing base case in recursion — This causes infinite recursion and a stack overflow. Always define when the recursion should stop.
5. Wrong order of default arguments — Default arguments must be from right to left. int add(int a = 5, int b) is invalid; int add(int a, int b = 5) is valid.
6. Not returning a value from a non-void function — If the return type is int, the function must return an int. Forgetting the return statement causes undefined behaviour.
7. Shadowing global variables — A local variable with the same name as a global variable hides the global one inside that function. Use distinct names to avoid confusion.
8. Infinite recursion — Ensure the recursive call moves toward the base case. factorial(n) should call factorial(n-1), not factorial(n).
10. Exam Checklist
- [ ] I can define a function with return type, name, parameters, and body
- [ ] I can write a function prototype and explain its purpose
- [ ] I can differentiate call by value vs call by reference with examples
- [ ] I can write a swap program demonstrating both call types
- [ ] I can classify functions into four categories (with/without argument, with/without return)
- [ ] I can explain default arguments and their right-to-left rule
- [ ] I can write recursive functions (factorial, Fibonacci)
- [ ] I can identify the base case and recursive case in any recursion
- [ ] I can explain the advantages and disadvantages of recursion
- [ ] I can differentiate local and global scope with examples
