Answer to Question #262892 in C for Mr Z

Question #262892

.4. • Enter the code

// Function prototype. Some compilers absolutely insist that function
// prototype(s) are present
void blah2(int *i);


// ===========================================================
// Function blah2()
//
// This takes a reference to a value i, increments it and displays it
//
// Returns the calculated voltage.
// ===========================================================

void blah2(int *i)
{
    printf("In function: the value of *i is %d\n", *i);
    (*i)++;  // Equivalent to *i = *i + 1;
    printf("In function: the value of *i after increment is %d\n\n", *i);
}


int main(int argc, char *argv[]) {
    int num;
    
    num = 3;
    blah2(&num);
    
    printf("In main(): the value of num is %d\n", num);

    system("pause");
	return 0;
}


• Write a short reflective account of the code concentrating on its functionality and comparing the outputs against the source code. Also, comment on the difference between passing parameters to a function by reference and by value.



1
Expert's answer
2021-11-08T17:33:44-0500

The blah2 function in the code takes a value and then increment it. In the code , a value 3 is given this value is passed through the function which increments it to 4 which is displayed as the output.

Pass by value means that a copy of the actual parameter's value is made in memory while pass by reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function so that a copy of the address of the actual parameter is made in memory.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS