Log in using your OpenID or your Sol Union log in account.
[Cancel]     

C++ Language Tutorial Page 6
Functions
Silas Bruner
Previous Index Next
Functions
     So far, you've been writing code that is extremely repetitive, and has little in terms of organizational devices. In C++, functions serve as a means to organize your code, and render certain useful pieces of code reusable, lightening your coding load.
     You may remember the mathematical definition of a function from algebra as some thing that takes a value, does something to it, and returns the result. Basically, a function serves as a segment of reusable code that can be applied in a number of different situations, and that is separate from the rest of the program. The syntax for a function definition is:
// prototype
function_type function_name(var1_type var1_name, var2_type var2_name, ..., varN_type varN_name);

// function declaration
function_type function_name(var1_type var1_name, var2_type var2_name, ..., varN_type varN_name) 
{
	// segment of code
	return return_value;
}

int main()
{
	// how to call the function in your code
	function_type var;
	var = function_name(var1_name, var2_name, ect...);
}
     All your functions are declared before main(). The inputs for main() are just the command-line arguments that you supply to the program (if you're running it from a shell, or console-mode). The value that main returns specifies any error condition that exists. The reason main is special is that it is the first function the computer runs at run-time. Printf, the old C equivalent to cout, is also a function. The reason you don't see the declaration anywhere is because it is prototyped in the header stdio.h and declared in the pre-compiled linked library stdlib.
int square(int b)// returns the square of b
{
	return b * b;
}

int power(int base, int exp)// returns base to the exp'th power
{
	int counter, result = base;
	for (counter = 0; counter < exp; counter++)
		result *= base;
	return result;
}

void say_stuff()
{
	cout << "stuff";
}
     In the say_stuff case, the void indicate that the function doesn't have anything to return. In many void functions, the function either outputs using pointers as inputs or variables belonging to a class.
int square(int b)
{
	return b * b;
}

int main()
{
	// Here, the "square(5)" is replaced with 25 at runtime
	cout >> 11 + square(5);
	return 0;
}
     Just like in main, functions which call other functions. A function can also call itself, which is called recursion. This is used in a few more advanced algorithms, and is a bit mind-bending. Here is an example of the recursive function factorial().
int factorial(int n)
{
	if (n == 1)
	{
		return n;
	}
	else
	{
		return n * factorial(n - 1);
	}
}
     If you don't understand what the factorial function is doing, trace it through a basic run. Say we're doing a factorial(5) (which is 5 * 4 * 3 * 2 *1 or 120). Because n is 5 to begin with, then the program moves to the else statement. In the else statement, the code says to return 5 * factorial(4), but first it has to calculate factorial(4). Factorial(4) says to return 4 * factorial(3). The recursion continues until the function returns 5*4*3*2*1, and then stops at one because of the if statement.