←Back to Subject
Notes
Notes
Study material and explanations
Diagrams
Diagrams
Visualize concepts easily
Mnemonics
Mnemonics
Tricks to remember faster
One Night Prep
One Night Prep
Short and focused revision
Revision
Revision
Quick summary and key points
Important Questions
Important Questions
Practice most relevant questions
Notes

Functions in C++: The Main Function & Function Prototyping

Study material and explanations for Unit 2

Functions in C++: The Main Function & Function Prototyping

1. What is a Function? (In Simple Words)

A Function is a reusable block of code written to perform a specific task.

Real-Life Analogy: Think of a mixer grinder in your kitchen. Whenever you want to make tomato puree or mango shake, you don’t invent a new machine from scratch. You just put in the ingredients, press the button, and get the result!
A function works the same way: you pass inputs (arguments), it does the work, and gives you back the answer.

Why Do We Use Functions?

  • No Duplicate Code: Write the logic once and call it as many times as you like.
  • Easy to Understand (Modularity): Instead of writing 500 lines in main(), break your project into small, neat functions (like add(), calculateBill(), printSlip()).
  • Easy to Find Bugs: If the bill total is wrong, you only need to check the calculateBill() function.

2. The main() Function in C++

Every single C++ program must have a main() function. This is where your program starts running and where it ends.

int main() {
    // Your code runs here
    return 0; // Tells the OS: "Everything went smoothly!"
}

Important Questions Asked by DAVV Examiners:

  1. Why int main() instead of void main()?
    Modern standard C++ strictly requires int main(). The operating system (Windows or Linux) expects an integer return code from the program when it finishes.
  2. What does return 0; mean?
    Returning 0 tells the operating system that the program executed successfully without any crashes or errors. A non-zero number (like 1 or -1) means an error occurred.
  3. What if I forget to write return 0;?
    In modern C++, if you reach the closing brace } of main(), the compiler automatically adds return 0; for you.

3. Function Prototyping (Why is it Required?)

In old C language, you could call a function without telling the compiler in advance. The compiler would try to guess the data types, which often caused unexpected crashes!

C++ fixes this with strict type checking using Function Prototyping.

A Function Prototype (Function Declaration) is an advance notice given to the compiler. It tells the compiler:

  • The name of the function.
  • What data type it returns.
  • How many arguments (parameters) it expects, and their data types.
return_type function_name(data_type param1, data_type param2);
Anatomy of a Function Prototype
Return Type
What kind of data the function sends back (int, float, void).
Function Name
The name you use to call it (e.g., findMax).
Parameter List
Types of inputs it expects (e.g. int, int). Variable names are optional here.
Semicolon (;)
A prototype always ends with a semicolon ; because it is just a declaration.

4. Code Example: Prototype, Call & Definition

Here is how all 3 pieces work together in a complete program:

#include <iostream>
using namespace std;

// 1. FUNCTION PROTOTYPE (Notice the semicolon at the end)
// Informs compiler: "A function called findMax exists, takes 2 ints, returns an int"
int findMax(int, int);

int main() {
    int num1 = 45, num2 = 82;

    // 2. FUNCTION CALL
    int result = findMax(num1, num2); 
    cout << "The largest number is: " << result << endl;

    return 0;
}

// 3. FUNCTION DEFINITION (Contains actual logic, NO semicolon at function header)
int findMax(int a, int b) {
    if (a > b)
        return a;
    else
        return b;
}

How Does the Compiler Check It?

When the compiler sees findMax(num1, num2) inside main(), it checks the prototype above:

  • Does findMax accept 2 arguments? Yes.
  • Are they both integers? Yes.
  • Does the return value match int result? Yes.

If you mistakenly write findMax("hello"), the compiler immediately catches the mistake with an error message instead of letting your program crash at runtime!


DAVV Exam Preparation Tips

Frequently Asked 5-Mark Questions:

  1. What is function prototyping in C++? Why is it mandatory?
    Model Answer:
    • Define prototyping: declaring a function’s return type, name, and parameter list before main().
    • Explain that C++ uses it for compile-time type checking so argument types and counts are verified before the function is called.
  2. Why is int main() preferred over void main() in C++?
    Model Answer:
    • Standard ANSI/ISO C++ requires int main().
    • Operating systems need an exit status code (0 for success, non-zero for error) to confirm whether the program finished properly.

Frequently Asked 10-Mark Questions:

  1. Explain the 3 main parts of a function in C++: Declaration (Prototyping), Call, and Definition. Write a C++ program to calculate the power of a number (xⁿ) using a user-defined function.
    Model Answer:
    • Explain Function Declaration (the prototype before main()).
    • Explain Function Call (triggering execution from inside main()).
    • Explain Function Definition (the actual logic written with curly braces {}).
    • Provide the complete code for double power(double base, int exp) with proper prototype, input handling, and loop multiplication.