|
The member functions are very similar, and they will be introduced later when we discuss classes
|
The function definition contain all the information on a function:
|
double cube( double x ) { return( x * x * x ); } |
|
|
Examples: function declaration
extern double cube( double ) ; cube() has one parameter extern int myFunc( int, short, double ); myFunc() has three parameters |
NOTE: a function definition encompasses (includes) all information in a function declaration
ReturnType MySubrName(type1 Var1, type1 Var2, ...) { *** LOCAL Variable definitions *** Subroutine Body (statements) } |
Function Definition: float Square(float x) // Function definition { // float y; // Has all the information // on the function: y = x * x; // return(y); // name, parameters, output } // what to do (body) |
Note that if the type of the expression (...) if different from float (which is the type of value that the function WILL return), the type of expression (...) will first be converted to float before the value is returned !
int main(int param1, char *param2[]) { MAIN PROGRAM } |
extern RetType MySubrName(type1 [Variable1], type1 [Variable2], ...); Things in [ ] brackets are OPTIONAL items Thus: the name of the variables are optional in the function declaration |
Note
To make certain that your program will compile on as many platforms as possible, stick to ANSI C/C++ standard.
extern float Square(float x); // Function declaration OR: extern float Square(float); // Function declaration int a; short b; b = Square(a); // Conversion !! |
extern float Square(float x); OR extern float Square(float);
The compiler also knows that the function will return a float type output value.
// Function is defined // in this file float Square(float x) { float y; y = x * x; return( y ); } |
// Function declaration extern float Square(float); int main(int param1, char *param2) { int a = 4; short b; b = Square(a); cout << " a = " << a; cout << " b = " << b << "\n"; } |
C++ will catch your lie and prevent a successful compilation
The C compiler is not as careful as the C++ (type checking is better in C++) and does NOT catch your lie...
Try running the problem and see the problem...
|
Notice that the parameter f is a function !!!
|
Example:
float RectangleRule_Prec(float h(float), float a, float b, float EPS) { ... } |
NOTE: This name need NOT be the SAME name of the actual function that you will use in the function call.
(The name h is just a name that the program uses to refer to the parameter of the function RectangleRule_Prec
You may specify names for the parameters of the function parameter; but they will be ignored by the C/C++ compiler
Example:
float RectangleRule_Prec(float f(float x ), float a, float b, float EPS) { ... } |
The name x will be ignored by the compiler.
Example:
extern float RectangleRule_Pred(float (float), float, float, float); |
NOTICE that the function name is missing
|
float(float) tells the compiler that
|
extern float RectangleRule_Pred(float (float), float, float, float); |
Notice that a Function DECLARATION does NOT have curly braces "{" and "}" and MUST be terminated by a semi-colon (;)
WRONG SYNTAX: |
extern float RectangleRule_Pred(float h(float y), float a, float b, float EPS); |
(just like in the Function DEFINITION (See: click here )),
I recommend you use this feature - a lot easier to read !!!
But some C/C++ compilers do not need this keyword.
Again, I recomment that you use it....
int main(int argc, char *argv[]) { extern float RectangleRule_Pred(float h(float x), float, float, float); extern float f(float x); extern float g(float x); float result1, result2; .... result1 = RectangleRule_Pred(f, 1.0, 2.0, 0.0001); result2 = RectangleRule_Pred(g, 7.0, 9.0, 0.0001); |