Bahria University, Islamabad
Department of Software Engineering
Computer Programming Lab (Fall-2024)
Teacher: Engr. Muhammad Amin Khan
Student : Hayat Nabi
Enrollment : 09-131242-097
Lab Journal: 10
Date: 24-11-2024
Documentation
Task Wise Marks Total
Task Marks Marks
No:
Assigned Obtained Assigned Obtained (20)
1 3
2 3
3 3 5
4 3
5 3
Comments:
Signature
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 10 Dept of SE, BUIC
Lab No: 10 – Recursion & Pass by Reference
Lab Task :
Swapping Two Numbers: Call by Value vs Call by Reference Implement two separate
functions to swap two numbers: one using call by value and the other using call by reference.
The program should ask the user for two numbers, perform the swaps, and display the results
to demonstrate the difference between the two methods.
No.1 : Call By Value
Code:
#include<iostream>
using namespace std;
void PassByValue(int a , int b){ // Through this Function swap will not work
int temp = a;
a = b;
b = temp;
int main(){
int Num1,Num2;
cout<<"Enter First Number : ";
cin>>Num1;
cout<<"Enter Second Number : ";
cin>>Num2;
cout<<"Before Pass By Value Function :\nNum1 : "<<Num1 << "\nNum2 : "<<Num2 <<endl;
PassByValue(Num1 , Num2);
cout<<"After Pass By Value Function : \n";
cout<<"Num1 : " << Num1 <<endl;
cout<<"Num2 : " << Num2<<endl;
Screenshot:
2
Hayat Nabi Computer Programming Engr. M Amin Khan
09-131242-097 Lab # 10 Dept of SE, BUIC
No.2: Call By Reference
Code:
#include<iostream>
using namespace std;
void PassByReference(int &x , int &y){ // It will swap because it reach upto memory address where it
change value
int temp =x;
x=y;
y = temp ;
int main(){
int Num1,Num2;
cout<<"Enter First Number : ";
cin>>Num1;
cout<<"Enter Second Number : ";
cin>>Num2;
cout<<"Before Pass By Reference Function :\nNum1 : "<<Num1 << "\nNum2 : "<<Num2 <<endl;
PassByReference(Num1 , Num2);
cout<<"After Pass By Reference Function : \n";
cout<<"Num1 : " << Num1 <<endl;
cout<<"Num2 : " << Num2;
Screenshot: