Parameter passing in C: “Functions can be called either by value or by reference.” Decide whether this statement is correct or incorrect.
-
ACorrect
-
BIncorrect
-
COnly correct for structs
-
DOnly correct for arrays
-
EDepends on the optimizer
Answer
Correct Answer: Incorrect
Explanation
Introduction / Context:This concept separates C from languages that support true pass-by-reference. In C, parameters are always passed by value. Reference-like behavior is achieved by passing the address of an object (a pointer), which is still passing a value (the address).
Given Data / Assumptions:
- Standard ISO C semantics: arguments are evaluated, converted, and their values are copied into parameters.
- No C++ references exist in C.
- Arrays in parameter lists decay to pointers to their first elements, still following pass-by-value of the pointer.
Concept / Approach:“Pass by reference” means the callee receives an alias to the caller’s variable such that assignments in the callee directly modify the caller’s variable without indirection. C does not provide this. Instead, you pass a pointer (by value) and dereference it inside the function to modify the caller’s object.
Step-by-Step Solution:Call style: foo(x) copies x; changes in foo cannot affect the caller’s x.Reference-like pattern: foo(&x) copies the pointer value; inside foo, p = new_value changes caller’s x.Therefore, the statement claiming both value and reference calling is incorrect.
Verification / Alternative check:Write two functions: one that takes int and tries to modify it (no effect), and one that takes int and modifies *p (visible at caller). The second is still pass-by-value of a pointer.
Why Other Options Are Wrong:Correct/arrays/structs: arrays and structs can be passed, but always by value (arrays decay to pointers).Optimizer: has no bearing on language semantics.
Common Pitfalls:Equating “can modify caller’s variable via pointer” with pass-by-reference; assuming arrays are passed by reference.
Final Answer:Incorrect