Program in C to add numbers using call by reference

This program in C will accept 2 numbers and perform addition of two numbers using call by reference method.

The below program accepts 2 numbers from the user and stores the value in num1 and num2. The program then makes a call to add function. We are passing address/reference of num1 and num2 as function parameters and hence the name, call by reference (also known as – pass by reference)

#include <stdio.h>
#include <stdlib.h>

int main(){

	int num1, num2, result;


	/*Accept the numbers from the user*/
	printf("\nEnter the two number: ");
	scanf("%d %d", &num1, &num2);

	/* Pass the value of num1 and num2 as parameter to function add.
	    The value returned is stored in the variable result
	 */

	result = add(&num1, &num2);

	printf("\nAddition of %d and %d is %d", num1, num2, result);

}

/*Defining the function add()*/
int add(int *no1, int *no2)
{
	int res;
	res = *no1 + *no2;
	return res;
}

Output:

Enter the two number: 10 20

Addition of 10 and 20 is 30

You may also like...

7 Responses

  1. Anonymous says:

    please post more programs.. mostly with pointer..
    i want to write every program with ponters….

  2. Anonymous says:

    thanx author.really help me

  3. Write a program to find out the table of a given no. using array of structure….pls answer this question

  4. burhan uddin says:

    #include
    #include

    void table(int n,int start,int end)
    {
    int i;
    for(i=start; i<=end; i++)
    printf(“%d x %d = %d \n”,n,i,n*i);

    }

    main ()
    {
    int n,start,end;
    printf(“enter table to generate:\n “);
    scanf(“%d”,&n);
    printf(“from which number you want to start your table:\n”);
    scanf(“%d”,&start);
    printf(” At which number you want to end your table:\n “);
    scanf(“%d”,&end);
    table(n,start,end);
    getche();

    }
    #rekha

  5. C programmer says:

    We can use pointers to add two numbers. We will pass base address of both operand.

  1. July 5, 2014

    […] Program to add two numbers using function (Pass by Reference) […]

  2. August 22, 2015

    […] Few more examples: Program to add two numbers using function (Pass by value method) Program to add two numbers using function (Pass by Reference) […]

Leave a Reply

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