Dani 4,645 The Queen of DaniWeb Administrator Featured Poster Premium Member

Breaking Code Apart
By following the theories of data abstraction, described in detail in the documents section, code is broken apart into smaller segments known as functions. Functions actually operate much like their mathematical counterparts. In the simplest terms, functions act like more complex versions of operations. However, unlike mathematical operators which act upon numbers, functions act on an infinte number of variables and perform an infinite number of tasks. Functions can be thought of as mini-programs which together make up a program. For example, suppose one was to write a program to compute the numeric average of numbers which the user inputs. One function might get the data from the user, another function might calculate the average, and a third function would spit the result back out to the user. Functions make programming tasks much simpler and allow certain parts of programs to be reused over and over again. For example, a function to get data from a user can be used in numerous ways throughout a program. Memory is saved by re-using the code in more than one way. In addition, in large-scale programs, where many programmers are working on a particular program, they allow each programmer to work individually.

Mathematical Logic Behind Functions
The starting point for everything up to the most basic of C++ programs is the int main() function. The main purpose of this starting function is, for the most part, to call out to other functions. Functions consist of a declaration and a definition. A function declaration is located at the very top of a C++ document and defines the data types of variables being passed into the function, and the data type being passed back. A function definition, meanwhile, can be located anywhere in the program so long as there exists its definition. It consists of the actual function code. Functions can operate without being declared. However, in such a case, their definitions must lay prior to the function(s) calling them. All functions can be called from within all other functions.

The best way to think of a function is to begin by reintroducing the mathematical functions first described in Math Sequential III and continued through Pre-Calculus and way into the heart of Calculus. Take, for example, the function f(x) = 2x + 3, pronounced f of x is equal to 2x + 3. Multiple values can be plugged in for x yielding various results for f(x) ... or y, if relating to the x, y axes. Suppose we plug 2 into the variable x. This would make y equal to 2(2) + 3, or 7. Now suppose we plug 12 into x, making y = 2(12) + 3 = 27. Note that one variable, x, is being passed into the function, while another variable, y, is being passed back.

Passing Parameters
In a function, a series of variables, called parameters, are passed into the function. Inside the function, a series of code is executed to perform various actions and manipulate these parameters. Then, either nothing or a single variable is passed back into the function which called the function in question. Just as in mathematical functions, the domain represents what is being passed into a function while the range is what is being passed out.

The variable which is passed back from a function can be any data type such as an int, float, double, or char. It is passed back with a simple return statement, where the word return is followed by the value or variable to be passed back. If not for anything else but simplicity, it is best to have the fewest number of return statements possible within a function if it can be helped. Note that once a return statement is executed, the program automatically ends the function and returns to the function making the call. While there may be multiple return statements within a function, only one may be executed during the course of the function run. (The same function may be executed again at a later time in the program and a different return statement may be implemented based on various conditions the programmer defines, i.e. program branching.)

Of course, not all functions have information to be passed back. Sometimes functions are used only to print information out or perform other behind-the-scenes tasks. If no value is to be passed back, the function is deemed void.

Calling Functions
Of course, functions are not executed right away. Rather, they are executed when other functions call them. A function call can be in the form of an expression or a statement. When a function has a value to return, the function call is in the form of an expression representing that value. It can be used in code in a similar way to a variable, itself. Therefore, something needs to be done with the value being passed back: the function call must be a part of a statement. However, if the function is void and nothing is being passed back, the function call acts as a stand-alone statement. (Remember all statements end in a semi-colon and this is no exception).

A C++ function call looks almost exactly like a mathematical function call, only it may act as a C++ expression or statement. In other words, it is embedded within other C++ code.

Local Variables
In C++, variables declared within a function are known as local variables. In other words, they can only be seen within the particular function. If, perhaps, the variable happy were declared in function A, you would generally not be able to make reference to happy in function B. That is, of course, unless A calls B and happy is passed into B as a parameter. When a variable is passed into a function as a parameter, it does not have to go by the same name as it did in the function which called it. For example, a variable x can be passed into a function and suddenly take up the variable name number. Both x as well as number are local variables, each only able to be seen by their own respective functions. It should also be noted that if a variable is declared as being passed into a function, it need not be declared over again within that function.

#include<iostream.h>

double f(int);

int main() {

int x = 5;
cout << f(x);
cout << "end";
return 0;

}

double f(int number) {

cout << number;
if (number > 10)

return 10.0;

else

return 1.0;

}

The above C++ program is perhaps one of the simplest demonstrations of a function. It begins by declaring a function named f in which an integer is passed in and a double is passed back out. This can be broken down more clearly to understand that a whole number, called a parameter, is being passed in while a decimal number is being passed out. Up to this point, however, we only know what types of data are being passed in and out and not yet what they represent.

The program then begins in main, where a variable x with value 5 is declared. The function f is then called in which case we pass the value of x, which is 5, into the function. According to C++ syntax, number is a variable local to the function. In other words, it cannot be seen anywhere else in the program. Because whatever value f passes back after it is called will now be printed, we must jump down to see what happens.

When we jump down, we see that f begins by printing out number. Number is, of course, what x used to be: 5. A simple if statement is then used with two return statements. Depending on the value of number, a different decimal value (double) is passed back into the main() function. Because the entire function itself is part of a cout<< statement, the double value passed back is now printed out as part of main(). In other words, the function f returns a double, and a cout<< statement in main() has been used to tell the computer what to do with this double being thrown back out at it.

Void Functions
As previously stated, void functions are those which do not return a value. Parameters may still be passed into such functions, only the type of the function, itself, is void. Void functions contain either no return statements, or a simple return; statement with no data to return. In addition, because void functions have no data to be passed back, their function calls do not need to be enclosed within a statement directing what to do with information passed back. There is nothing to be passed back and thus their calls may stand on their own as individual statements.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.