1.Call by Value
2.Call by Reference
Call by Value এবং Call by Reference এর মধ্যে প্রধান পার্থক্য হল Call by Value তে শুধু আসল প্যারামিটার/ আর্গুমেন্টেরই একটা কপি পাস হয়।তাই ফাংশনে পাস হওয়া ঐ কপি প্যারামিটার/ আরগুমেন্টের চেইঞ্জ করলেও আসল প্যারামিটারের কোন চেইঞ্জ হয় না।কিন্তু Call by Reference এ প্যারামিটারের কপি পাস না হয়ে অ্যাড্রেস পাস হয়। তাই ফাংশনের মধ্যে ঐ প্যারামিটারের কোন চেইঞ্জ হলে আসল প্যারামিটারেও সেই চেইঞ্জ হবে।
Example of "Call by Value":
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
void swapByValue(int n,int m){ | |
int tmp; | |
tmp=n; | |
n=m; | |
m=tmp; | |
} | |
int main() | |
{ | |
int n=5,m=10; | |
swapByValue(n,m); | |
printf("%d %d\n",n,m); | |
return 0; | |
} |
Output: 5 10
আসল প্যারামিটারের কোন চেইঞ্জ হয় নাই।
Example of "Call by Reference":
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
void swapByReference(int *n,int *m){ | |
int tmp; | |
tmp=*n; | |
*n=*m; | |
*m=tmp; | |
} | |
int main() | |
{ | |
int n=5,m=10; | |
swapByReference(&n,&m); | |
printf("%d %d\n",n,m); | |
return 0; | |
} |
Output: 10 5
এইখানে আসল প্যারামিটার swap হয়ে গেছে।
No comments:
Post a Comment