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
Would be nice to have syntax highlighting too. Makes a huge difference when learning new language.
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 }
~
Doing a good job
write a program in c ++ add two numbers by passing parameters by reference
You should find the solution here – https://www.learnconline.com/2010/04/program-c-to-add-two-numbers-using-call-by-reference.html
Write a program to add two numbers using four functions and only pass by reference in input function in c language