Loading lessons...
The main() Function & Function Prototyping
The main() Function & Function Prototyping
Functions are the building blocks of every C++ program, and member functions are the behaviour of every object. This lesson starts the "functions in C++" topic: the special function main(), and function prototypes, which C++ makes compulsory so that every call can be type-checked.
1. The main() Function
Every C++ program must have exactly one function called main. The operating system calls it to start the program, and the value it returns tells the system whether the program succeeded.
In C, main may be written without a return type. In C++ main returns an int. The standard allows two forms:
int main() {
// ...
}
int main(int argc, char* argv[]) {
// ...
}| Point | Rule |
|---|---|
| Return type | always int. void main() is not standard C++ (old Turbo C++ allowed it) |
| Return value | return 0; (or EXIT_SUCCESS) means success; non-zero means failure |
Missing return | in main only, reaching the closing brace acts as return 0; |
Calling main | a C++ program must not call main itself (C allows it) |
| Overloading | main cannot be overloaded, declared inline or static |
Command-line arguments
argc (argument count) is the number of words on the command line, including the program name. argv (argument vector) is an array of C strings holding them. argv[0] is the program name and argv[argc] is a null pointer.
#include <iostream>
#include <cstdlib>
using namespace std;
int main(int argc, char* argv[]) {
// Pretend the program was run as: add 12 30
// (here we build the same argv by hand so the example can run on its own)
if (argc < 3) {
const char* fake[] = {"add", "12", "30"};
int total = atoi(fake[1]) + atoi(fake[2]);
cout << "argc would be 3" << endl;
cout << fake[1] << " + " << fake[2] << " = " << total << endl;
return 0;
}
int total = atoi(argv[1]) + atoi(argv[2]);
cout << argv[1] << " + " << argv[2] << " = " << total << endl;
return 0;
}Output:
argc would be 3
12 + 30 = 42Run from a terminal as add 12 30, the program reads its numbers from argv[1] and argv[2] instead.
2. Function Prototyping
A function prototype is a declaration that tells the compiler a function's name, return type, and the number and types of its arguments, before the function is used. The compiler then checks every call against it.
float volume(int x, float y, float z); // prototype with names
float volume(int, float, float); // names are optional in a prototypeIn C, prototypes were optional (ANSI C) and old-style C allowed calls to undeclared functions. In C++ prototypes are essential: a function must be declared before it is called.
Why prototypes matter
- Type checking of arguments: a call with the wrong number of arguments is a compile-time error, not a run-time surprise.
- Automatic conversion of arguments: a call
volume(2, 3, 4)converts3and4tofloatas the prototype says. - Checking the return type: the compiler knows what the function returns and converts or complains correctly.
- Overloading: the compiler can only choose between overloaded functions if it knows their parameter types.
- Separate compilation: header files hold the prototypes, so a function can be defined in a different file from where it is called.
#include <iostream>
using namespace std;
// prototypes: main can call these before their definitions appear
float volume(int length, float width, float height);
void line(int n);
int main() {
float v = volume(2, 3, 4); // ints converted to float as the prototype says
line(20);
cout << "Volume = " << v << endl;
line(20);
// volume(2, 3); // error: too few arguments to function
return 0;
}
// definitions
float volume(int length, float width, float height) {
return length * width * height;
}
void line(int n) {
for (int i = 0; i < n; i++) {
cout << '-';
}
cout << endl;
}Output:
--------------------
Volume = 24
--------------------Declaration vs definition
| Declaration (prototype) | Definition |
|---|---|
| tells the compiler about the function | contains the actual code (the body) |
ends with ; | has a { ... } body |
| argument names optional | argument names required if used in the body |
| may appear many times | must appear exactly once in the program |
| usually placed in a header file | usually placed in a .cpp file |
An empty argument list
In C++, int display(); means the function takes no arguments; it is the same as int display(void);. In C, an empty list means "arguments not specified". An open argument list, meaning any number of arguments, is written with an ellipsis: void print(int n, ...); (as printf does).
The names of arguments in a prototype are like comments: they help the reader, but the compiler ignores them. Names in the prototype and the definition do not have to match. The types and their order must match.
Key points to remember
mainreturnsint;return 0means success;void main()is non-standard.int main(int argc, char* argv[]):argccounts words,argv[0]is the program name.- A prototype gives the name, return type and argument types; C++ requires one before any call.
- Prototypes allow compile-time type checking, automatic argument conversion and overloading.
f()in C++ meansf(void): no arguments.
📝 IPU End-Term Exam Corner (5-10 Marks)
- Q1. What is function prototyping? Why is it necessary in C++? (5 Marks)
- Write: the definition and syntax, the five reasons, and the
volumeprogram. - Q2. Explain the
main()function in C++. How are command-line arguments passed? (5 Marks) - Write: the two standard forms, the rules table, and the
argc/argvprogram. - Q3. Differentiate between a function declaration and a function definition. (2.5 Marks)
- Q4. What is the difference between
int f();in C and in C++? (2.5 Marks)