Question

In: Computer Science

(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 achieve the same result without them?

Solutions

Expert Solution

note: The answers are with respect to C++ language.

answer 1:

FORMAL PARAMETERS

formal parameters are nothing but the data types and variable names that are written in the definition of the function.

example: int my_function(int x, int y)

any number of formal parameters can be there.

the variables should be used in the same name and type as they appear in the formal parameter list.

formal parameters represent a general input to the function on which it operates

formal parameters are mentioned after the function name within parenthesis

if there are more than one formal parameters, then they are separated by comma

each formal parameter is mentioned along with its data type

formal parameter list can be empty, in which case empty parenthesis should be written after the function name

ACTUAL PARAMETERS

actual parameters are nothing but the variables or the expressions that provide the value to formal parameters of the function

example: cout << my_function(10,30) << endl;

example:

int a = 10;

int b = 30;

int c = my_function(a, b);

int d = my_function(a, a+b);

actual parameters are mentioned when the function is being called.

actual parameters need to be of the same type as that of the formal parameters in the definition of the function

actual parameters need to be mentioned in the exact same order in the parenthesis as that of the formal parameters in the definition of the function

actual parameters are written in parenthesis after the function name

if there are more than one actual parameters, then they are separated by comma

if there is no actual parameter, then empty parenthesis is written after the function name

data types of actual parameters are not written in the parenthesis

actual parameters represent a specific input to the function

CORRESPONDENCE BETWEEN FORMAL AND ACTUAL PARAMETERS

as mentioned earlier, formal parameters are a general form whereas the actual parameters are a specific form of input to the function

formal parameters are mentioned during function definition while actual parameters are mentioned during function call

the working of the function is defined with respect to the formal parameters while the actual operation happens on the corresponding actual parameters during function call

for example:

____________________

int sum(int x, int y)

{

int z = x + y;

return z;

}

int main()

{

int a = 10;

int b = 30;

int c = sum(a, b);

return 0;

}

____________________

The function definition in this example defines that the function takes two parameters, both of type int, and the first parameter is named x while the second parameter is named y.

the function definition defines that it will return an integer z whose value will be equal to the sum of x and y i.e. the sum of two integer input parameters.

during function call, actual parameters a and b are passed to the function, both of them are of type int.

so the variable a corresponds to variable x

similarly the variable b corresponds to y

answer 2:

scope of a variable is nothing but the code area where the variable is valid or applicable.

LOCAL

local variables are the ones that are defined within a function or a block

these can only be accessed within the function or block in which they are declared

GLOBAL

global variables are the ones that are declared at the beginning of the program (outside any function)

they can be accessed anywhere

LOCAL VS GLOBAL

two variables with same name cannot be defined in the same code area i.e function or block because it will give compilation error

however, a local variable with same name as that of a global variable is allowed.

in this case, if we use the variable name to access it, then the program automatically gives precedence to the local variable

if we want to access global variable in such a case, the operator :: should be used.

example:

int x = 100;

int main()

{

int x = 500;

cout << x << endl; // local

cout << :: x << endl; // global

return 0;

}

answer 3:

any local variable when declared in a function or a block, is allocated an appropriate amount of memory on the function stack.

whenever the control of the program execution reaches out of the block or whenever the function returns, this space is deallocated.

this allocation and deallocation is a process that needs to be done every time the control goes inside the function or block and every time the control gets out of it.

it is better to have global variables since, they will be allocated memory at the beginning of the program execution and deallocated after the program ends in one go


Related Solutions

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...
(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 =...
The best quote is one which the exporter lists different pricesfor different Incoterms rules, allowing...
The best quote is one which the exporter lists different prices for different Incoterms rules, allowing the importer to choose the best option for his specific case. a. TRUE. b. FALSE.Special English is used for written communications, just as International English is practiced. a. TRUE. b. FALSE.
If you were to create a social media policy including Slack, what types of rules would...
If you were to create a social media policy including Slack, what types of rules would you include and why? Write three rules and explain the reasoning behind them. Do you think the use of technology can more often than not help or hinder communication among individuals and groups?
One of the approaches the ATO uses to ensure compliance is data matching. What is it...
One of the approaches the ATO uses to ensure compliance is data matching. What is it and how does maintaining records assist the taxpayer in complying with the requirements of the ATO? 80–120 words In contect of Australia , Please do not copy paste from other source
Identify the four types of nonexchange transactions and discuss the rules for recognition of revenues and...
Identify the four types of nonexchange transactions and discuss the rules for recognition of revenues and expenses/expenditures for each type of transaction.
This data table lists the most common types of cancer in the United States, and includes...
This data table lists the most common types of cancer in the United States, and includes information on the known or likely carcinogens, estimated cases per year, and estimated deaths per year in 2014. Cancer in the United States (Ranked by number of cases) Cancer Known or Likely Carcinogenes or Factors Estimated cases (2014) Estimated deaths (2014) Breast Estrogen; possibly dietary fat 235,000 40,400 Prostate Testosterone; possibly dietary fat 233,000 29,500 Lung Cigarette smoke 224,000 159,000 Colon and rectum High...
Discuss each of the three (3) Rules of Risk Management. Select one (1) of the 4...
Discuss each of the three (3) Rules of Risk Management. Select one (1) of the 4 boxes of the frequency/severity matrix (p. 59 of fundamentals of risk and insurance, 11th edition) and identify a peril that fits into that box and how a risk manager might handle that risk.
Mergesort is known to be one of the fastest algorithms for sorting lists of data. In...
Mergesort is known to be one of the fastest algorithms for sorting lists of data. In fact, the runtime of mergesort is proportional to n· logn where n is the size of the list to be sorted. Bubblesort, on the other hand is a much slower algorithm, since it's runtime is proportional to n2 , where n is the size of the list to be sorted. As an experiment, mergesort is run on a slow computer, bubblesort is run on...
Discuss different types of shock including pathophysiology, symptoms, and treatment?
Discuss different types of shock including pathophysiology, symptoms, and treatment?
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT