Program in C to add two numbers using function (Pass by value method)

The below program in C accepts 2 integer numbers and calculates the sum of those 2 numbers using add function.

add function accepts two integer parameters namely nos1 and nos2. This function then calculates the sum and return the result to the calling main function. Once the result is received, main function prints the addition of those two numbers

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

	return 1;
}

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

Output:

Enter the two number: 5 96

Addition of 5 and 96 is 101

You may also like...

4 Responses

  1. laurencev says:

    Would be nice to have syntax highlighting too. Makes a huge difference when learning new language.

  2. Anonymous says:

    1 /* 01_add2num_function.c
    2 * add 2 numbers using function
    3 * pass by refrence
    4 **********************
    5 * I LOVE This Site *
    6 **********************
    7 */
    8
    9 #include
    10
    11 int main()
    12 {
    13 int num1, num2, result;
    14
    15 // get numbers from users
    16 printf(“Please enter 2 numbers: “);
    17 scanf(“%d %d”, &num1, &num2);
    18
    19 // add the numbers using the add() function
    20 result = add(num1, num2);
    21
    22 // print the result to the screen
    23 printf(“%d + %d = %d \n”, num1, num2, result);
    24 }
    25
    26 /* define the add() function
    27 * using reference
    28 */
    29 int add(int no1, int no2)
    30 {
    31 int res;
    32 res = no1 + no2;
    33
    34 return res;
    35 }
    ~

  3. Anonymous says:

    Doing a good job

  1. March 2, 2014

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

Leave a Reply

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