fb popup

/* remove if it's exist in your template */

Parameters Passing

In functions parameters can be passed in two ways:-
  1. Pass by value or Call by Value.
  2. Pass by reference or Call by Reference.
1.Pass by Value:-

this one we already did in the Functions . Any way I will explain this with a simple example:

float sum(float,int);
void main()
float num1,result;
int num2;
printf("Enter float value:");
printf("Enter integer value:");
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;

Pass by value

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

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;


Pass by Reference

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;
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