|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Functions
Functions are
segments of code that allow you to better organize your code.
You can think of a function as a small program, and of a program as a collection
of functions. I could have written a function for the "Hello World" program:
#include <iostream.h>
void print_hello() { // This line declares the function
cout << "Hello World!\n"; // This is the body, which defines the function
}
void main(){
print_hello(); // This is how the function is called
}
The print_hello() function The function is void because it does not
return any value once it has finished. If it did return a value, its return
type would be the type of the value it returned. After specifying the return
type, the name of the function is given, followed by parentheses. These
parentheses will usually contain function arguments, or the function's input,
which will be described below. Adding a semicolon to the end of the line would
have been enough to simply declare the function, and I could have
defined the function later. Since it is a short function, however, we
choose to define it on the spot by typing our code between opening and closing
braces.
The following example demonstrates some of the other options for function
writing:
#include <iostream.h>
float silly_calc(float num1, float num2);
void main(){
float a = 5.0;
cout << silly_calc(7.8, a) << endl;
}
float silly_calc(float num1, float num2){
float sc = (num1 + num2) / 2;
if (sc > 1) {return sc;}
else return (sc / 2);
}
Look at the declaration for the function silly_calc(). It establishes that
the function will return a floating point number, that the name of the function
is silly_calc, and that the function requires two floating point arguments.
In main(), the function is called with the arguments 7.8 and the variable
a, whose value is 5.0. The function silly_calc() is actually defined
after main(). The first line of a function definition must exactly match
the function declaration; that is, it must have the same return type, same name,
and same arguments as the declaration. The only code in this example that might
be unfamiliar to you is the use of the return statements in the function body.
The return command simply tells the program to stop execution of the function,
and to give back whatever value the function computed. If a function is void,
you can simply type return; and the function will terminate, whether or not
it has reached the closing brace.
Note that the variable sc is declared within the silly_calc() function, and
therefore can not be directly accessed by any other part of the program. Once
silly_calc() has finished, sc is "out of scope." Only if sc was
declared before main() would it be a
global variable and visible
throughout the program. Having more than a few global variables is usually bad
style; variables should only be accessible and modifiable where necessary.
Another interesting fact is to note that even the silly_calc() function will
forget the value of sc after it has finished. The next time
silly_calc() is called, it will create an entirely new sc variable. It
is possible to have a function remember the value of one of its local variables
by using the static keyword. The following function prints out the number
of times that it has been called:
int call_count() {
static int num_of_calls = 0; // the initial value of the static variable
// only effective the first call.
return ++num_of_calls; // increment the number of calls and return it.
}
C++ supplies programmers with the inline keyword, which can speed up
programs by making very terse functions execute more efficiently. By adding the
word inline before a function definition, C++ will essentially cut and paste
the inline function wherever it is called in your program at compile time.
Normally a function resides in a separate part of memory, and is referred to by
a running program when it is needed. Inline functions save the step of
retrieving the function, at the cost of a larger compiled program. C
programmers should forget the #define command for macros and instead use
these inline functions; inline functions are clearer in that they require data
type specification, thus avoiding any confusion for the programmer and compiler.
The MAX function is a typical (and good) example of a good time to use
inline:
inline int MAX(int a, int b)
{
return (a > b) ? a : b;
}
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Contact Us | Privacy Policy | Terms and Conditions | About
©2006 SparkNotes LLC, All Rights Reserved.
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||