Functions that return values

An example of a function that returns a value is the sqrt() function. The sqrt() function accepts a single parameter between its brackets (the number to root) and returns the actual root of the number.

Here's an example usage of the sqrt function:

cout << sqrt( 4 ) << endl;

The sqrt() function does something analogous to what the chef did when preparing the pizzas.

As a caller of the function, you do not care about what goes on inside the body of the sqrt() function; that information is irrelevant since all you want is the result of the square root of the number that you are passing.

Let's declare our own simple function that returns a value, as shown in the following code:

int sum(int a, int b)
{
  return a + b;
}

The following screenshot shows the anatomy of a function with parameters and a returned value:

Functions that return values

The sum function is very basic. All it does is take two int numbers a and b, sums them up together, and returns a result. You might say that we don't even need an entire function just to add two numbers. You're right, but bear with me for a moment. We will use this simple function to explain the concept of returned values.

You will use the sum function in this way (from main()):

int sum( int a, int b )
{
  return a + b;
}
int main()
{
  cout << "The sum of 5 and 6 is " << sum( 5,6 ) << endl; 
}

For the cout command to complete, the sum( 5,6 ) function call must be evaluated. At the point where the sum( 5,6 ) function call occurs, the returned value from sum( 5,6 ) is put right there.

In other words, this is the line of code that cout actually sees after evaluating the sum( 5,6 ) function call:

cout << "The sum of 5 and 6 is " << 11 << endl;	

The returned value from sum( 5,6 ) is effectively cut and pasted at the point of the function call.

A value must always be returned by a function that promises to do so (if the return type of the function is anything other than void).

Exercises

  1. Write an isPositive function that returns true when the double parameter passed to it is indeed positive.
  2. Complete the following function definition:
    // function returns true when the magnitude of 'a'
    // is equal to the magnitude of 'b' (absolute value)
    bool absEqual(int a, int b){
        // to complete this exercise, try to not use
        // cmath library functions
    }
  3. Write a getGrade() function that accepts an integer value (marks out of 100) and returns the grade (either A, B, C, D, or F).
  4. A mathematical function is of the form f(x) = 3x + 4. Write a C++ function that returns values for f(x).

Solutions

  1. The isPositive function accepts a double parameter and returns a boolean value:
    bool isPositive( double value )
    {
      return value > 0;
    }
  2. The following is the completed absEqual function:
    bool absEqual( int a, int b )
    {
      // Make a and b positive
    if( a < 0 )
        a = -a;
      if( b < 0 )
        b = -b;
      // now since they're both +ve,
      // we just have to compare equality of a and b together
      return a == b;
    }
  3. The getGrade() function is given in the following code:
    char getGrade( int grade )
    {
      if( grade >= 80 )
        return 'A';
      else if( grade >= 70 )
        return 'B';
      else if( grade >= 60 )
        return 'C';
      else if( grade >= 50 )
        return 'D';
      else
        return 'F';
    }
  4. This program is a simple one that should entertain you. The origin of the name function in C++ actually came from the math world, as shown in the following code:
    double f( double x )
    {
      return 3*x + 4;
    }
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.160.181