Lecture 5

 

Functions

 Programs need to be broken into many functions.

  1. A program may need to call the same function numerous times.
  2. Different programs may need to utilize the same function.
  3. Large programs may require many software personnel
  4. Ideal program is where the main only has call functions

 

There are either predefined functions, which come from C++ libraries, or user defined functions.  When using the predefined functions one needs to include the header files from where these functions reside.

 

Predefined Functions

abs(x)        header file <cstdlib>  int  x               int result            returns the absolute value of its argument

ceil(x)        header file <cmath> double x          double result     returns the smallest whole number that is not less than x

cos(x)        header file <cmath> double x          double result     returns the cosine of angle x

exp(x)       header file <cmath> double x          double result     returns ex  where e = 2.718

fabs(x)       header file <cmath> double x          double result     returns the absolute value of its argument

floor(x)     header file <cmath>  double x          double result     returns the largest whole number that is not greater than x

pow(x,y)   header file <cmath> double x          double result     returns x . If xy  is a negative number y must be a whole number. Why?

tolower(x) header file <cctype>            int  x                int result            returns the lower case value of x if x is uppercase; otherwise returns x

toupper(x) header file <cctype>            int  x                int result            returns the upper case value of x if x is lowercase; otherwise returns x

 

Exercise

ceil(9.8)=

ceil(-4.7)=

floor(3.35)=

pow(2,3)=

pow(-4,3)=

fabs(pow(-4,2))=

 

User defined functions

Function format

 

function_Type function_Name(type_variables variables) //function header with variables

// referred as formal parameters

{

            statement 1; combination of statements called function body

            .

 

            .

            statement n;

}

 

Function call format

 

function_name(variables); // variables referred to  as actual parameters

//actual parameters need to be in the same order and same amount as the formal ones.

 

Functions defined in C++ are placed in two categories

 

Value-returning functions - functions that provide a return type

Void functions – functions that return nada

 

The value returning function ends with the return statement

Return expression;

Expression is a variable, constant value, or expression evaluated to a value and returned to the calling program

* If a return has more than one return value the last one is the one to be sent back to the calling routine

e.g. return a, b; // only the variable b will be returned to the calling code.

 

Therefore main() is the principal function where program execution always begins with

They have a return 0;

This returns a 0 to the operating system

 

Order of user defined functions

C++ programmers normally place the function main() before all user defined functions.  Normally this would produce a compilation error because the user function will be mentioned in the call before it is defined to the compiler.  We therefore use what we call a function prototype

function_Type function_Name(variable_Type variable_Name);

function variables are parameter list, acceptable to leave out variable names

 

Exercise

1. Convert fahrenheit to celcius using 1 function

 

# include <iostream>

using namespace std;

double fahrToCelcius(double);

int main()

{

            cout<<”\n This program converts a temperatue\n “

                   << “\t from Fahrenheit to Celcius.\n”;

            double fahrenheitTemp;

            cout<<”please enter a Fahrenheit temperature: “;

            cin>>fahrenheitTemp;

            double celciusTemp=fahrToCelcius(fahrenheitTemp);

            cout<<”\n\t”<<fahrenheitTemp

                   <<” in Fahrenheit is equivalent to “

                   <<celciusTemp<<” in Celcius.\n\n”;

            return 0;

}

double fahrToCelcius(double Temp)

{

            return(Temp-32.0)/1.8;

}

 

2. Convert fahrenheit to celcius using 1 function

 

# include <iostream>

using namespace std;

double getFahrTemperature(void);

double fahrToCelcius(double);

void displayResult(double,double);

 

int main()

{

            cout<<”\n This program converts a temperatue\n “

                   << “\t from Fahrenheit to Celcius.\n”;

 

            double fahrenheitTemp= getFahrTemperature();

 

            double celciusTemp=fahrToCelcius(fahrenheitTemp);

 

            displayResult(fahrenheitTemp, celciusTemp);

 

            return 0;

}

           

double getFahrTemperature(void)

{

cout<<”please enter a Fahrenheit temperature: “;

            cin>>temp;

            return temp;

}

 

double fahrToCelcius(double Temp)

{

            return(Temp-32.0)/1.8;

}

 

void displayResult(double fahrenheitTemp, double celciusTemp)

{

cout<<”\n\t”<<fahrenheitTemp

                   <<” in Fahrenheit is equivalent to “

                   <<celciusTemp<<” in Celcius.\n\n”;

}