Review Functions
Review Functions
if(x>=y)
maximum = x;
else
maximum = y;
return maximum;
}
Function Prototype
• Every function should have a function prototype.
• The function prototype specifies the type of the
value that the function returns (if any) and the
type, number, and order of the function's
arguments.
return-data-type function-name(argument data types);
or
void function-name(argument data types);
Function Prototype (cont.)
• The use of function prototypes permits
error checking of data types by the
compiler.
• It also ensures conversion of all arguments
passed to the function to the declared
argument data type when the function is
called.
Preconditions and Postconditions
• Preconditions are a set of conditions
required by a function to be true if it is to
operate correctly.
• Postconditions are a set of conditions
required to be true after the function is
executed, assuming that the preconditions
are met.
Preconditions and
Postconditions (cont.)
int leapyr(int)
// Preconditions: the integers must represent a year in
// a four digit form, such as 1999
// Postconditions: a 1 will be returned if the year is
a // leap year; otherwise, a 0 will be returned
{
C++ code
}
Calling a function
• A function is called by specifying its name
followed by its arguments.
• Non-value returning functions:
function-name (data passed to function);
• Value returning functions:
results = function-name (data passed to
function);
Calling a function (cont.)
#include <iostream.h>
int FindMax(int, int); // function prototype
int main()
{
int firstnum, secnum, max;
cout << "\nEnter two numbers: ";
cin >> firstnum >> secnum;
max=FindMax(firstnum, secnum); // the function is called here
cout << "The maximum is " << max << endl;
return 0;
}
void cdabs(float x)
{
if (x<0)
x = -x;
cout << "The abs value of the float is " << x << endl;
}