Example: hello2.htm
| 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>
#includedouble 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 Hello()
{
cout << "Hello world" << endl;
}
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.
Scoping Example: scoping
Example:
extern int someInt;
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:
void Swap(int& a, int& b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
}
#include <iostream.h>
float value = 1.2345;
main()
{
int value = 7;
cout << "Local value=" << value << endl
<< " Global value=" << ::value << endl;
return 0;
}
#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;
}
#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;
}