Functions

Program Modules

Example: hello2.htm

Library Functions

Math Functions

Header File Function Parameter Type Result Type Result
stdlib.h abs(i) int int absolute
stdlib.h labs(j) long long absolute
math.h fabs(x) float float absolute
math.h cos(x) float float cosine
math.h sin(x) float float sine
math.h sqrt(x) float float square root
                                
Boolean Functions in <ctype.h>

Value-Returning Functions


#include 

double FahrToCelsius(double);             // function declaration

int main(void)
{
   double
      FahrenheitTemp;

   cout << "\nPlease enter a Fahrenheit temperature: ";
   cin >> FahrenheitTemp;

   double                                 // function call
      CelsiusTemp = FahrToCelsius(FahrenheitTemp);

   cout << "\n\t" << FahrenheitTemp 
        << " in Fahrenheit is equivalent to " 
        << CelsiusTemp << " in Celsius.\n\n";
		
   return 0; 
} 

double FahrToCelsius(double Temp) // function definition 
{ 
   return (Temp 32.0) / 1.8; 
}

Void Functions

void Hello()
{
    cout << "Hello world" << endl; 
}

Function Prototype

Random Number Generation

Scope and Lifetime

scope

The region of program code where it is legal to reference (use ) an identifier.
  1. class scope
  2. local scope
  3. global scope
name precedence (hiding) : when a function declares a local identifier with the same
name as a global identifier, the local one takes precedence within the function.

scope rules

  1. A function name has global scope. Function definitions can not nested within function definitons.
  2. The scope of a formal parameter is identical to the scope of a local variable declared in the outmost block of the function body.
  3. The scope of a global variable or constant extends from its declaration to the end of the file, except as noted in rule 5.
  4. The scope of a local variable or constant extends from its declaration to the end of the block in which it is declared. This scope includes any nested blocks, except as noted in rule 5.
  5. The scope of an identifier does not include any nested block that contains a locally declared identifier with the same name.

Scoping Example: scoping

external declaration

Example:
    extern int someInt;

lifetime

The period of time during program execution when an identifier has memory allocated to it.

In C++, each variable has a storage class that determines its lifetime:

Reference parameters

void Swap(int& a, int& b)
{
	int tmp;
	tmp = a;
	a = b;
	b = tmp;
}

Unary Scope Resolution Operator

#include <iostream.h>

float value = 1.2345;

main()
{
   int value = 7;

   cout << "Local value=" << value << endl
        << " Global value=" << ::value << endl;

   return 0;
}

Function Overloading

#include <iostream.h>

int square(int x) { return x * x; }

double square(double y) { return y * y; }

main()
{
   cout << "The square of integer 7 is " << square(7) << endl 
        << "The square of double 7.5 is " << square(7.5) << endl; 
   return 0; 
}

Function Templates

#include <iostream.h>

template <class T>
T maximum(T value1, T value2, T value3)
{
   T max = value1;

   if (value2 > max)
      max = value2;

   if (value3 > max)
      max = value3;

   return max;
}

main()
{
   int int1, int2, int3;
   cout << "Input three integer values: ";
   cin >> int1 >> int2 >> int3;
   cout << "The maximum integer value is: " 
        << maximum(int1, int2, int3); // int version
 
   double double1, double2, double3; 
   cout << endl << "Input three double values: ";
   cin >> double1 >> double2 >> double3;
   cout << "The maximum double value is: " 
        << maximum(double1, double2, double3); // double version
 
   char char1, char2, char3; cout << endl << "Input three characters: ";
   cin >> char1 >> char2 >> char3;
   cout << "The maximum character value is: " 
        << maximum(char1, char2, char3) // char version << endl; 

   return 0;
}