|
The member functions are very similar to the ones in Java, so you should be familiar with them already
The member functions will be discussed later when we discuss classes
|
From now on, I will not used my coined phrase stand-alone function, but simply: function...
The function definition contain all the information on a function:
|
ReturnType MySubrName(type1 Var1, type1 Var2, ...) { *** LOCAL Variable definitions *** Subroutine Body (statements) } |
Basically, it is the same syntax as in Java, except the function definition is not contained in any class
double cube( double x ) { return( x * x * x ); } |
|
double r, x; x = 4; r = cube(x); |
int main(int argc, char *argv[]) { double r, x; x = 4; r = cube(x); // Error: cube() undefined !!! } double cube( double x ) { return( x * x * x ); } |
|
To make the above program work (without re-arranging the order), we must declare the function cube() before it is used:
int main(int argc, char *argv[]) { double r, x; ****** Declare cube() x = 4; r = cube(x); // Error: cube() undefined !!! } double cube( double x ) { return( x * x * x ); } |
|
|
Examples: function declaration
[extern] double cube( double ) ; cube() has one parameter |
The use of extern in a function declaration is optional.
int main(int argc, char *argv[]) { double r, x; double cube(double); // ****** Declare cube() x = 4; r = cube(x); // Error: cube() undefined !!! } double cube( double x ) { return( x * x * x ); } |
|
It "looks nicer"...
File 1: cube.C | File 2: main.C |
---|---|
double cube( double x ) { return( x * x * x ); } |
int main(int argc, char *argv[]) { double r, x; double cube(double); // Declare x = 4; r = cube(x); } |