In functions parameters can be passed in two ways:-
this one we already did in the Functions . Any way I will explain this with a simple example:
2.Pass by Reference:-
In Pass by Reference you need not have to return a value to called function as here we use pointers* concept which we will cover later in upcoming pages.
Here the reference value is passed to called function so when ever there is a change in value of a variable automatically the variable in main() will be affected
i.e. when we change the value of variable in function the value in main() will also be changed
Ex:-
Explanation:- You might not understand this concept completely but for now keep this in mind that when you pass a variable using '&' and by receiving that value in called function using '*' then any change made to variable in called function causes a change in value in the main() function.
1.While declaring the function you need to declare it as (int *) as the values are taken as pointers in the Called function.
2.The values 'a' and 'b' are sent to swap function which are stored in 'c' and 'd'.
- Pass by value or Call by Value.
- Pass by reference or Call by Reference.
this one we already did in the Functions . Any way I will explain this with a simple example:
#include<stdio.h> float sum(float,int); void main() { float num1,result; int num2; printf("Enter float value:"); scanf("%f",&num1); printf("Enter integer value:"); scanf("%d",&num2); result=sum(num1,num2); printf("The sum of %f and %d is %f",num1,num2,result); } float sum(float x,int y) { float z=(float)(x+y); return z; }Output:-
2.Pass by Reference:-
In Pass by Reference you need not have to return a value to called function as here we use pointers* concept which we will cover later in upcoming pages.
Here the reference value is passed to called function so when ever there is a change in value of a variable automatically the variable in main() will be affected
i.e. when we change the value of variable in function the value in main() will also be changed
Ex:-
#include<stdio.h> void swap(int *,int *); //declaration void main() { int a=5,b=6; printf("Values before swapping a=%d,b=%d\n",a,b); swap(&a,&b); //calling function printf("Values after swapping a=%d,b=%d\n",a,b); } void swap(int *c,int *d) //called function { int r; r=*c; *c=*d; *d=r; }Output:-
Explanation:- You might not understand this concept completely but for now keep this in mind that when you pass a variable using '&' and by receiving that value in called function using '*' then any change made to variable in called function causes a change in value in the main() function.
1.While declaring the function you need to declare it as (int *) as the values are taken as pointers in the Called function.
2.The values 'a' and 'b' are sent to swap function which are stored in 'c' and 'd'.
3.using variable 'r' as we know that a=5,b=6 then r=a==>r=5; c=d==>c=6; d=r==>d=5; as 'c' and 'd' are pointers* a change in them causes change in 'a' and 'b' so are the values of 'a' and 'b' Note:*--Covered in later pages.
No comments:
Post a Comment