A subroutine/function consist of a (unique) name and a (subroutine/function) body
The subroutine/function can optionally have one or more parameters that can affect the behavior of the subroutine/function
The function definition contain all the information on a function:
The function declaration contain enough information to use the function:
NOTE: a function definition encompasses (includes) all information in a function declaration
BEFORE you can call a function/method, you MUST have either DEFINED or DECLARED the function |
I will separate the discssion into 2 parts:
ReturnType MySubrName(type1 Var1, type1 Var2, ...) { *** LOCAL Variable definitions *** Subroutine Body (statements) } |
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) int a; short b; b = Square(a); // Conversion !! |
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.
C++ will catch your lie and prevent a successful compilation
The C compiler is not as careful as the C++ (better type check) and does NOT catch your lie...
Try running the problem and see the problem...
The type of the input parameters are:
Example:
float RectangleRule_Prec(float h(float), float a, float b, float EPS) { ... } |
You may (for your own record) specify names for the parameters of the function parameter; but they will be ignored by the C/C++ compiler
Example: for clarity, you can define RectangleRule_Prec as:
float RectangleRule_Prec(float f(float x), float a, float b, float EPS) { ... } |
Example:
extern float RectangleRule_Prec(float (float), float, float, float); |
This is NO need mention the name of the actual function that you will use...
All the compiler need to know is (1) how many parameters the function takes, (2) the type of each parameter and (3) the type of value returned
Notice that a Function DECLARATION does NOT have curly braces "{" and "}" and MUST be terminated by a semi-colon (;)
Here also (as in the Function DEFINITION (See: click here )), you may (for your own record) specify the name of the function and names for the parameters; but they will be IGNORED by the C/C++ compiler:
extern float RectangleRule_Pred(float h(float y), float a, float b, float EPS); |
The ANSI C/C++ standard requires the use of the keyword extern.
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 (float), float, float, float); extern float f(float); extern float g(float); float result1, result2; .... result1 = RectangleRule_Pred(f, 1.0, 2.0, 0.0001); result2 = RectangleRule_Pred(g, 7.0, 9.0, 0.0001); |