Current Affairs updated daily
KalviGavaniKalviGavani

Functions Exercise Solutions | KalviGavani

TN Class 11 Computer Science Functions exercise solutions — definition, prototype, call by value/reference, recursion with worked examples.

Updated 15 June 202616 min read

Exercise Solutions Available

Step-by-step answers for all textbook questions in this chapter

View Solutions →

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

PartDescriptionExample
Return typeData type of value returnedint, float, void
Function nameIdentifier for the functioncalculateArea, add
ParametersInput values the function receivesint a, int b
Function bodyStatements that perform the task{ return a + b; }
Return statementSends value back to callerreturn 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

FeatureCall by ValueCall by Reference
What is passedCopy of the valueAddress of the variable
Effect on originalNo changeChanges reflect in original
Parameter typeOrdinary variableReference variable (&) or pointer
MemorySeparate copy createdNo separate copy
SafetySafer (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

CategoryDescriptionExample
No return, no argumentvoid func(void)Just prints something
No return, with argumentvoid func(int a)Processes input, no return
With return, no argumentint func(void)Reads input, returns value
With return, with argumentint 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

ScopeWhere accessibleExample
LocalInside the function where declaredVariables inside main() or other functions
GlobalThroughout the program, after declarationVariables declared outside all functions

3. I. Choose the Best Answer (MCQs)

1. What does a function with return type void return?
Answer

(a) 0 (b) 1 (c) Nothing (d) NULL

Answer(c) Nothing void means "no value." A function with void return type performs a task but does not return any value to the caller.
2. In call by reference, changes made inside the function:
Answer

(a) Do not affect the original variable (b) Affect the original variable (c) Create a new copy (d) Cause a compilation error

Answer(b) Affect the original variable In call by reference, the function receives the address of the original variable, so any modification inside the function directly changes the original value.
3. Which of the following is a valid function prototype?
Answer

(a) int add(int, int); (b) int add(int a, int b) (c) add(int a, int b); (d) int add(int a, b);

Answer(a) int add(int, int); A prototype must specify the return type and parameter types. Option (b) is missing the semicolon. Option (c) is missing the return type. Option (d) is missing the type of parameter b.
4. What happens if a recursive function has no base case?
Answer

(a) Returns 0 (b) Returns 1 (c) Infinite recursion (stack overflow) (d) Compilation error

Answer(c) Infinite recursion (stack overflow) Without a base case, the function calls itself endlessly, consuming stack memory until it overflows and the program crashes.
5. Default arguments must be specified from:
Answer

(a) Left to right (b) Right to left (c) Any order (d) Centre outward

Answer(b) Right to left C++ requires default arguments to be specified from right to left. You cannot have a default for a parameter unless all parameters to its right also have defaults.
6. A variable declared outside all functions is called a:
Answer

(a) Local variable (b) Global variable (c) Static variable (d) Auto variable

Answer(b) Global variable Global variables are declared outside all functions and are accessible from any function in the program. Local variables are declared inside a function and accessible only within that function.

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 AColumn B
1. voida. Function calls itself
2. Call by valueb. Address is passed
3. Call by referencec. Copy is passed
4. Recursiond. No return value
5. Function prototypee. Stops recursion
6. Base casef. Declares function signature
7. returng. Sends value back to caller
Answer1-d, 2-c, 3-b, 4-a, 5-f, 6-e, 7-g

7. V. Answer Briefly (2-mark questions)

Q1. Differentiate between call by value and call by reference.
Answer
FeatureCall by ValueCall by Reference
What is passedCopy of the valueAddress (reference) of the variable
Effect on originalNo effectOriginal is modified
Syntaxvoid func(int x)void func(int &x)
MemorySeparate copy createdNo separate copy
SafetySafer (original protected)Less safe (original can change)
Q2. What is a function prototype? Why is it needed?
Answer

A function prototype is a declaration that tells the compiler the function's name, return type, and parameter types before the function is actually defined. It is needed so the compiler can verify that function calls are correct (matching number and types of arguments) even if the definition appears later in the program or in another file.

int add(int, int);   // prototype
int main() {
    cout << add(3, 5);  // compiler knows add takes two ints
    return 0;
}
int add(int a, int b) { return a + b; }  // definition
Q3. What are default arguments? Give an example.
Answer

Default arguments are values assigned to parameters in the function declaration. If the caller does not provide an argument for that parameter, the default value is used.

float area(float r, float pi = 3.14) {
    return pi * r * r;
}
// area(5)     → 3.14 * 25 = 78.5
// area(5, 3)   → 3 * 25 = 75

Default arguments must be specified from right to left.

Q4. Write a recursive function to find the factorial of a number.
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
Q5. What is the difference between local and global variables?
Answer
FeatureLocal VariableGlobal Variable
Declared insideA function/blockOutside all functions
Accessible inOnly that function/blockEntire program
LifetimeCreated on entry, destroyed on exitExists for entire program duration
Initial valueGarbage (if uninitialized)Zero (if uninitialized)
Preferred forEncapsulation, safetyShared data (use sparingly)

8. VI. Answer in Detail (5-mark questions)

Q1. Explain the different categories of functions based on arguments and return values, with examples for each.
Answer

Functions are classified into four categories based on whether they take arguments and return values:

Category 1 — No return, no argument: The function neither receives arguments nor returns a value. It performs a self-contained task.

void greet() {
    cout << "Welcome to Tamil Nadu!";
}
int main() {
    greet();  // Output: Welcome to Tamil Nadu!
    return 0;
}

Category 2 — No return, with argument: The function receives arguments but does not return a value. It processes the input and may display results directly.

void printSquare(int n) {
    cout << "Square = " << n * n;
}
int main() {
    printSquare(5);  // Output: Square = 25
    return 0;
}

Category 3 — With return, no argument: The function does not receive arguments but returns a value. It typically reads input inside the function.

int getInput() {
    int num;
    cout << "Enter a number: ";
    cin >> num;
    return num;
}
int main() {
    int val = getInput();
    cout << "You entered: " << val;
    return 0;
}

Category 4 — With return, with argument: The function receives arguments and returns a value. This is the most common and flexible form.

int add(int a, int b) {
    return a + b;
}
int main() {
    int result = add(3, 5);
    cout << "Sum = " << result;  // Output: Sum = 8
    return 0;
}
Q2. Explain call by value and call by reference with C++ programs. Illustrate how the original variable is affected in each case.
Answer

Call by Value Program:

#include <iostream>
using namespace std;
void swapByValue(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;
    swapByValue(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 = 10, y = 20  (originals unchanged)
ExplanationIn call by value, copies of x and y are passed. The swap happens only on the copies. The original variables remain unchanged.

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)
ExplanationIn call by reference, a and b are aliases for x and y. Swapping a and b directly swaps x and y.
Q3. Explain recursion with an example. What are the advantages and disadvantages of recursion?
Answer

Recursion is a technique where a function calls itself to solve a problem by breaking it into smaller sub-problems. Every recursive function must have: 1. Base case — condition that stops the recursion. 2. Recursive case — the function calls itself with a reduced problem size.

Example: Fibonacci Series

#include <iostream>
using namespace std;
int fibonacci(int n) {
    if (n == 0) return 0;        // Base case 1
    if (n == 1) return 1;        // Base case 2
    return fibonacci(n - 1) + fibonacci(n - 2);  // Recursive case
}
int main() {
    int n = 7;
    cout << "Fibonacci series up to " << n << " terms: ";
    for (int i = 0; i < n; i++)
        cout << fibonacci(i) << " ";
    return 0;
}
// Output: Fibonacci series up to 7 terms: 0 1 1 2 3 5 8

How it works for fibonacci(5):

fibonacci(5) = fibonacci(4) + fibonacci(3) = (fibonacci(3) + fibonacci(2)) + (fibonacci(2) + fibonacci(1)) = ... ultimately = 5

Advantages of recursion:

  • Makes code simpler and more readable for problems with a natural recursive structure (trees, towers of Hanoi, factorial, etc.).
  • Reduces the amount of code needed.
  • Follows mathematical definitions closely.

Disadvantages of recursion:

  • Uses more memory due to repeated function calls on the stack.
  • Can be slower than iterative solutions because of function call overhead.
  • Risk of stack overflow if the base case is missing or incorrect.
  • Difficult to debug for complex problems.

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

Frequently Asked Questions

A function is a named, reusable block of code that performs a specific task. It may accept parameters (inputs) and return a value (output). Functions promote code reuse and modularity.