Functions
Functions
Functions
In Java, we refer to the fields and methods of a class. In C++, we use the
terms data members and member functions. Furthermore, in Java, every
method must be inside some class. In contrast, a C++ program can also
include free functions - functions that are not inside any class.
Every C++ program must include one free function named main. This is the
function that executes when you run the program.
int main() {
cout << "Hello world!" << endl;
return 0;
}
Things to note:
1. Function main should have return type int; you should return zero to
indicate normal termination and any non-zero value to indicate that an
error occurred (e.g., bad data was read, an attempt to open a non-
existent file was made).
2. Function main is not required to have any parameters. However, if you
intend your program to be run with command-line arguments, you
should declare main as follows:
Pass by Value
Using "pass by value", the data associated with the actual parameter is copied
into a separate storage location assigned to the formal parameter. Any
modifications to the formal parameter variable inside the called function or
method affect only this separate storage location and will therefore not be
reflected in the actual parameter in the calling environment. For example (this
could be either C++ or Java):
void cribellum(int x) // Formal parameter is "x" and is passed by value.
{
x = 27;
}
void someCaller()
{
int y = 33;
cribellum(y); // Actual parameter is "y"
// Actual parameter "y" still has 33 at this point.
...
}
Pass by Reference
Using "pass by reference", the formal parameter receives a reference (or
pointer) to the actual data in the calling environment, hence any changes to
the formal parameter are reflected in the actual parameter in the calling
environment. Here is a modified version of this example (now strictly a C++
example since Java does not allow primitive types to be passed by reference):
void cribellum(int& x) // Formal parameter "x" is now passed by reference.
{
x = 27;
}
void someCaller()
{
int y = 33;
cribellum(y);
// Actual parameter "y" now has 27 at this point.
...
}
call ↓
cribellum(14*y + i -
OK OK compilation error
10);
cribellum(y); OK OK OK
cribellum(values[2]); OK OK OK
cribellum(values[3*i -
OK OK OK
2]);
What about arrays? In C++, conventional arrays are not objects. (Recall the
comments in the earlier Arrays section.) Conventional C++ arrays can only
be passed by reference, in large part due to the fact that their size is
unknowable to the C++ runtime system, hence it would be impossible to
make a local copy for a "pass by value" scheme. From a syntactical point of
view, the "&" modifier is not used for formal parameter arrays in C++ since
they can only be passed by reference.
Usually, when you write a C++ program you will write more than just the
main function. Here's an example of a program with two functions:
# include <iostream>
using namespace std;
void print() {
cout << "Hello world!" << endl;
}
int main() {
print();
return 0;
}
In this example, function main calls function print. Since print is a free function (not a
method of some class), it is called just by using its name (not xxx.print()). It is
important that the definition of print comes before the definition of main; otherwise,
the compiler would be confused when it saw the call to print in main. If you do want
to define main first, you must include a forward declaration of the print function
(just the function header, followed by a semi-colon), like this:
# include <iostream>
using namespace std;
void print();
int main() {
print();
return 0;
}
void print() {
cout << "Hello world!" << endl;