![]() ![]() |
![]() |
||||||||||
![]()
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]()
|
CSET 3150 C++ Functions Divide and ConquerMost computer programs that solve real-world problems are much larger than the short, simple program examples that you find in your textbooks or on this web site. The best way to develop and maintain these larger programs is to modularize them - that is, to divide the overall problem into a number of smaller pieces that are more easily managed. Function CategoriesIn C++, these modules are called functions and classes. C++ programmers create programs by combining new programmer-defined functions with a number of pre-defined functions from the C++ standard library and by combining new programmer-defined classes with pre-defined classes available from a variety of class libraries. The two catagories of C++ functions are illustrated below. They include Library Functions from the C++ Standard Library and Programmer-Defined Functions that are custom functions written to satisfy a particular need. The diagram below illustrates the organization of C++ functions. Note that Library Functions are also called built-in functions. Under Programmer-Defined Functions we have functions that do not return a value (labeled Just Do IT!), functions that return a value, and functions that modify variable values from the calling function. More about all of these in other sections of this page.
Function SyntaxA function is a group of statements that is executed when it is called (invoked) from some point in the program. The following is the basic function syntax:
type name ( parameter1, parameter2, ...)
{
statements
}
where:
Below is an example of a C++ function that calculates the area of a triangle and is illustrative of the basic function syntax.
float triangle(float base, float height)
{
float area; // area of the triangle
area = base * height / 2.0;
return (area);
}
Function PlacementFunctions may appear anywhere in your program. However, the organization shown below is recommended.
Here is a simple C++ program that uses the function above to find the area of three triangles, given the base and the height as part of the function calls:
#include <iostream.h>
using namespace std;
// function prototype (declaration)
float triangle(float base, float height);
int main(void)
{
cout << "Triangle #1 " << triangle(1.3, 8.3) << endl;
cout << "Triangle #2 " << triangle(4.8, 9.8) << endl;
cout << "Triangle #3 " << triangle(1.2, 2.0) << endl;
return (0);
}
// triangle -- compute area of a triangle
//
// Parameters passed
// width -- base of the triangle
// height -- height of the triangle
//
// Returns
// area of the triangle
float triangle(float base, float height)
{
float area; // area of the triangle
area = base * height / 2.0;
return (area);
}
Note the inclusion of comments to describe just what the function does.
When compiled (using g++) and run on the class server (by typing ./triangle at the prompt), the program above produces the following output: et791:~/cset3150$ ./triangle Copy this example to your directory on the class server and try it! Variations on a ThemeHere is another function. This one accepts three numbers of type double and returns the largest (max) of the three.
double maximum( double x, double y, double z )
{
double max = x; // assume x is largest
if ( y > max ) // if y is larger,
{
max = y; // assign y to max
}
if ( z > max ) // if z is larger,
{
max = z; // assign z to max
}
return max; // max is largest value
}
The function above is written in compliance with the coding standards described elsewhere on this website. It is also correct C++ syntax to write it as follows:
double maximum( double x, double y, double z )
{
double max = x;
if ( y > max ) max = y;
if ( z > max ) max = z;
return max;
}
Although this format is more compact, you should follow the coding standards established for this course.
The C++ program below uses the function above to accept three numbers from the user (at the keyboard) and then return the maximum.
// Finding the maximum of three floating-point numbers
#include <iostream>
using std::cout;
using std::cin;
using std::endl;
// function prototype
double maximum( double, double, double );
int main()
{
double number1;
double number2;
double number3;
cout << "Enter three floating-point numbers: ";
cin >> number1 >> number2 >> number3;
// number1, number2 and number3 are arguments to
// the maximum function call
cout << "Maximum is: "
<< maximum( number1, number2, number3 ) << endl;
return 0; // indicates successful termination
}
// function maximum definition
// x, y and z are parameters
double maximum( double x, double y, double z )
{
double max = x;
if ( y > max )
{
max = y;
}
if ( z > max )
{
max = z;
}
return max;
}
In the program above there are some interesting syntax variations that you should notice. First, the using namespace std; statement has been replaced with the following: using std::cout; using std::cin; using std::endl;As you might guess, these three lines simply specify the functions within std that we are using in this program. Without either form of the using statement, we would have to place std:: in front of cout and cin each time they were used in our program. For example: std::cout << "Enter three floating-point numbers: "; std::cin >> number1 >> number2 >> number3;Second, the function prototype statement is a bit different. // function prototype double maximum( double, double, double );In this example, the parameters are idicated only by their data type (i.e., double), and no variable names (identifiers) are included. This syntax is allowed. When run on the class server, this program produces the following interactive output: et791:~/cset3150$ ./maximumNote that the user input three floats separated by whitespace (in this case, spaces). The Enter key could have been used to seperate the numbers. et791:~/cset3150$ ./maximum To Return or Not to ReturnIn C++, functions are of two types: those that return a value and those that simply "do something." The following two examples illustrate this difference.
#include <iostream>
using namespace std;
int square(int i);
int main(void)
{
int num;
int sq;
cout << "Please enter a number: ";
cin >> num;
sq = square(num);
cout << "Square of " << num << " is " << sq;
return 0;
}
int square(int i)
{
int sq = i * i;
return sq;
}
When you require a result from a function (e.g., square root, difference between two numbers, a string that is combined from two other strings, etc.), return a result. When you don't, then return void.
#include <iostream>
using namespace std;
void square(int i);
int main(void)
{
int num;
cout << "Please enter a number: ";
cin >> num;
square(num);
return 0;
}
void square(int i)
{
int sq = i * i;
cout << "Square of " << i << " is " << sq;
}
|
||||||||||
|
|
|
Added to the Web: September 7, 2007. Web page design by Dan Solarek. |
![]() http://cset.sp.utoledo.edu/cset3150/ |