C Program for Addition of Two Numbers Using Functions

Here we will write a C program for addition of two numbers using functions. First, we will write a program that has only one user-defined function. Later we will solve the same problem using three functions. The 2nd program teaches you how to write different types of function based on program requirements.

Prerequisites:-
Introduction to Function in C
User-defined Functions in C

C Program for Addition of Two Numbers Using Functions

Program description:- Write a C program to calculate the addition of two numbers using functions.

#include<stdio.h>

// function to find sum of two numbers
float addition(float num1, float num2)
{
   // declare variable
   float sum;

   // calculate sum value
   sum = num1 + num2;

   // return result
   return sum;
}

int main()
{
   // declare variables
   float number1, number2, result;

   // take input
   printf("Enter two number: ");
   scanf("%f %f",&number1, &number2);

   // find addition of two numbers
   result = addition(number1, number2);

   // display result
   printf("%.2f + %.2f = %.2f\n",
               number1, number2, result);

   return 0;
}

Output:-

Enter two number: 12.3 6.5
12.30 + 6.50 = 18.80

In this program, one function addition is defined which takes two floating-point number arguments and also returns floating-point value. In the main, two floating-point numbers are taken from the user and stored in the floating-point variables number1 and number2 respectively.

Later, addition function is called with passing arguments number1, number2. Now, control passed to function and execution of the function started. The value of arguments number1 and numbers are stored in the parameters num1 and num2.

One local variable sum is created and store the sum of parameters. After that sum value is returned to the main function. Control came back to the main function and the returning value from the function is stored in the variable result. Finally, the result is displayed on the screen.

Addition of Two Numbers Using Three Functions

Program description:- Write a C program to calculate addition of two floating-point numbers using functions. Write three functions:- input(), addition(), display(). Take input from user in user-defined function input() and return back to the main function. Add numbers in addition() function and return back to main function. Print the result using display() function.

#include<stdio.h>

// function for taking input from user
float input()
{
   float number; // declare variable

   // take input
   printf("Enter number: ");
   scanf("%f", &number);

   // return input value
   return number;
}

// function to find sum of two numbers
float addition(float num1, float num2)
{
   return num1 + num2;
}

// function for displaying the result
void display(float n1, float n2, float sum)
{
   printf("%.2f + %.2f = %.2f\n", n1, n2, sum);
}

int main()
{
   // declare variables
   float number1, number2, result;

   // take input from end-user
   number1 = input();
   number2 = input();

   // calculate addition of numbers
   result = addition(number1, number2);

   // display results
   display(number1, number2, result);

   return 0;
}

Any Function can return only one value at a time. So, to take two inputs from the user, we need to call input() function two times. The first call of input() returns first value which will be stored in variable number1 inside main function. Similarly, the returned value from the 2nd call stored in variable number2.

[ Note:- Function can return more than one value using array and pointer. Array is an implicit pointer. But now, we don’t know anything about array and pointer. After learning array, try this problem by calling input() function only once.]

Program Explanation

In this program, three user-defined functions are used. The input() function, which takes no arguments from the main method, takes input from the user and returned this value to the main function.

The addition() function takes two arguments because it will add two numbers. To add two numbers, first of all, numbers should be passed to the addition() function. The addition() function takes two arguments, store it in parameter variables, calculate the sum of both numbers and returned results to the main function.

The display() function takes three arguments and displays them. It doesn’t return anything back to the main function so, its return type is void.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

4 thoughts on “C Program for Addition of Two Numbers Using Functions”

  1. Hi there! I simply want to offer you a huge thumbs up for the great info you’ve got here on this post. I will be returning to your website for more soon.

    1. The second program in this post is using three user-defined functions input(), addition(), display(). The input() function doesn’t take any argument and returns one float value. And addition() function takes two floating-point values as argument and return sum value (floating point). Similarly, the display() function takes three arguments but return nothing.

  2. This design is incredible! You most certainly know how to keep a reader amused. Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Great job. I really enjoyed what you had to say, and more than that, how you presented it. Too cool!

Leave a Comment

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