Addition of 2 Numbers using Function: C Program


In this video tutorial lets learn how to add two integer numbers using functions in C programming language.

Related Read:
Function / Methods In C Programming Language
Addition of 2 Numbers: C

Video Tutorial: Addition of 2 Numbers using Function: C Program


[youtube https://www.youtube.com/watch?v=1fmOsKbnTxQ]

YouTube Link: https://www.youtube.com/watch?v=1fmOsKbnTxQ [Watch the Video In Full Screen.]

Source Code: Addition of 2 Numbers using Function: C Program

  1. #include<stdio.h>  
  2.   
  3. int add(intint); // function prototype  
  4.   
  5. int main()  
  6. {  
  7.     int a, b;  
  8.   
  9.     printf("Enter 2 integer numbers\n");  
  10.     scanf("%d%d", &a, &b);  
  11.   
  12.     //function call add(a, b);  
  13.     printf("%d + %d = %d\n", a, b, add(a, b));  
  14.   
  15.     return 0;  
  16. }  
  17.   
  18. //function definition  
  19. int add(int x, int y)  
  20. {  
  21.     return(x+y);  
  22. }  

Output 1
Enter 2 integer numbers
25
25
25 + 25 = 50

Output 2
Enter 2 integer numbers
50
5
50 + 5 = 55

Here we call user defined function add from within printf() function. We pass 2 integer variables as argument to add method. The values of variable a and b are copied to variable x and y. Note that values of variable x and y are local to function add and main function do not know values of x and y. x and y are separate copy and altering which do not alter the value of variables a and b.

a and b are called actual arguments.
x and y are called formal arguments.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert

One thought on “Addition of 2 Numbers using Function: C Program”

Leave a Reply

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