Lecture 12 Flashcards
What is Pass-by-Value in parameter passing?
Pass-by-Value copies the actual parameter’s value to the formal parameter. Changes to the formal parameter do not affect the actual parameter. It is used in languages like C, C++, and Java (for primitive types).
What is pass-by-Result in parameter passing?
In Pass-by-Result, the formal parameter acts as a local variable whose value is copied back to the actual parameter when the subprogram exits. Changes to the formal parameter are reflected in the actual parameter after execution.
What is Pass-by-Value-Result in parameter passing?
Pass-by-Value-Result combines pass-by-value and pass-by-result. The actual parameter’s value is copied to the formal parameter at the beginning and back to the actual parameter at the end of the subprogram.
What is Pass-by-Reference in parameter passing?
Pass-by-Reference makes the formal parameter an alias for the actual parameter, allowing changes to the formal parameter to directly affect the actual parameter. Used in C++ (pointers), Java (objects), and C#.
What is Pass-by-Name in parameter passing?
Pass-by-Name directly substitutes the actual parameter into the subprogram, using a late binding mechanism where the expression is evaluated each time it is used. Implemented using thunks.
How are parameters and return addresses managed in stack-based implementation?
Parameters and return addresses are stored on the runtime stack using activation records, which manage information for each active subprogram, including local variables, parameters, return address, and control information.
How are parameters passed in C/C++?
C/C++ use pass-by-value for basic types and pass-by-reference using pointers.
What parameter passing modes does Ada support?
Ada supports in, out, and inout parameter passing modes.
How are parameters passed in Fortran?
Fortran primarily uses pass-by-reference for parameter passing.
What is aliasing and what problems can it cause?
Aliasing occurs when multiple variables reference the same memory location, leading to unexpected side effects and bugs, especially in pass-by-reference and pass-by-name parameter passing.
What are address conflicts in parameter passing?
Address conflicts occur when two parameters reference the same memory location, potentially leading to unintended interactions between parameters.
What are the efficiency concerns with pass-by-value?
Copying large data structures in pass-by-value can be inefficient, making it less suitable for large or complex data.
What are the best practices for choosing parameter passing mechanisms?
Use pass-by-value for small, immutable data, and pass-by-reference for large data structures and when modifications are needed. Avoid pass-by-name unless necessary for specific scenarios.
What are the advantages of pass-by-reference?
Pass-by-reference is efficient because it avoids copying large data structures, but it can lead to aliasing and side effects.