0% found this document useful (0 votes)
59 views

Practical 04 - More On Functions

The document discusses functions and parameters in C++ programming. It includes sample code with functions and asks questions to test understanding of concepts like parameter passing, scope of variables, and how functions return values. Sample programs are provided and analyzed to illustrate pass-by-value versus pass-by-address. Questions cover defining functions, function headers, scopes of variables, and how/when function data areas are created and destroyed.

Uploaded by

Jasper Lim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Practical 04 - More On Functions

The document discusses functions and parameters in C++ programming. It includes sample code with functions and asks questions to test understanding of concepts like parameter passing, scope of variables, and how functions return values. Sample programs are provided and analyzed to illustrate pass-by-value versus pass-by-address. Questions cover defining functions, function headers, scopes of variables, and how/when function data areas are created and destroyed.

Uploaded by

Jasper Lim
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Part A (Understanding Concepts)

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 }

2. Correct the errors in the following function definitions.


(a) int func1(int x)
{
cout << "x = " << x << endl;
}

(b) int func2(int x, y)


{
return x + y;
}

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;
}

(a) z = func(2.0, 3, 100);


(b) z = func(double x, int y); // assume x and y are defined

4. Consider the following program:


(a) Identify the names of the local variables used in the functions.
(b) How it is possible for functions main, func_a and func_b to have a variable named a?
(c) Show the values of the variables and formal parameter for the program by filling the table below.
1 #include <iostream>
2 using namespace std;
3
4 int func_a(void);
5 int func_b(int x);
6
7 int main(void)
8 {
9 int a, b;
10
11 a = func_a();
12 b = func_b(a);
13 cout << a << b << endl;
14
15 return 0;
16 }
17
18 int func_a(void)
19 {
20 int a;
21
22 cout << "Enter a: ";
23 cin >> a; // assume the value 23 is entered
24
25 return a;
26 }
27
28 int func_b(int x)
29 {
30 int a, r;
31
32 a = x / 10;
33 r = x - a * 10;
34
35 return r;
36 }

No. Statement executed Data Area for function


main func_a func_b
1. In main: func_a() a b a

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 }

No. Statement executed Variables Output


a b c d r s
1. a=1;
b=2;
c=3;
d=4;
2. r = strange (a, b);
3. cout << "r = " << r << endl;
4. s = strange (r, strange(c, d));
5. cout << "s = " << s << endl;
6. cout << "result = " << strange (r, s) << endl;

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.

Part B (Programming Exercises)


1. Write a program that asks the user to enter a temperature in degrees Celsius and converts it to degrees
Fahrenheit. Formula for conversion to Fahrenheit:
9
f = c +32
5
The program’s design should use main and at least the THREE(3) functions described below:
i. Read a temperature in degrees Celsius from the keyboard.
ii. Convert the temperature in degrees Celsius to degrees Fahrenheit.
iii. Display the results.

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;

void exchange(short x, short y); void exchange(short* x, short* y);

int main(void) int main(void)


{ {
short a, b; short a, b;
a = 5; a = 5;
b = 3; b = 3;
exchange(a, b); // by value exchange(&a, &b); // by address

cout << "a = " << a cout << "a = " << a
<< ", b = " << b << endl; << ", b = " << b << endl;

return 0; return 0;
} }

void exchange(short x, short y) void exchange(short* x, short* y)


{ {
short temp; short temp;
temp = x; temp = *x;
x = y; *x = *y;
y = temp; *y = temp;
return; return;
} }

3. Implement the program described in Part A question 6.

Part C (Self-Review / Revision)


1. How do you pass information to a function when it is called?
2. How does a function give backs its result?
3. What is the general format for a function header?
4. What are the rules for actual and formal parameter list correspondence?
5. When is the data area for a function created? When is the data area destroyed?

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?

Part D (Practice Exercises)

1. (a) Write a function that reads a 4-digit number.


(b) Write a function that returns the first digit in a given 4-digit number. For example, given the integer 4537,
the function returns 4.
(c) Write a function that displays the first digit.
(d) Write a main function to test the functions you wrote in part (a), (b) and (c).

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.

You might also like