Question

In: Computer Science

(a) Discuss how an actual parameter is related to its corresponding formal parameter in the following...

(a) Discuss how an actual parameter is related to its corresponding formal parameter in the following four parameter-passing
methods for procedure calls.
a. Pass by value
b. Pass by reference
c. Pass by name
(b) Given the output of the following program (written in C syntax) using the above three parameter-passing methods:
int i, j, a[5]; // a is a 5 element array with indices 0 - 4
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main() {
for (j=0; j<5; j++)
a[j] = j;
i = 1;
swap(i, a[i+1]);
printf("%d %d\n", i, a[2]);
return 0;
}
Assume that procedure arguments are evaluated from left to right. What numbers does the program print if both parameters
in swap are passed by
a. value?
b. Reference?
c. Name?
Note that you can get partial credit if you show your hand evaluation of the code.

Solutions

Expert Solution

( a )  an actual parameter is related to its corresponding formal parameter

a . Pass by value

The changes made to formal parameter does not get transmitted back to the calling function. Any changes made to the formal parameter variable inside the called function will not be reflected in the actual parameter in the calling function or method.

b . Pass by reference

The changes made to formal parameter does get transmitted back to the caller function . Any changes made to the formal parameter are reflected in the actual parameter in the calling function.

This is done because formal parameter receives a reference to the actual data and passed to the function.

c. Pass by name

In this parameter passing, symbolic “name” of a variable is passed to the calling function, which allow to be access and update in the same function.

( b )

int i, j, a[5]; // a is a 5 element array with indices 0 - 4
void swap(int x, int y) {
int temp = x;
x = y;
y = temp;
}
int main() {
for (j=0; j<5; j++)
a[j] = j;
i = 1;
swap(i, a[i+1]);
printf("%d %d\n", i, a[2]);
return 0;
}

The program print if both parameters in swap are passed by :

a) Value :

OUTPUT is : 1 2

b) Reference :

OUTPUT is : 2 1

c ) Name :

OUTPUT is : 2 1


Related Solutions

Discuss the importance of sampling, and articulate how sampling is related to parameter estimation. Provide practical...
Discuss the importance of sampling, and articulate how sampling is related to parameter estimation. Provide practical examples that support your arguments (e.g. examples of how appropriate sampling can provide better parameter estimates for your population you are studying).
Discuss the Older Adult Age Related Changes to oxygenation and corresponding nursing strategies for them. How...
Discuss the Older Adult Age Related Changes to oxygenation and corresponding nursing strategies for them. How does this relate to you as a nurse?
1. What does it mean to pass a parameter when the formal parameter is some type of class?
JAVA QuestionsAnswer both questions explaining what each is and using an example to back up your explanation.1. What does it mean to pass a parameter when the formal parameter is some type of class?2. What does it mean to pass a parameter when formal parameter is some type of interface?
1.) Discuss the rules involving actual parameter lists, including matching data types and a one-to-one correspondence...
1.) Discuss the rules involving actual parameter lists, including matching data types and a one-to-one correspondence with the formal parameter list. Note that the parameter list can be empty, but the function call will still require parentheses after the function name. (2) Discuss scope rules with respect to global and local identifiers, nested blocks, and functions. (3) Discuss how local declarations are stored in computer memory. Are there any reasons to avoid using local declarations if it is possible to...
Contigo Company provided the following information related to its inventory sales. Actual – December: 80,000 Budgeted-January:...
Contigo Company provided the following information related to its inventory sales. Actual – December: 80,000 Budgeted-January: 140,000 Budgeted-February: 180,000 Budgeted-March: 120,000 Contigo desired ending inventory levels are 20% of the following months projected COGS. Budgeted purchases of inventory in February 2017 would have been:
array • First, create a function called addNumber, which has a formal parameter for an array...
array • First, create a function called addNumber, which has a formal parameter for an array of integers and increase the value of each array element by a random integer number between 1 to 10. o Add any other formal parameters that are needed. • Second, create another function called printReverse that prints this array in reverse order. • Then, you need to write a C++ program to test the use of these two functions.
using the complementary principalthat the cell structure is related to its fiction, discuss how the shapes...
using the complementary principalthat the cell structure is related to its fiction, discuss how the shapes of the nerve cell and muscle cell are specific to their respective functions
Sensor Specification: 1- What's the parameter related to how close your sensor readings are from a...
Sensor Specification: 1- What's the parameter related to how close your sensor readings are from a reference method readings? 2- What's the parameter related to the dispersion of your sensor's multiple readings in a single simple?
Why is it often impossible to know the actual value of any population parameter? Explain and...
Why is it often impossible to know the actual value of any population parameter? Explain and offer at least two examples of a population parameter that you cannot calculate, but that you can estimate. A sample can be used to estimate a population parameter. How does the sample size affect the estimate? To estimate a population parameter (such as the population mean or population proportion) using a confidence interval first requires one to calculate the margin of error, E. Why...
Why is it often impossible to know the actual value of any population parameter? Explain and...
Why is it often impossible to know the actual value of any population parameter? Explain and offer at least two examples of a population parameter that you cannot calculate, but that you can estimate. A sample can be used to estimate a population parameter. How does the sample size affect the estimate? To estimate a population parameter (such as the population mean or population proportion) using a confidence interval first requires one to calculate the margin of error, E. Why...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT