0% found this document useful (0 votes)
14 views

Algoritma & Pemrograman 02 Using Functions and Classes v1.2

This document discusses functions and classes in C++. It covers declaring functions, function prototypes, function calls, functions without return values or arguments, using header files, and standard classes. Examples are provided of function prototypes, calls, declaring functions without return values or arguments, using header files, and a program using the string class. Exercises are included to write programs using functions like sqrt() and manipulating strings.

Uploaded by

J3ane_Titahena
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views

Algoritma & Pemrograman 02 Using Functions and Classes v1.2

This document discusses functions and classes in C++. It covers declaring functions, function prototypes, function calls, functions without return values or arguments, using header files, and standard classes. Examples are provided of function prototypes, calls, declaring functions without return values or arguments, using header files, and a program using the string class. Exercises are included to write programs using functions like sqrt() and manipulating strings.

Uploaded by

J3ane_Titahena
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 27

Algoritma & Pemrograman

Using Functions and Classes

Prepared by
Frans Panduwinata, S.Kom, M.T
Declaring Functions

• Each name (identifier) occurring in a program must be


known to the compiler or it will cause an error message.

• Each time a variable or a function is defined it is also


declared.

• A function has a name and a type, much like a variable.


Declaring Functions (cont)

• The function’s type is defined by its return value, that is,


the value the function passes back to the program.

• In addition, the type of arguments required by a function is


important.
Function Prototype

• When a function is declared, the compiler must therefore


be provided with information on
– The name and type of the function and
– The type of each argument.

This is also referred to as the Function Prototype.


Function Prototype (cont)
Function Prototype (cont)
• EXAMPLES Functions Prototype:
int toupper(int);
double pow(double, double);

• EXAMPLES Functions:
int toupper(int c);
double pow(double base, double exponent);
Function Prototype (cont)

• If the header file is included in the program’s source code


by means of the #include directive, the function can be
used immediately.
EXAMPLES :
#include <cmath>

Following this directive, the mathematical standard functions, such as


sin(), cos(), and pow(), are available.
Mathematical Standard Functions
Function Calls

• A Function Call is an expression of the same type as the


function and whose value corresponds to the return value.
EXAMPLE:
y = pow( x, 3.0);

EXAMPLE:
cout << 2.0 + pow( 5.0, x);
Function Calls (cont)

• The compiler refers to the prototype to check that the


function has been called correctly.

• If the argument type does not match exactly to the type


defined in the prototype, the compiler performs type
conversion, if possible.
EXAMPLE:
y = pow( x, 3); // also ok!, actually function expects a
// double value,
Function Calls (cont)

• EXAMPLE :
float x = pow(3.0 + 4.7); // Error!

The compiler recognizes that the number of arguments is incorrect.


In addition, the compiler will issue a warning, since a double, i.e. the
return value of pow(), is assigned to a float type variable.
Sample Program
Functions without Return Value

• The type void is available for functions that perform a


certain action but do not return a value to the function
that called them.
EXAMPLE:
void srand( unsigned int seed );

The function srand() is used to generates random numbers.


An unsigned value is passed to the function as an argument to seed
the random number generator.
Functions without Arguments

• If a function does not expect an argument, the function


prototype must be declared as void or the braces following
the function name must be left empty.
EXAMPLE:
int rand( void ); // or int rand();

The standard function rand() is called without any arguments and


returns a random number between 0 and 32767.
Usage of srand() and rand()

• The function prototypes for srand() and rand() can be


found in both the cstdlib and stdlib.h header files.

• Calling the function rand() without previously having called


srand() creates the same sequence of numbers.

• You must call srand() with a different value for the


argument whenever the program is run.
Sample Program
Using Header Files

• Pay attention to the following points when using header


files:
– Header files should generally be included at the start of a
program before any other declarations

– 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)

• angled brackets < ... >, search for


header files in the include folder only.

• double quotes, search for current


project folder.
EXAMPLE:
#include "project.h"
Standard Class Definitions

• The header files also contain standard class definitions.

• When a header file is included, the classes defined and


any objects declared in the file are available to the
program.
EXAMPLE:
#include <iostream>
using namespace std;

cin is an object of the istream class and cout an object of the


ostream class.
Standard Class Definitions (cont)

EXAMPLE without namespace: EXAMPLE with namespace:

#include <iostream> #include <iostream>


using namespace std;
int main ()
{ int main ()
std::cout << "Hello world!\n"; {
return 0; cout << "Hello world!\n";
} return 0;
}
Header Files of the C++ Standard Library
Header Files of the C Standard Library
Using Standard Classes

• Several classes are defined in the C++ standard library.

• These include stream classes for input and output, but also
classes for representing strings or handling error
conditions.

• Each class is a type with certain properties and


capacities.
Sample Program Using Class string
Exercises

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:

I have learned something new again!

and displays the length of the string on screen.

Read two lines of text from the keyboard.


Concatenate the strings using " * “ to separate the two parts of the string.
Output the new string on screen.

You might also like