It is important to me that you understand what it means to pass by value and wha
ID: 3842657 • Letter: I
Question
It is important to me that you understand what it means to pass by value and what it means to pass information to a function whose change by the called function is visible to the caller.
The Solution Requirements
Your solution will entail answering 4 questions:
1. What is meant by "an argument is passed by value" and what is the implication of passing by value?
2. Is an array passed by value? Is a struct passed by value?
3. How is it possible for a function to change the value of a variable it receives?
4. Explain how the C-string function strcmp() does it's processing. What are it's return values and what do they mean?
5. What is a function pointer and how could it prove useful?
Provide text that answers each of these questions.
Explanation / Answer
1.When an argument is passed by value a copy of its value is made or created and is passed to the called method. Changes to the copy do not affect the variable itself.Besides it has a disadvantage is that,if a large data item is being passed, copying that data can take a considerable huge amount of execution time and memory space too.
2. An array can be passed to a function by declaring the value in the called function the array name with square brackets attached to the end. When calling the function, simply pass the address of the array which implies an array's name to the called function.
Passing structure to a function can be done in 2 ways.They can be passed by either value or by reference in the sense with address.
3.For a function in order to change the value of a variable that it receives, it should use "Call by reference".
4.The strcmp() compares two strings character by character. If the first character of two strings are equal, next character of two strings are compared. This continues until the corresponding characters of two strings are different or a null character '' is reached. It is defined in "string.h" header file.
Syntax: int strcmp (const char* str1, const char* str2);
The following are the return values of the strcmp() function.They are:
0--------------------------->if both string are identical i.e.,equal
negative-------------------->if the ASCII value of first unmatched character is less than second.
positive integer------------>if the ASCII value of first unmatched character is greater than second.
5.The use of function pointer is for setting up the "callback" functions that are invoked when a particular event happens. Then the function is called and observes that something which means some event has been taken place.
Syntax: void (*foo)(int);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.