সি প্রোগ্রামিং ল্যাঙ্গুয়েজে পাস বাই রেফারেন্স হল অ্যাড্রেস যা আর্গুমেন্ট হিসেবে পাঠানো হয়।
অ্যালগরিদম
C ল্যাঙ্গুয়েজে পাস বাই মানের কাজ ব্যাখ্যা করার জন্য নিচে একটি অ্যালগরিদম দেওয়া হল।
START Step 1: Declare a function with pointer variables that to be called. Step 2: Declare variables a,b. Step 3: Enter two variables a,b at runtime. Step 4: Calling function with pass by reference. jump to step 6 Step 5: Print the result values a,b. Step 6: Called function swap having address as arguments. i. Declare temp variable ii. Temp=*a iii. *a=*b iv. *b=temp STOP
উদাহরণ প্রোগ্রাম
পাস বাই রেফারেন্স -
ব্যবহার করে দুটি সংখ্যা অদলবদল করার জন্য C প্রোগ্রামটি নিচে দেওয়া হল#include<stdio.h>
void main(){
void swap(int *,int *);
int a,b;
printf("enter 2 numbers");
scanf("%d%d",&a,&b);
printf("Before swapping a=%d b=%d",a,b);
swap(&a, &b);
printf("after swapping a=%d, b=%d",a,b);
}
void swap(int *a,int *b){
int t;
t=*a;
*a=*b; // *a = (*a + *b) – (*b = * a);
*b=t;
} আউটপুট
যখন উপরের প্রোগ্রামটি কার্যকর করা হয়, তখন এটি নিম্নলিখিত ফলাফল তৈরি করে -
enter 2 numbers 10 20 Before swapping a=10 b=20 After swapping a=20 b=10
রেফারেন্স পাস সম্পর্কে আরও জানতে আরেকটি উদাহরণ নেওয়া যাক।
উদাহরণ
রেফারেন্স দ্বারা কল ব্যবহার করে বা রেফারেন্স দ্বারা পাস করে প্রতিটি কলের জন্য 5 দ্বারা মান বৃদ্ধি করার জন্য সি প্রোগ্রামটি নিম্নোক্ত।
#include <stdio.h>
void inc(int *num){
//increment is done
//on the address where value of num is stored.
*num = *num+5;
// return(*num);
}
int main(){
int a=20,b=30,c=40;
// passing the address of variable a,b,c
inc(&a);
inc(&b);
inc(&c);
printf("Value of a is: %d\n", a);
printf("Value of b is: %d\n", b);
printf("Value of c is: %d\n", c);
return 0;
} আউটপুট
যখন উপরের প্রোগ্রামটি কার্যকর করা হয়, তখন এটি নিম্নলিখিত ফলাফল তৈরি করে -
Value of a is: 25 Value of b is: 35 Value of c is: 45