| Main |< C/C++ Functions With Return Values Part 4 | C/C++ Functions With Return Values Part 6 >| Site Index | Download |


 

 

 

 

 

C LAB WORKSHEET 13a

C & C++ Functions Part 5: With Return Values

 

 

 

 

 

 

  1. Functions and array data types.

  2. More on C/C++ functions that receive and/or return values.

  3. Tutorial references that should be used together with this worksheet are: C and C++ function part 1, function part 2, function part 3 and function part 4.

  4. For main() function complete story please refer to C and C++ main() story.

 

 

 

 

 

 

 

 

 

 

 

 

  1. The following example shows two ways to call Average() function. Show the output and answer the questions.

 

#include <stdio.h>

 

float Average2(int, int);

 

void main(void)

{

   float Avg;

 

   Avg = Average2(30, 20);

 

   printf("Average = %.2f\n", Avg);

   printf("Average2(30, 40) = %.2f\n", Average2(30, 40));

}

 

float Average2(int x, int y)

{

   return (float)((x+y) / 2.0);   // Line 1

}

  1. How many times does main() call Average2()?

  2. Is the function assigned to a variable the first or the second time that Average2() is called?

  3. What happens to the value returned from the function when it is called the first time?

  4. What about the second time, when Average2() is called? What happens to the value returned from that function if it is not assigned to Avg?

  5. Add this line between the printf()’s in the main().

Average2(10, 50);

 

Does the program run? If so, does it print the average of 10 and 50? Why or why not?

  1. When calling a function that returns a value, it should be assigned to a variable or printed. True or false?

  2. In Line 5 exercise 2, the value of the function is not assigned or printed in main(). Why not?

  3. Replace Line 1 in Average2() with printf("(x+y) / 2.0 = %.2f\n", (x+y)/2.0);. Are there any problems? Why?

  1. Two times.

  2. The first one in main().

  3. The value has been assigned to Avg variable.

  4. The value got printed or it becomes an argument to printf().

  5. The program runs smoothly. However it does not print the average of 10 and 50 because the returned average value does not assigned to any variable to be used later or as an argument to any function.

  6. False. Not really.

  7. It got printed in the function itself.

  8. Yes there is a warning: "warning C4716: 'Average2' : must return a value". The Average2() has been declared to return a value of type float.

   
  1. In the following exercise, there are other methods for calling Average2(). Watch the parentheses. In this exercise there are three printf()’s. Show the output and answer the questions.

 

#include <stdio.h>

 

float Average2(float, float);

 

void main(void)

{

   float Avg;

 

   Avg = Average2(10.0, 30.0) + Average2(40.0, 60.0) + 1;

   printf("Average = Average2(10.0, 30.0) + Average2(40.0, 60.0) + 1 = %.2f\n", Avg);

 

   Avg = Average2(Average2(20.0, 70.0), Average2(50.0, 90.0));

   printf("Average = Average2(Average2(20.0, 70.0), Average2(50.0, 90.0)) = %.2f\n", Avg);

 

   printf("Average = Average2(10.0, Average2(30.0, 50) = %.2f\n", Average2(10.0, Average2(30.0, 50)));

}

 

float Average2(float x, float y)

{

   return (float)((x+y) / 2.0);

}

  1. When evaluating the value of Avg for the first time, the average of 10.0 and 30.0 is returned. What is its value?

  2. The average of 40.0 and 60.0 is also returned. What is its value? How is the value of Avg obtained?

  3. The second time that Avg is assigned a value, the average of 20.0 and 70.0 is received first, then the average of 50.0 and 90.0 is received. How is the value of Avg obtained?

  4. Can you have a function inside another function? If so, which function’s result is obtained first, the nested one or the outer one?

  5. Are the values received from the nested call (such as the value received from the average of 20.0 and 70.0) used to call the outer one?

  6. In the last printf(), printf() calls which function? This function calls which function? Notice that there are multiple nestings of function calls.

  7. How is the value received for the printf() to print in the last statement in main()?

 

  1. 20.00.

  2. 50.00. The value of Avg=20.00+50.00+1=71.00.

  3. Avg=Average2(45,70)=57.50.

  4. Yes we can. The nested result is obtained first. If there are more than one nested functions the evaluations are from the innermost to the outermost.

  5. Yes.

  6. printf() calls Average2() and Average2() calls itself.

  7. The printf() calls Average2(). Inside the Average2() there is another call to itself, Average2(). The result from the final Average2() has been used for the outer Average2(). Then the returned result from this Average2() has been used as a printf() argument.

   
  1. Function can’t return more than one value but it can choose from several values to be returned. In the following program AverageHi() calculates the average and the larger of two numbers and makes these two values available to main() via an array. In the provided diagram, the address FF00 is fictitious. Fill in the values in as many places in the diagram as you can. Don’t forget to show the output before you answering the questions.

 

#include <stdio.h>

 

void AverageHi(float, floatfloat[ ]);

 

void main(void)

{

   float Ans[2];

 

   AverageHi(10.0, 30.0, Ans);

   printf("The average is %.2f\n", Ans[0]);

   printf("The largest is %.2f\n", Ans[1]);

}

 

void AverageHi(float f1, float f2, float x[2])

{

   x[0] = (f1 + f2) / 2;

   if(f1 > f2)

         x[1] = f1;

   else

         x[1] = f2;

}

 

  1. How many scalars and how many arrays does main() pass to AverageHi()?

  2. What are the values of the scalars in main()? In AverageHi()?

  3. What is the name of the array in main()? In AverageHi()?

  4. AverageHi() calculates the average of f1 and f2. In which index of the array is it placed?

  5. AverageHi() calculates the larger of f1 and f2. In which index of the array is it placed?

  6. Does AverageHi() calculate two answers, the average and the largest and make them available to main()?

  7. Since a return is not used in Average(), how are these two answers provided for main()?

 

 

------------------------------------------

  1. Two scalars and one array.

  2. Both in main() and AverageHi() it is 10.00 and 30.00.

  3. In main() it is Ans[2] and in AverageHi() it is x[2].

  4. Index 0 as depicted by Ans[0].

  5. Index 1 as depicted by Ans[1].

  6. Yes.

  7. When we pass an array to a function, a reference or pointer to the array was passed. When we change or manipulate the array in the function, it effects the original array. Then, when main() refer to the original array, those values are the current values.

   
  1. As a conclusion, if more than one answer has to be returned from a function, then those answers can be placed in an array that was passed from the calling function. One function can call a second function, which can call a third function. Enter 10.0 and 20.0 for the following example.

 

#include <stdio.h>

 

// prototypes

float AverageTwo();

void ReadTwo(float [ ]);

 

void main(void)

{

   printf("Average of two numbers is %.2f\n", AverageTwo());

}

 

float AverageTwo()

{

   float x[2];

   ReadTwo(x);

   return (float)((x[0] + x[1]) / 2.0);

}

 

void ReadTwo(float A[ ])

{

   printf("Enter two floats: \n");

   // for older compiler you can use scanf("%f %f", &A[0], &A[1])

   scanf_s("%f %f", &A[0], &A[1], sizeof(A), sizeof(A));

}

  1. main() calls which function inside its printf()?

  2. Does AverageTwo() call ReadTwo() or does ReadTwo() call AverageTwo()?

  3. There are three functions defined here. Which functions receive arguments?

  4. Which function (or functions) returns values?

  5. Which function (or functions) places values in an array?

  6. Number the sequence of the following events.

_______ - ReadTwo() scans two numbers in an array.

_______ - AverageTwo() returns the average of floats stored in its array back to main()

_______ - AverageTwo() calls ReadTwo() passing an array (actually the address of the array)

_______ - main() prints the average.

_______ - main() calls AverageTwo(), passing no arguments.

_______ - ReadTwo() receives the address of an array into A from the calling function.

 

 

----------------------------------------------------------

  1. AverageTwo().

  2. AverageTwo() calls ReadTwo().

  3. ReadTwo() receives a float as an argument.

  4. AverageTwo() returns value of type float.

  5. ReadTwo() places values in array A[ ].

  6. The sequences:

   4     - ReadTwo() scans two numbers in an array.

   5     - AverageTwo() returns the average of floats stored in its array back to main()

   2     - AverageTwo() calls ReadTwo() passing an array (actually the address of the array)

   6     - main() prints the average.

   1     - main() calls AverageTwo(), passing no arguments.

   3     - ReadTwo() receives the address of an array into A from the calling function.

   

More Questions on Functions

 

  1. Show the output for the following program. What do you know about the return statement in the Socks() function body?

 

#include <stdio.h>

 

char Socks(char [ ]);

 

void main(void)

{

      char x;

      x = Socks("Black");

      printf("x = %c\n", x);

}

 

char Socks(char knee[ ])

{

      return (knee[0]);

}

 

 

The return statement return the first element of the array, denoted by the knee[0], that is a B character from Black string.

   
  1. You are reading in “Yellow”, “Red” and “Brown”.  Why the output is like that? Why not the whole words displayed?

 

#include <stdio.h>

 

char Socks(char [ ]);

 

void main(void)

{

      int i;

      char x, str[20];

      printf("Enter 3 colors: \n");

      for(i = 1; i <= 3; ++i)

      {

            // for older compiler you may use scanf("%s", &str)

            scanf_s("%s", &str, 20);

            x = Socks(str);           // Line 1

            printf("x = %c\n", x);  // Line 2

      }

}

 

char Socks(char knee[ ])

{

      return (knee[0]);

}

 

Similar to the previous example, the return statement just return denoted by the knee[0], the first element of the string though the whole string has been passed to the Socks() function through a pointer.

   
  1. For question 2, show the output if Line 1 and 2 are combined as shown below and you can delete the variable x.

 

printf("Socks(str) = %c\n", Socks(str));

 
 

#include <stdio.h>

 

char Socks(char [ ]);

void main(void)

{

    int i;

    char str[20];

    printf("Enter 3 colors: \n");

    for(i = 1; i <= 3; ++i)

    {

        // for older compiler you may use scanf("%s", &str)

        scanf_s("%s", &str, 20);

        printf("Socks(str) = %c\n", Socks(str));

    }

}

char Socks(char knee[ ])

{

    return (knee[0]);

}

 

 

In this example the Socks() has been called from printf() function.

   
  1. You are reading in 4, 3 and 7 line by line. What is this program tries to show?

 

#include <stdio.h>

 

int MoreSocks(void);

 

void main(void)

{

      int Sum = 0, i;

      for(i = 1; i <= 3; ++i)

            // notice here, summing the function

            Sum = Sum + MoreSocks();

      printf("Sum = %d\n", Sum);

}

 

int MoreSocks(void)

{

      int x;

      printf("Enter an integer: \n");

      scanf_s("%d", &x, 1);

      return (x);

}

 

The return value from the MoreSocks() has been summed up.

   
  1. Just show the output. What this program tries to show to us?

 

#include <stdio.h>

 

int JustFun(int x);

 

void main(void)

{

      printf("JustFun(5) = %d\n", JustFun(5));

      printf("JustFun(JustFun(5)) = %d\n", JustFun(JustFun(5)));

      printf("JustFun(5) + JustFun(5) = %d\n", JustFun(5) + JustFun(5));

      printf("JustFun(JustFun(JustFun(5))) = %d\n", JustFun(JustFun(JustFun(5))));

}

 

int JustFun(int x)

{

      return (x + x);

}

 

This program example shows a nested function, function in another function. The innermost function will be evaluated first and then toward the outermost. In this case the same function was used that show a recursive function where the same function calls itself.

   
  1. Show the output for the following program. What this program tries to demonstrate to us?

 

#include <stdio.h>

 

int Remainder(int a, int b);

 

void main(void)

{

      int x = 3, y = 8;

      for( ; y >= x ; )

            y = Remainder(x, y);

      printf("y = %d\n", y);

}

 

int Remainder(int a, int b)

{

      return (b - a);

}

 

 

 

The Remainder() function calculate the difference between the two given numbers when the second number is grater than the first number.

 

 

 

 

 

 

 

  1. For the given main(), calls to function are given below. These calls will be placed where the comment is shown. By observing how the calls are made in each part, write the function prototype so that the data type match.

 

void main(void)

{

      int i = 7, a[4];

      float f = 7.5;

      char c = 'R', b[4];

     

      // place a function call here.

}

 

  1. Testing();

  2. Testing(i, f);

  3. c = Testing(a, f);

  4. f = Testing (b[2], c);

  5. printf("Testing(b) = %.2f\n", Testing(b));

  6. printf("Testing(c) = %d\n", Testing(c));

 

  1. void Testing(void);

  2. void Testing(int, float);

  3. char Testing(int [ ], float);

  4. float Testing(char [ ], char);

  5. void Testing(char [ ]);

  6. void Testing(char);

   

Write Programs Using Functions

 

Write a complete program that include the main() and the functions needed to test each of the following program.

  1. The function Largest() is to receive three floats and return the largest of them.

 

#include <stdio.h>

 

float Largest(float, float, float);

 

void main(void)

{

    float p, q, r, s;

   

    printf("Enter three floats: ");

    // scanf("%d%d%d", &p, &q, &r);

    scanf_s("%f%f%f", &p, &q, &r, 1,1,1);

    s = Largest(p, q, r);

    printf("The largest of %.2f, %.2f, %.2f is %.2f\n", p, q, r, s);

}

 

float Largest(float x, float y, float z)

{

    float Largest = 0;

   

    if((x > y) & (x > z))

        Largest = x;

    if((y > z) & (y > x))

        Largest = y;

    if((z > y) & (z > x))

        Largest = z;

    return Largest;

}

 

 

 

 

 

 

 

www.tenouk.com

 

| Main |< C/C++ Functions With Return Values Part 4 | C/C++ Functions With Return Values Part 6 >| Site Index | Download |


The C & C++ Functions With Return Values: Part 1 | Part 2 | Part 3