Practical 04 - More On Functions
Practical 04 - More On Functions
1. Consider the following program that finds the average velocity of a particle travelling on a line between points
p1 and p2 in time t1 to t2:
(a) How many parameters does each of the functions have and what are their types, if any?
(b) What is the return type of each of the functions?
(c) Is it valid to write this statement in function main? If no, explain why.
cout << "Average velocity is " << fixed << setprecision(2)
<< find_velocity(4.5, 8.2, 0.0, 10.0) << endl;
(d) Is it valid to write this statement in function main? If no, explain why.
cout << instruct();
(e) Write the program documentation for function find_velocity using comments to describe its purpose and
preconditions.
1 #include <iostream>
2 #include <iomanip>
3 using namespace std;
4
5 void instruct(void);
6 double find_velocity(double p1, double p2, double t1, double t2);
7
8 int main(void)
9 {
10 instruct();
11
12 double ave_velocity;
13 ave_velocity = find_velocity(4.5, 8.2, 0.0, 10.0);
14
15 cout << "Average velocity is " << fixed << setprecision(2)
16 << ave_velocity << endl;
17
18 return 0;
19 }
20
21 double find_velocity(double p1, double p2, double t1, double t2)
22 {
23 return (p2 - p1) / (t2 - t1);
24 }
25
26 void instruct(void)
27 {
28 cout << "This program calculates the average velocity\n";
29 cout << "of a particle travelling on a line between\n";
30 cout << "points p1 and p2 in time t1 to t2\n";
31 }
3. Given the following function definition, what are the errors in the function calls below?
1
double func(double x, int y)
{
return x * y / 100;
}
2
2. In func_a: cin >> a; a b a
3. In func_a: return a; a b
In main: a = func_a();
4. In main: func_b(a) a b x a r
5. In func_b: a = x / 10; a b x a r
6. In func_b: r = x – a * 10; a b x a r
7. In func_b: return r; a b
In main: b = func_b(a);
5. Consider the following program. Show the values of the variables and the output by filling the table below.
1 #include <iostream>
2 using namespace std;
3
4 int strange(int x, int y);
5
6 int main()
7 {
8 int a, b, c, d, r, s;
9 a = 1;
10 b = 2;
11 c = 3;
12 d = 4;
13
14 r = strange(a, b);
15 cout << "r = " << r << endl;
16
17 s = strange(r, strange(c, d));
18 cout << "s = " << s << endl;
19
20 cout << "result = " << strange(r, s) << endl;
21
22 return 0;
23 }
24
25 int strange(int x, int y)
26 {
27 return x + y;
28 }
3
6. Draw a structure chart with data flows for a program that has the following functions:
main – contains variables dividend, divisor, quotient, and remainder and calls
the 3 functions below.
get_data – reads the values for the dividend and divisor.
divide – computes the quotient and remainder of the dividend divided by the divisor.
print_results – displays the quotient and the remainder.
2. Run the two programs below and observe the output. The first program uses the parameter passing method
pass-by-value and the second program uses pass-by-address. Also note data type short is used.
#include <iostream> #include <iostream>
using namespace std; using namespace std;
cout << "a = " << a cout << "a = " << a
<< ", b = " << b << endl; << ", b = " << b << endl;
return 0; return 0;
} }
4
6. What is the purpose of the concept of scope? What is the scope of a variable or parameter declared in a
function? What is the scope of a function prototype?
7. What are the two parameter-passing methods used in C/C++ programs? What are the differences between the
two methods?
8. Suppose a function reads two values from the user. How can the function pass back these two values to the
calling function?
9. What is the * symbol used for in a C/C++ program?
2. The distance that a car (undergoing constant acceleration) will travel is given by the expression below where
S = distance traveled, V = initial velocity, t = time travelled, and a = acceleration.
1
S=Vt + at 2
2
(a) Write a function that reads value for initial velocity, time travelled, and acceleration.
(b) Write a function that computes the distance traveled where the values of V, t and a are the parameters.
(c) Write a function that displays the result.
(d) Write a main function to test the functions you wrote in part (a), (b) and (c). It calls the function to ask the
user for values of V, t, and a, calls the function to compute the distance travelled and calls the function to
display the result.