-
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
}
-
How many times does main() call Average2()?
-
Is the function assigned to a variable the first or the second
time that Average2() is called?
-
What happens to the value returned from the function when it
is called the first time?
-
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?
-
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?
-
When calling a function that returns a value, it should be assigned
to a variable or printed. True or false?
-
In Line 5 exercise 2, the value of the function is not assigned
or printed in main(). Why not?
-
Replace Line 1 in Average2() with printf("(x+y) / 2.0 = %.2f\n",
(x+y)/2.0);. Are there any problems? Why?
|

-
Two
times.
-
The
first one in main().
-
The
value has been assigned to Avg variable.
-
The
value got printed or it becomes an argument to printf().
-
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.
-
False.
Not really.
-
It
got printed in the function itself.
-
Yes
there is a warning: "warning C4716: 'Average2' : must return a value".
The Average2() has been declared to return a value of type float.
|
| |
|
-
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);
}
-
When evaluating the value of Avg for the first time, the average
of 10.0 and 30.0 is returned. What is its value?
-
The average of 40.0 and 60.0 is also returned. What is its value?
How is the value of Avg obtained?
-
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?
-
Can you have a function inside another function? If so, which
function’s result is obtained first, the nested one or the outer
one?
-
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?
-
In the last printf(), printf() calls which function? This function
calls which function? Notice that there are multiple nestings
of function calls.
-
How is the value received for the printf() to print in the last
statement in main()?
|

-
20.00.
-
50.00.
The value of Avg=20.00+50.00+1=71.00.
-
Avg=Average2(45,70)=57.50.
-
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.
-
Yes.
-
printf()
calls Average2() and Average2() calls itself.
-
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.
|
| |
|
-
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,
float,
float[ ]);
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;
}

-
How many scalars and how many arrays does main() pass to AverageHi()?
-
What are the values of the scalars in main()? In AverageHi()?
-
What is the name of the array in main()? In AverageHi()?
-
AverageHi() calculates the average of f1 and f2. In which index
of the array is it placed?
-
AverageHi() calculates the larger of f1 and f2. In which index
of the array is it placed?
-
Does AverageHi() calculate two answers, the average and the
largest and make them available to main()?
-
Since a return is not used in Average(), how are these two answers
provided for main()?
|
------------------------------------------


-
Two
scalars and one array.
-
Both
in main() and AverageHi() it is 10.00 and 30.00.
-
In
main() it is Ans[2] and in AverageHi() it is x[2].
-
Index
0 as depicted by Ans[0].
-
Index
1 as depicted by Ans[1].
-
Yes.
-
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.
|
| |
|
-
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));
}
-
main() calls which function inside its printf()?
-
Does AverageTwo() call ReadTwo() or does ReadTwo() call AverageTwo()?
-
There are three functions defined here. Which functions receive
arguments?
-
Which function (or functions) returns values?
-
Which function (or functions) places values in an array?
-
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.
|
----------------------------------------------------------

-
AverageTwo().
-
AverageTwo()
calls ReadTwo().
-
ReadTwo()
receives a float as an argument.
-
AverageTwo()
returns value of type float.
-
ReadTwo()
places values in array A[ ].
-
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
-
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.
|
| |
|
-
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.
|
| |
|
-
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.
|
| |
|
-
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.
|
| |
|
-
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.
|
| |
|
-
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.
|
| |
|
-
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.
}
-
Testing();
-
Testing(i, f);
-
c = Testing(a, f);
-
f = Testing (b[2], c);
-
printf("Testing(b) = %.2f\n", Testing(b));
-
printf("Testing(c) = %d\n", Testing(c));
|
-
void
Testing(void);
-
void
Testing(int, float);
-
char
Testing(int [ ], float);
-
float
Testing(char [ ], char);
-
void
Testing(char [ ]);
-
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.
-
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;
}

|