This article is all about how to Find a “Sum Of The Numbers Using Functions In C”. What are the functions? So, the answer is The set of code that sort out or organize your code in the body of a policy. It increases the reusability. You can call user-defined functions multiple times in your program instead of writing code again and avoid too many declarations of the variables. The Functions make your code very easy.
Sum Of The Numbers Using Functions In C:
We are using the 4th type of user-defined functions (with return type and with arguments). The type of function only has the logic of the program inside the function definition’s body. Then, return some value in which we are interested. In the below program, we are passing two arguments which we will initialize through the main.
In the main function, we take two numbers and take their values from the user. After taking the value from the user we have to declare another variable to store the result which will be returning from the sum function. We invoke the functions and pass two arguments this time the arguments are those numbers that are already taken by us from the keyboard and store the result of the function in some variable.
The value of the first variable will be assigned to the first variable (x) and the second value to the second argument of the function (y). After this initialization of the variables of the function, it will now add two numbers and store the result in the sum and then simply return the value of the sum to the main function. In the main function, we store this value in some variable (add) and simply print the value of add to check the sum of two numbers.
Source Code:
#include<stdio.h> int sum(int x , int y); int sum(int x,int y){ int sum; sum = x + y; return sum; } int main(){ int a,b; int add = 0; printf("Enter The First Number : "); scanf("%d",&a); printf("Enter The Second Number : "); scanf("%d",&b); add = sum(a,b); printf("Sum Of Two Numbers = %d",add); return 0; }
Output:
From the above program, we conclude how to write a program to find the sum of the numbers using functions in C language. We were recently published an article C Program to Copy the Elements of One Array into Another Array.