Dr.
Ahmed AlKhazzar
Computer Science
Programming 3: Using Functions
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 1
Declaring Functions
The prototype above gives the following information to the compiler:
• func is the function name
• the function is called with two arguments: the first argument is of type int, the
• second of type double
• the return value of the function is of type long.
Ex: double pow(double base, double exponent);
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 2
Mathematical standard functions
• double sin (double); // Sine
• double cos (double); // Cosine
• double tan (double); // Tangent
• double atan (double); // Arc tangent
• double cosh (double); // Hyperbolic Cosine
• double sqrt (double); // Square Root
• double pow (double, double); // Power
• double exp (double); // Exponential Function
• double log (double); // Natural Logarithm
• double log10 (double); // Base-ten Logarithm
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 3
Ex: Calculating powers with the
standard function pow()
#include <iostream> // Declaration of cout
#include <cmath> // Prototype of pow(), thus:
// double pow( double, double);
using namespace std;
int main(){
double x = 2.5, y;
// Computes x raised to the power 3:
y = pow("x", 3.0); // Error! String is not a number
y = pow(x + 3.0); // Error! Just one argument
y = pow(x, 3.0); // ok!
y = pow(x, 3); // ok! The compiler converts the int value 3 to double
cout << "2.5 raised to 3 yields: "
<< y << endl;
// Calculating with pow() is possible:
cout << "2 + (5 raised to the power 2.5) yields: "
<< 2.0 + pow(5.0, x) << endl;
return 0;
}
2.5 raised to the power 3 yields: 15.625
2 + (5 raised to the power 2.5) yields: 57.9017
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 4
Functions without Return Value
• You can write functions that perform a certain action but do not return
a value to the function that called them.
• The type void is available for functions of this type, which are also
referred to as procedures in other programming languages.
Example: void srand( unsigned int seed );
• The standard function srand()initializes an algorithm that generates
random numbers.
• Since the function does not return a value, it is of type void.
• An unsigned value is passed to the function as an argument to seed
the random number generator. The value is used to create a series of
random numbers.
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 5
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 );
The same!
int rand();
• The standard function rand() is called without any
arguments and returns a random number between 0
and 32767.
• A series of random numbers can be generated by
repeating the function call.
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 6
Outputs three random numbers
#include <iostream> // Declaration of cin and cout
#include <cstdlib> // Prototypes of srand(), rand():
// void srand( unsigned int seed );
// int rand( void );
using namespace std;
int main() {
unsigned int seed;
int z1, z2, z3;
cout << " --- Random Numbers --- \n" << endl;
cout << "To initialize the random number generator, "
<< "\n please enter an integer value: ";
cin >> seed; // Input an integer
srand(seed); // and use it as argument for a
// new sequence of random numbers.
z1 = rand(); // Compute three random numbers.
z2 = rand();
z3 = rand();
cout << "\nThree random numbers: "
<< z1 << " " << z2 << " " << z3 << endl;
return 0; --- Random Numbers ---
}
To initialize the random number generator,
please enter an integer value: 7777
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 7
Three random numbers: 25435 6908 14579
Header Files
• Header files are text files containing
declarations and macros.
• By using an #include directive these
declarations and macros can be made
available to any other source file.
• Header files should 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 " ... ".
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 8
Using string in C++
#include <iostream> // Declaration of cin, cout
#include <string> // Declaration of class string
using namespace std;
int main()
{
// Defines four strings:
string prompt("What is your name: "),
name, // An empty string
line( 40, '-'), // string with 40 '-'
total = "Hello ";
cout << prompt;
cin >> name);
total = total + name;
cout << line << endl
<< total << endl;
cout << line << endl;
return 0;
}
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 9
Exercise 1
Create a program to calculate the square roots
of the numbers 4, 12.25, 0.0121
and output them to the screen.
To calculate the square root, use the function
sqrt(), which is defined by the following
prototype in the cmath header file:
double sqrt( double x);
The return value of the sqrt() function is the
square root of x.
Prosthetics and Orthotics Engineering Dept. | Dr. Ahmed AlKhazzar 10