Algoritma & Pemrograman 02 Using Functions and Classes v1.2
Algoritma & Pemrograman 02 Using Functions and Classes v1.2
Prepared by
Frans Panduwinata, S.Kom, M.T
Declaring Functions
• EXAMPLES Functions:
int toupper(int c);
double pow(double base, double exponent);
Function Prototype (cont)
EXAMPLE:
cout << 2.0 + pow( 5.0, x);
Function Calls (cont)
• EXAMPLE :
float x = pow(3.0 + 4.7); // Error!
– You can only name one header file per #include directive
– The file name must be enclosed in angled brackets < ... > or
double quotes " ... ".
Using Header Files (cont)
• These include stream classes for input and output, but also
classes for representing strings or handling error
conditions.
EXERCISE 1
Create a program to calculate the square roots of the numbers
4 12.25 0.0121
and output them as shown below. Then read a number from the keyboard
and output the square root of this number.
To calculate the square root, use the function sqrt(), which is defined by the
following prototype in the math.h (or cmath ) header file: double sqrt( double x);
Exercises (cont)
EXERCISE 2
The program shown
side contains several
errors!
Correct the errors and
ensure that the program
can be executed.
Exercises (cont)
EXERCISE 3
Create a C++ program that defines a string containing the following
character sequence: