lect4_1

Predefined Functions
1
Predefined Functions
The programs that we have seen so far are very simple ones. In larger and more
complicated programs, we have to divide the problem into smaller subtasks and then
implement each subtask as a function.
Instead of dealing with the complexity of the entire program all at once, a good plan of
attack is to divide the task into a few smaller and more manageable subtasks, decompose
each of these subtasks further into smaller subtasks, and so forth. Eventually the subtasks
become so small that they are very easy to implement in C++. This method is called topdown design. The top-down method is also sometimes referred to as stepwise
refinement.
Maintaining the top-down structure makes it easy • to
write a program.
• to test a program.
• to debug a program.
• to understand a program functionality.
• to modify a program if there is a need for it.
Functions
• A function is a named block of code.
• It is a small program that performs some well-defined subtask.
• Like a program, a function is usually given some data to accomplish the subtask.
• A function can have its own variables.
We will discuss two types of functions in detail.
1.
Functions that compute a single value based on the data they are given and then
return that value. Such functions are called value-returning functions.
2.
Functions that perform a specific task, based on the data they are given, but do
not return any value.
Such functions are called void functions.
Predefined Functions
2
We begin with some predefined functions that are packaged in the C++ standard library.
Later we discuss how we can create our own functions.
Predefined functions
• C++ provides several predefined mathematical functions. These functions are available
in the library file cmath.
• Any program that uses a function from a library file must have an include statement
specifying that library file. Otherwise, the compiler will be unable to identify the
function.
• A program using any of the basic mathematical functions from the library file cmath
requires the following include statement.
#include <cmath>
Function call, passing arguments, and returning values
• A function call is a program statement that transfers control to a function.
Let us consider the following program which uses the sqrt function, available in the header file cmath,
and assigns the square root of 25 to the double variable dval.
#include <iostream>
#include <cmath>
using namespace std;
int main()
{
double dval;
dval = sqrt(25.); // function call
cout << "square root of 25 is " << dval << endl;
dval = 10 * sqrt(36.); // function call in expression
cout << "10 times square root of 36 is " << dval;
return 0;
}
• Any value that a function call provides to the function is referred to as an argument of
the function call.
Predefined Functions
3
• The result computed and returned by the function is called the return value.
In our example program, in the function call
dval = sqrt(25.);
the argument is 25 and the return value is 5.
• The general form for a function call is the name of the function, followed by parentheses
that contains a list of zero or more arguments.
• Commas separate multiple arguments as shown below:
name_of_function(arg_l, arg_2, ... arg_n);
• An argument in a function call can be a variable, a constant, or an expression.
Thus the following calls of the sqrt are all valid.
sqrt(25.) // argument is a constant
sqrt(dval) // argument is a variable
sqrt(10 * dval + 7) // argument is an expression
• The sqrt function is defined in the cmath header file.
• The argument to this function is any value of type double.
• However, the function accepts any int, long, or float argument as well. In such cases,
the argument will be automatically type cast to a double and then supplied to the
function.
• The sqrt function returns a double value, which is the square root of the argument
value.
A few predefined functions are given in the following table.
Some predefined functions
Return Type
double
Function
Name
sqrt
Arguments
Type
double
Header File
Example
cmath
sqrt(4.0)
Return
Value
2.0
Predefined Functions
double
int
long
double
double
double
pow
abs
labs
fabs
ceil
floor
4
double
int
long
double
double
double
cmath
cstdlib
cstdlib
cmath
cmath
cmath
pow(2., 3.)
abs(-5)
labs(-5)
fabs(-5.4)
ceil(5.1)
ceil(5.9)
8.0
5
5
5.4
6.0
5.0
The pow function: The pow function has two arguments. The first argument is the base
and the second is the power.
For example, pow (2, 5) returns the value 32 (= 25), and pow(10, -2)
returns 0.01 (= 10−2).
The absolute value functions: Three different functions can be used to calculate
absolute value. These are abs, labs, and fabs. These functions take arguments of type int,
long, and double, respectively, and return a result of the same type as the argument.
• Note that calls such as abs(-999000000000) and labs(-1.234) are essentially
meaningless because the arguments are not of the correct types.
The ceil function: The ceil function returns the smallest integer not less than its
argument. For example, ceil(2.1) and ceil(3.0) both return the same value, that is 3.0
The floor function: The floor function returns the largest integer not greater than its
argument. For example, floor(2.1) and floor(2.9) both return the same value, that is 2.0
Predefined character functions
We discussed some predefined functions that operate with numeric data. C++ provides
several useful predefined functions that operate on characters as well. We describe these
functions next.
We often want to convert lowercase letters to uppercase or vice versa in applications such
as word processing. C++ library cctype provides predefined functions to accomplish things
like this and more.
Let us look at some predefined functions that we can use from the header file cctype.
Note that any program that uses the functions in this library, must contain the following
include directive.
Predefined Functions
5
#include <cctype>
The toupper function: This function can be used to convert a lowercase letter to an
uppercase letter. Consider the code segment
char symbol;
symbol = toupper(’a’);
cout << symbol; // outputs: A
symbol = toupper(’A’);
cout << symbol; // outputs: A
symbol = toupper(’*’);
cout << symbol; // outputs: *
From the above code segment, it is clear that toupper(’a’) returns ’A’ and if the argument
to the function is anything other than a lowercase letter, then toupper simply returns the
argument unchanged.
The tolower function: This function is similar to toupper except that it converts an
uppercase letter to its lowercase version as displayed in the following example.
char symbol;
symbol = tolower(’A’);
cout << symbol; // outputs: a
symbol = tolower(’a’);
cout << symbol; // outputs: a
symbol = tolower(’*’);
cout << symbol; // outputs: *
A note on toupper and tolower functions
he C++ language considers characters as integers in the range 0 to 255 (see Week 2 Part
2). We have seen before that each character is represented using its ASCII code whose
range is between 0 to 255.
Therefore, in C++ we can use a value of type char as a number, by assigning it to an int
variable. We can also store an int in a variable of type char (provided the number is
between 0 to 255). Thus, the type char can be used as the type for characters or as a type
for small integers.
Predefined Functions
6
In general, we are not concerned with this detail and can simply consider values of type
char as being characters and not worry about their use as numbers. However, when using
the functions in cctype, this detail can be important.
• The toupper and tolower actually return values of type int but not type char. So, these
functions return the corresponding ASCII code number of the character, rather than the
character itself.
Consider the following code segment:
char symbol;
symbol = (char) tolower(’A’); // int 97 cast to char and stored in symbol
cout << symbol << " " << tolower(’A’); // outputs: a 97
cout << toupper(’a’) << " " << (char) toupper(’a’); // outputs: 65 A
The following table display some of the most commonly used character functions from the header file
cctype. For all of these functions the actual type of the argument is int, but for most purposes we can
consider the argument type as char.
Some character functions
Function Name
isalpha(char)
Return Type Description
bool
Returns true if the argument is an upper or lower case letter.
Otherwise returns false.
isdigit(char)
bool
Returns true if the argument is a digit. Otherwise returns false.
isalnum(char)
bool
Returns true if either isalpha or isdigit is true for the argument.
Otherwise returns false.
isspace(char)
bool
Returns true if the argument is a white space character (blank, tab,
new line).
Otherwise returns false.
ispunct(char)
bool
Returns true the argument is a printable character, and if both
isalnum and isspace are false for the argument. Otherwise returns
false.
Predefined Functions
7
tolower(char)
int
Returns the lower case version of the argument. If there is no
lower case for the argument, returns the argument unchanged.
toupper(char)
int
Returns the upper case version of the argument. If there is no
upper case for the argument, returns the argument unchanged.