Lab P3 β Implement call by value, call by address, and call by reference.
Aim
Aim: Demonstrate call by value, address, and reference.
Theory
Demonstrate call by value, address, and reference.
C++ Program
#include
using namespace std;
void byValue(int a){ a=100; }
void byAddress(int* a){ *a=100; }
void byRef(int& a){ a=100; }
int main(){
int x=10; byValue(x); cout<<"after value: "< Expected Output
after value: 10
after address: 100
after ref: 100Explanation
- byValue doesn't change original.
- byAddress changes via pointer.
- byRef changes via reference.
Viva / Important Questions
Short Questions
- Define call by value/address/reference.
- When use each?
Long Questions
- Explain the program in detail.
- Discuss key concepts demonstrated.
MCQ Practice
1. Call by value passes:
- Copy
- Address
- Reference
- Nothing
Answer: Copy.
2. Call by address uses:
- Pointer
- Reference
- Copy
- None
Answer: Pointer.
3. Call by reference uses:
- Alias
- Copy
- Pointer
- None
Answer: Alias.
4. Which can modify original? (value)
- No
- Yes
- Maybe
- Always
Answer: No.
5. Reference must be:
- Initialized
- Optional
- Null
- None
Answer: Initialized.
Quick Revision
- Value: copy (no change).
- Address/Ref: modify original.