Home - Contribute - References - Site Map - Contact Us
C++ Review [Variables, Functions, Pointers, Object Oriented Design, Templates, Other]
Data Structures [Preliminary, Lists, Stacks, Queues, Binary Trees, Heaps, Sorting, Searching]
Functions
A function is a collection of programming statements that can be called in various locations of the program. In C/C++, the entire program consists of functions, main() is the first to be called when the program starts. Functions can have values passed to them, which can in turn be used and/or modified by the statements in the function. These values are called function parameters. Also, functions can return a value when they finish executing their code. For instance, an addition function might add two numbers, and return the result. The function return value can then be outputted, or stored. The syntax for declaring a function is shown below.
return_type function_name(parameters list)
If a function does not return any value, void is used in place of the return type. In order to call a function, the name of the function is used, followed by the parameters in parenthesis. If a function has a return value, make sure to assign it to a variable or output the value. A simple addition function is shown below.
{
//function code
}
int main()int add(int x, int y)
{
return (x+y);
}
{
int a = 2;
int b = 5;
cout << add(a,b);
//cout << add(2,5); would also have the same effect
return 0;
}
The function lifetime ends when it encounters the first return statement. When the function is terminated, any variables that were created in that function, known as local variables, are also destroyed. A function can have multiple return statements using if conditions, each that returns a different value depending on the parameters passed or user input.
When a variable is passed as a parameter to function, a local copy of that variable is created in memory for the function to use. Therefore, if a variable passed is modified within the function, the local copy is actually changed, which is destroyed when the function ends. For example,
int a=7;
void Foo(int x)
The above statement would output '7' to the screen. A local value of 'a' is created in function Foo. This local copy is then modified on the line x=20, and then destroyed when the function ends. When the scope is returned to the function from where
{
x=20;
}
Foo(a);
cout << a;Foo() was called, the value of 'a' remains the same. A special reference parameter may be used if you do not wish the computer to create a local variable for a function to use. The address of the variable is passed to the function, and the actual variable can be accessed. In order to make a parameter a reference, the & operator is used. The swap() function is shown below. The function takes in two integer variables, and exchanges their values.
int a=7;void swap(int &x,int &y)
{
int tmp = x;
x = y;
y = tmp;
}
int b=5;
swap(a,b);
cout << a << ' ' << b;
The output of the above code segment is [5 7]. Reference parameters may only be variable names, and cannot be values. The call swap(2,3) would result in an error, since the two numbers are constants, and cannot be changed.
Sometimes, a reference parameter is used even if you do not wish to modify a parameter of the function. If a large structure is to be passed as a parameter, a reference is used in order to conserve the memory used to create a local copy. It is good programming practice to make such parameters const, so they cannot be modified inside the function by accident.
Before a function can be called, it must first be defined in the lines of code preceding the function call. However, a function prototype can be used to signal the program that the function exists, and the function definition can appear after a call to the function is made. The function prototype consists of the function return type, name, and parameters list. In the prototype, you only need to specify the type of parameters, as their names are unimportant. Function prototypes usually appear before main(). An example of a prototype is shown below.
int add(int,int);
Once the prototype has been defined, the function add() can now be called from main(), whereas the statements associated with the function can appear after main() has been defined.
int add(int x, int y)int main()
{
//...
cout << add(a,b);
//...
}
{
//...
}
In C/C++, functions can be 'overloaded'. That is, different functions that do different things can have the same name. However, these functions must have different parameters, either the type, or the amount. The compiler decides which version of the function to call based on the parameters that are passed. An example of function overloading is shown below.
int Foo(int,float)int Foo(int)
{
//...
}
{
//...
}
If Foo(5) is called, then the first version of the function is executed. If Foo(5,2.5) is called, then the code in the second version of the function is executed.
© 2000 ThinkQuest Team C005618