Call by value and Call by reference

There are 2 ways that a C function can be called from a program. They are,
1]Call by value
2]Call by reference

1] CALL BY VALUE:

• the value of the variable is passed to the function as parameter.
• The value of the actual parameter are not modified by formal parameter.
• Memory allocation for both actual and formal parameters is different. Because, value of actual parameter is copied to formal parameter.

Note:
• Actual parameter – This is the argument is used in function call.
• Formal parameter – This is the argument is used in function definition

PROGRAM FOR C FUNCTION (USING CALL BY VALUE):

• In this program, the values of the variables “m1” and “n1” are passed to the function “swap1”.
• m1 and n2 are copied to formal parameters “a1” and “b1” in swap function and used.

Example
#include<stdio.h>

void swap1(int a1, int b1);

int main()

{

int m1 = 23, n1 = 43; // calling swap function by value

printf(” values before swaping m = %d \nand n = %d”, m1, n1);

swap1(m1, n1);

return 0;

}

void swap1(int a1, int b1)

{

int tmp1;

tmp1 = a1;

a1 = b1;

b1 = tmp1;

printf(” \nvalues after swaping m1 = %d\n and n1 = %d”, a1, b1);

}

Output
values before swap m = 23 and n = 43

values after swap m = 43 and n = 23

2. CALL BY REFERENCE:

• the address of the variable is passed function as parameter in the call by refereace merhod.
• The value of the actual parameter can be modified by formal parameter.
• memory allocation is same for both actual and formal parameters since only address is used by both parameters.

PROGRAM FOR FUNCTION (USING CALL BY REFERENCE):

• In this call by reference, the address of the variables “m1” and “n1” are passed to the function “swap1”.
• m1 and n1 are not copied to formal parameters “a1” and “b1” in swap function.
• Because, they are just holding the address of those variables.
• This address is used tochange and access the values of the variables.

Example

#include<stdio.h>

// function prototype, also called function declaration void swap1(int *a1, int *b1);

int main()

 

{ int m1 = 23, n1 = 43; // calling swap function by reference

printf(“values before swap m1 = %d \n and n1 = %d”,m1,n1);

swap1(&m1, &n1);

return 0;

}

void swap1(int *a1, int *b1)

{

int tmp1;

tmp1 = *a1;

*a1 = *b1;

*b1 = tmp1;

printf(“\n values after swap a1 = %d \nand b1 = %d”, *a1, *b1);

}

Output

values before swap m = 23 and n = 43

values after swap a = 43 and b = 23

Example #1: No arguments passed to function and does not return Value

#include<stdio.h>

void checkPrimeNumber1();

int main()

{

checkPrimeNumber1();

return 0;

}

void checkPrimeNumber1()

{

int nub1, i1, flag1=0;

printf(“Enter a positive integer: “);

scanf(“%d”,&nub1);

for(i1=2; i1 <= nub1/2; ++i1)

{

if(nub1%i1 == 0)

{

Flag1 = 1;

}

}

if (flag1 == 1)

printf(“given no %d is not the prime number.”, nub1);

else

printf(“given no %d is the prime number.”, nub1);

}

Output

Enter a positive integer:7

given no 7 is the prime number.

Example #2: WAP for function having No arguments passed but a return value

#include<stdio.h>

int getInteger1();

int main()

{

int nub1, i1, flag1 = 0;

n1 = getInteger1();

for(i1=2; i1<=nub1/2; ++i1)

{

if(nub1%i1==0)

{

flag1 = 1; break;

}

}

if (flag1 == 1)

printf(“given no %d is not prime number.”, nub1);

else

printf(“given no %d is prime number.”, nub1);

return 0;

}

int getInteger1()

{

int n1; printf(“Enter a positive integer: “);

scanf(“%d”,&nub1);

return nub1;

}

Output
Enter a positive integer:7

given no 7 is prime number

Example #3: WAP to passed Argument but no return value

#include<stdio.h>

void checkPrimeAndDisplay1(int nub1);

int main()

{

int nub1;

printf(“Enter a positive integer: “);

scanf(“%d”,&nub1);

checkPrimeAndDisplay1(nub1);

return 0;

}

void checkPrimeAndDisplay1(int nub1)

{

int i1, flag1 = 0;

for(i1=2; i1 <= nub1/2; ++i1)

{

if(nub1%i1 == 0)

{

flag1 = 1; break;

}

}

if(flag1 == 1)

printf(” given number %d isn’t a prime number.”,nub1);

else

printf(“given number %d is a prime number.”, nub1);

}

Output

Enter a positive integer:5

given number 5 is a prime number

Example #4: WAP to passed Argument and a return value

#include<stdio.h>

int checkPrimeNumber1(int nub1);

int main()

{

int nub1, flag1;

printf(“Enter a positive integer: “);

scanf(“%d”,&nub1);

flag = checkPrimeNumber1(nub1);

if(flag1==1)

printf(“given no %d isn’t a prime number”,nub1);

else

printf(“given number %d is a prime number”,nub1);

return 0;

}

int checkPrimeNumber1(int nub1)

{

int i1;

for(i1=2; i1 <= nub1/2; ++i1)

{

if(nub1%i1 == 0)

return 1;

}

return 0;

}

Output

Enter a positive integer:43

given number 43 is a prime number

Example#5: Sum of Natural Numbers Using Recursion

#include<stdio.h>

int sum1(int n1);

int main()

{

int number1, result1;

printf(“Enter a +ve integer: “);

scanf(“%d”, &number1);

result1 = sum1(number1);

printf(“sum1=%d”, result1);

}

int sum(int num1)

{

if (num1!=0)

return num1 + sum(num1-1);

else

return num1*9 ;

}

output

Enter a +ve integer: 6

21

 

Leave a Reply

Your email address will not be published. Required fields are marked *