In: Computer Science
(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?
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