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 (likeadd(),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:
- Why
int main()instead ofvoid main()?
Modern standard C++ strictly requiresint main(). The operating system (Windows or Linux) expects an integer return code from the program when it finishes. - What does
return 0;mean?
Returning0tells the operating system that the program executed successfully without any crashes or errors. A non-zero number (like1or-1) means an error occurred. - What if I forget to write
return 0;?
In modern C++, if you reach the closing brace}ofmain(), the compiler automatically addsreturn 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);
int, float, void).findMax).int, int). Variable names are optional here.; 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
findMaxaccept 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:
- 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.
- Define prototyping: declaring a function’s return type, name, and parameter list before
- Why is
int main()preferred overvoid main()in C++?
Model Answer:- Standard ANSI/ISO C++ requires
int main(). - Operating systems need an exit status code (
0for success, non-zero for error) to confirm whether the program finished properly.
- Standard ANSI/ISO C++ requires
Frequently Asked 10-Mark Questions:
- 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.
- Explain Function Declaration (the prototype before