Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(C) 1.Why would you choose to write a function that computes a single numeric or

ID: 3559143 • Letter: #

Question

(C) 1.Why would you choose to write a function that computes a single numeric or character value as a non void function that returns a result through a return statement rather than to write a void function with an output parameter? 2.Explain the allocation of memory cells when a function is called. What is stored in the function data area for an input parameter? Answer the same question for an output parameter.

3. Present arguments against these statements:

a. It is foolish to use function subprograms because a program written with functions has

many more lines than the same program written without functions.

b. The use of function subprograms leads to more errors because of mistakes in using

argument lists.

4. Which of the functions in the following program outline can call the function grumpy? All function prototypes and declarations are shown; only executable statements are omitted.

int grumpy(int dopey);

char silly(double grumpy);

double happy(int goofy, char greedy);

int
main(void)
{
double p, q, r;
. . .
}

int
grumpy(int dopey)
{
double silly;
. . .
}

char
silly(double grumpy)
{
double happy;
. . .
}

double
happy(int goofy, char greedy)
{
char grumpy;
. . .
}

Explanation / Answer

C 1. I would prefer returning value using return statement rather than having an output parameter, becasue Output parameter will works on address of the original variable and it may change it's value in many ways which we might not want. So to prevent unwanted changes in the value of original variable, it would be safe to use return statement and use non void function.

C. 2 when the function call takes place, data elements are stored on the stack in the following way:

The function parameters are pushed on the stack before the function is called. The parameters are pushed from right to left.

The function return address is placed on the stack by the x86 CALL instruction, which stores the current value of the EIP register.

Then, the frame pointer that is the previous value of the EBP register is placed on the stack.

If a function includes try/catch or any other exception handling construct such as SEH (Structured Exception Handling - Microsoft implementation), the compiler will include exception handling information on the stack.

Next, the locally declared variables.

Then the buffers are allocated for temporary data storage.

Finally, the callee save registers such as ESI, EDI, and EBX are stored if they are used at any point during the functions execution. For Linux/Intel, this step comes after step no. 4.


if we have an output parameter instead of input parameter then the copy of passed arguments is not to be made in function stack rather than we can work on the passed address.


3a- No. Subprograms written with functions will always have less lines because if we have to call the same set of statements many times then without using functions we would have need to write the same set of statements at every place. thus increasing the size of code.

3b. yes if we don't know how to pass arguments to a function then it may lead to many typ eof errors for example if our function takes parameters passed by address or reference and we try passing arguments by value then it would lead us to errors

4. every function in above code can call function grumpy() because grumpy has already been declared above and then also defined below in the program . so anyone can call this function.