9.6 The Break Statement
// Demonstration break statement
#include <iostream.h>
main( )
{
int i = 1;
while (i > 0)
{
cout << "count = " << i++ << endl;
/* Break out of the while statement when
i > 5 */
if (i > 5)
break;
}
cout << "End of program";
}
10.0 FUNCTIONS
10.1 What is a function?
A function can be equated to a “Blackbox” to which one or more input values are passed, some processing done, and output value(s) are then returned automatically.
Figure 10 illustrates this sequence when a function named sqrt is called. The value of x (16.0) is the function input, and the function result or output is the square root of 16.0
10.1.1 Functions as Program Building Blocks
Programmers use functions like building blocks to construct large programs. Arguments of a function are used to pass information into the function subprogram from the main function and to return to the calling function multiple results computed by the function subprogram. Arguments that carry information from the main function into the function subprogram are called input arguments or parameters. Arguments that return results to the main function are called output arguments.
C++ Function
A C++ function is a grouping of program statements into a single unit to carry out tasks at a given level. Each C++ function can be activated through the execution of a function call.
Why Use a Function?
a) The use of functions enables us to write our program in logically independent sections in the same way as we develop the solution algorithm.
b) Functions may be executed more than once in a program. Also if you have written and tested a function subprogram, you may use it in other created sub programmes.
10.1.2 Classification of Functions
Functions are of two types. These are
a) Built-in Functions or Library functions
b) User Defined Functions
10.1.3 Built-in Functions
Operations that are frequently used by in a programme like determining the square root of a number, sine value of a number etc, are programmed in the form of standard functions and stored in C++ library, so that they can be called in any program when needed by the programme. These functions are called library function or built-in functions. Table 9 shows some of the commonly used mathematical built-in library functions.
Table 9 Mathematical Built-in Library Functions
Function | Library File | Return value | Argument | Result |
Abs(x) | <stdlib.h> | Returns the absolute value of its integer Argument if x is -5, abs(x) is 5 | int | int |
ceil(x) | <math.h> | Returns the smallest whole number that is not less than x: If x is 45.23, ceil(x) is 46.0 | double | double |
cos(x) | <math.h> | Returns the cosine of angle x: If x is 0.0. cos(x) is 1.0 (radians) | double | double |
exp(x) | <math.h> | Returns ex where e = 2.71828 | double | double |
fabs(x) | <math.h> | Returns the absolute value of its type double Argument: If x is -8.432,fabs(x) is 8.432 | double | double |
floor(x) | <math.h> | Returns the largest whole number that is not greater than x: If x is 45.23, floor (x) is 45.0 | double | double |
log(x) | <math.h> | Returns the natural logarithm of x for x > 0.0 It x is 2.71828, log(x) is 1.0 | double | double |
log10(x) | <math.h> | Returns the base 10 logarithm of x for x> 0.0 | double | double |
pow(x,y) | <math.h> | Returns xy. If x is negative, y must be a whole Number: If x is 0.16 and y is 0.5, pow(x,y) is 0.4 | double | double |
sin(x) | <math.h> | Returns the sine of angle x: if x is 1.5708, sin(x) is 1.0 (radians) | double | double |
sqrt(x) | <math.h> | Returns the non-negative square root of x ![]() If x is 2.25, sqrt(x) is 1.5 | double | double |
tan(x) | <math.h> | If x is 0.0, tan(x) is 0.0 | Double (radians) | double |
Example 10.1:
Write a program to compute the quadratic equation
Not that the roots of the quadratic equation are given by:
The discriminate (disc) is defined as
When the value of the discriminant is greater or equal to zero, the roots of the quadratic equation exist, otherwise the roots are imaginary (complex).
/* A program to computing the roots of a quadratic equation*/
#include<iostream.h>
#include<stdio.h>
#include<math.h>
main()
{
double disc, root_1, root_2;
double a, b, c;
cout << "\n input the values of a, b and c: ";
cin >> a >> b >> c;
disc = pow(b, 2) - 4 * a * c;
if (disc >= 0)
{
root_1 = (-b + sqrt(disc))/(2*a);
root_2 = (-b -sqrt(disc))/(2*a);
cout << "\n Root1 = " << root_1 << endl;
cout << "\n Root2 = " << root_2 << endl;
}
else
{
cout << "\n No real root exists \n";
}
}
10.1.4 User Defined Functions
A part from the built in C++ library functions, users can also define their own functions to do a task relevant to their programs. Such functions are called user-defined functions. These functions are codified by the user, so that such functions can perform the task as desired by the user.
Types of User Defined Functions
The user defined functions may be classified in three types. Each type is based on the formal arguments or parameters passed to the functions and the usage of return statement. These types are:
i. A function is invoked or called without passing any formal argument from the calling portion of a program and also the function does not return any value to the called function.
ii. A function is invoked by the calling program and formal arguments are passed from the calling program but the function does not return any values to the calling program.
iii. A function is invoked with formal arguments from the calling portion of a program which returns value(s) to the calling program.
10.1.5 Advantages of Functions
The following are some advantages of using functions:
a) The complexity of the entire program can be divided into simple subtasks and function subprogram can be written for each subtasks.
b) The subprograms are easier to write, understand and debug.
c) A function can be shared by other programs by compiling it separately and loading them together.
d) In C++, a function can call itself again. It is called recursiveness. Many calculations can be done easily using recursive process such as calculation of factorial of a number.
e) The functions such as cout, cin, the numerical computation functions sin, cos, sqrt etc, are used very frequently. Hence these are kept in C++ library and compiler is written in such a way that any C++ program can call any of the library functions.
10.2 Defining A Function
A function definition has a name, a parentheses pair containing zero or more parameters and a body. For each parameter, there should be a corresponding declaration that occurs before the body. Any parameter not declared is taken to be an int by default. For good programming, one should declare all parameters. Turbo C++ compiler permits including the argument declaration with the parentheses.

No comments:
Post a Comment