call by value call by reference in c

call by value call by reference in c                                                                                 


Code: 


#include <stdio.h>
#include<conio.h>
void swap(int a,int b);
int main()
{
int a,b;
printf("Enter two Number:");
scanf("%d%d",&a,&b);
printf("The value of a&b before calling the swap function are %d&%d\n",a,b);
swap(a,b);
printf("The value of a&b after calling the swap function are %d&%d\n",a,b);
getch();
return 0;
}
void swap(int a,int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("The Value of a&b in the swap function after swapping are %d&%d\n",a,b);
}

 

Sample Input and Output:

Enter two Number:4                                                                                                                   
3                                                                                                                                    
The value of a&b before calling the swap function are 4&3                                                                            
The Value of a&b in the swap function after swapping are 3&4                                                                         
The value of a&b  after calling the swap function are 4&3       

 

Explanation of above program 

In this article,we are going to see how to write a program to swap two number by call by value method.

What is Call by Value ?

The call by value is a method of passing argument to a function copies the actual value of an argument into the formal parameter of the function.In this Process,the argument(value passed in function call) is known as actual parameter and the A variable and its type as they appear in the prototype of the function or method is called formal parameter.

In this case the values of actual parameters are copied to the formal parameters. Thus operations performed on the formal parameters don’t reflect in the actual parameters.


In the first line of our code,we have simply including stdio header file which stands for standard Input Output header file which help us to use some of the inbuilt function which are defined in stdio.h like printf(),scanf() etc. 

Similiarly,In the next line we have included one more header file conio.h which help us to use clrscr() and getch(),most of you must thinking that what is the use of clrscr() and getch() function and why we need it,So I would like to tell them that In new compiler there is need of clrscr() and getch() but In old and traditional compiler like Turbo C Compiler we used to use clrscr() function to clear the output screen so that when we execute the code again then it should show clear the output of previously executed program.

The getch() is used to hold the screen I mean when we executed the code without getch() in Turbo C Compiler then we are not able to see output  screen because it come and go so fast we are not able to see it.So to overcome this problem we use getch() so that we can see the output properly


In the next line of our code,We have declared one user defined function with name swap and return type void and accepting two parameter of type int a,b.we declare the function to tell the compiler that we are going to define the function further as now just declaring the function.we should declare any user-defined function  outside the main function.function declaration is also known as function prototype.


In the next line  of our code we have defined our main function from which execution of our program will start.In the next line we have declared two variable a and b both variable will be used as input for taking two input value.In the next line we have simply printed one statement 'Enter two number'  this statement will indicate the user that our application is waiting for our input and proceed only when appropriate input.


In the next line we have wrote scanf() function,this is the line of code where actually the execution of program pauses and resume only when the input value is provided,In the scanf we are taking two input value and storing it a and b respectively for both variable we are using %d format specifier since both are integer(int) type variable.

In the next line of our code we have printed one statement 'The value of a&b before calling the swap function are' this statement will be printed as it is,and In this print statement we have also printed the value of a and b.I known most of you must be thinking why we have wrote this statement,what it sense so I just want to tell them that we have wrote this for debugging purpose I mean after swapping we should get to known what is difference between before and after swapping.









If you any have doubt,suggestion regarding this post or website then feel free to share with us.
 
If you have learned something new from this post then share this post with your family and
friends.
 
Happy Learning :)😎  

  Previous Next

Post a Comment

Previous Post Next Post