For instance, if there were twelve different places in a program where you needed to compute square roots, it would be extremely inconvenient to have to rewrite essentially the same code sequence in all twelve places. Furthermore, if you needed to alter your square root computations, it might require alterations in all twelve locations - leading to an increased chance of confusion and errors.
A function takes a block of code and gives it a name, as well as giving names to the different kinds of data (parameters) it needs to process. When you want to use a function in a program you simply specify the name of the function and the data you want to supply to it:
// Here we assume there is a function named SquareRoot, that takes // a floating point value as a parameter and returns a floating // point value (the square root of the parameter it was given) x = SquareRoot(36.0);There are already a collection of functions available for you to use (library functions) and later we will also consider the ways in which you can create your own functions.
setw
and setprecision
setw(7)
)
sqrt
function
#include <cstdio> #include <cmath> int main() { double Original, RootOfOriginal; printf( "Please enter any positive real number\n"); printf("%lf", &Original); RootOfOriginal = sqrt(Original); printf( "The square root of %lf", Original); printf( " is %lf\n", RootOfOriginal); }
<cmath>
fmod(x, y); // returns floating point remainder of x / y log(x); // returns the natural logarithm of x pow(x, y); // returns x raised to the power of y sqrt(x); // returns the square root of x cos(x); // returns the cosine of xall data is of type
double
x = sqrt(3.0 / y) - z * cos(r);
sqrt(3.9); // makes no sense printf("%lf", sqrt(3.9)); // prints out calculated value x = sqrt(3.9); // stores calculated value
printf( "The fifth root of 3 is "); // the parameter to printf is "The fifth root of 3 is " printf("%lf\n", pow(3, 0.2)); // the parameters to printf are "%lf\n" and pow(3, 0.2) // the parameters to pow are 3 and 0.2
printbox
,
it expects three parameters:
printbox(4, 2, 'X');
would tell the routine to print two rows of four X's each:
printbox(2, 4, 'X')
would have completely different results
double fabs(double x)
double floor(double x)
double fmod(double x, double y)
int isspace(char c)
int main()
is like a function
that takes no parameters and returns no data (i.e. a return data type of
void
)
#include <cstdio> const float Pi = 3.14; // here is a declaration, summarizing a function // in terms of // the type of data it returns when it is completed (float) // the name for the function (Calc_Circumference) // the list of parameters the function needs to work on // (a floating point number that it will use as a radius) float Calc_Circumference(float radius); int main() { printf( "Please enter the circle radius\n"); float circ_radius; scanf("%f", &circ_radius); printf( "The circle circumference is "); // Call the function using its name and a value, // and use the return value as data for the printf statement printf("%f\n", Calc_Circumference(circ_radius)); } // the actual implementation details of the function // first, repeating the header/declaration information, // then describing the sequence of actions used to // compute the function's result // then a return statement is used to send the calculated // result back to whichever routine called the function float Calc_Circumference(float radius) { // calculate and return circumference return(radius * Pi * 2.0); }
main
routine can call several high level
functions to carry out the major tasks
void
if the
function does not return any data
// printdata prints out the passed parameters // it does not return any value void printdata(int x, char y); // getcommand needs no parameters, // it gets a single character command from the user // and returns this to the calling routine char getcommand(); // calculate area calculates and returns a rectangle's area // given the height and width parameters float calculate_area(float width, float height);
<return type> <function name> ( <list of formal parameters> ) { // local variables, // executable statements, // return statement if return type not void }Examples:
void printdata(int x, char y) { printf( "The integer field has value %d", x); printf( ", while the character field is %c\n", y); } char getcommand() { char cmd; printf( "Please enter a single letter command\n"); scanf("%c", &cmd); return(cmd); } float calculate_area(float width, float height) { return(width * height); }
#include <cstdio> #include <cmath> float Get_wall_area(); float Get_paint_cost(); float Get_paint_coverage(); int Calculate_paint(float area, float coverage); float Calculate_cost(int litres_paint, float unit_cost); void Display_results(float area, float coverage, float unit_cost, int litres_paint, float total_cost); int main() { float area, coverage, unit_cost, total_cost; int litres_paint; area = Get_wall_area(); unit_cost = Get_paint_cost(); coverage = Get_paint_coverage(); litres_paint = Calculate_paint(area, coverage); total_cost = Calculate_cost(litres_paint, unit_cost); Display_results(area, coverage, unit_cost, litres_paint, total_cost); } float Get_wall_area() { float height, width; printf( "Please enter the room height and width "); printf( "in metres, then enter return\n"); scanf("%f", &height); scanf("%f", &width); return(height * width); } float Get_paint_cost() { float cost; printf( "Please enter the paint cost per litre"); scanf("%f", &cost); return(cost); } float Get_paint_coverage() { float coverage; printf( "Please enter the number of square metres"); printf( " of wall space that can be covered with "); printf( "a single litre of paint\n"); scanf("%f", &coverage); return(coverage); } int Calculate_paint(float area, float coverage) { return( int( ceil(area / coverage))); } float Calculate_cost(int litres_paint, float unit_cost) { return( litres_paint * unit_cost ); } void Display_results(float area, float coverage, float unit_cost, int litres_paint, float total_cost) { printf( "For a wall with area %f", area); printf( " square metres, we require "); printf( "%f litres of paint\n", litres_paint); printf( "costing %.2f", unit_cost); printf( " dollars per litre,\n"); printf( "gives a total cost of $%f\n", total_cost); }
double foo(int x, char ch)
myvar = foo(37, 'Y');
#include <cstdio> void XPlus1(int x); int main() { int myvar = 0; XPlus1(myvar); printf("%d", myvar); } void XPlus1(int x) { x = x + 1; }
void swap(int &x, int &y); int main() { int var1 = 13; int var2 = -5; swap(var1, var2); print("%d %d\n", var1, var2); } void swap(int &x, int &y) { int temp; temp = x; x = y; y = temp; }Inside swap, x is acting like another name for var1, while y is acting like another name for var2.
#include <cstdio> double get_area_height_width(float &height, float &width); int main() { float wallheight, wallwidth, wallarea; wallarea = get_area_height_width(wallheight, wallwidth) printf( "area %f = ", wallarea); printf( "width %f * ", wallwidth); printf( "height %f\n", wallheight); } double get_area_height_width(float &height, float &width) // set width and height to user input values // and return area = height * width { printf( "Please enter the wall height\n"); scanf("%f", &height); printf( "Please enter the wall width\n"); scanf("%f", &width); return(width * height); }Because of the & symbols, height and width are local names for wallheight and wallwidth, whose values can really be changed. If the & symbol is left off, get_area_height_width would only receive copies of the values of wallwidth and wallarea, and would not be able to change the values of the `real' variables.
double myfunction(int &refparam); int main() { double x; int y; x = myfunction(3); // illegal, 3 is not an integer variable x = myfunction(y); // is o.k. }The reason for this is that when a parameter is declared as a reference parameter, the function can make assignments to the parameter,
// could appear inside myfunction refparam = 17;In the first example above this would mean 3 = 17; which makes no sense whatsoever
// get the user to enter an integer in the range min..max int getInteger(int min=0, int max=100);If the function is called normally, i.e. with two parameters, then min and max have the values the caller passed.
However, if the function is called without parameters then it uses the default values supplied in the prototype.
For example, x = getInteger(3,10); would use 3 for min and 10 for max, but x = getInteger(); would use 0 for min and 100 for max.
If the function is called with only some of the optional parameters then it assumes the parameters at the end of the list are the ones that use default values. For example, the call x = getInteger(5); would use 5 for min and 100 for max.
Note that the default values are only placed in the prototype - if you place them in both the prototype and the full function implementation you will get a compile-time error.