Write a C program For Addition of two numbers using Function.

Table of Contents
< All Topics
Print

Write a C program For Addition of two numbers using Function.

You have done simple additions of two numbers in your initial programming sessions. Here also you have to do the similar task but the addon here is that this task should include one more function (here i have taken a name as chkSum() function).

In this way you will be able to understand the coding in segregated way on each of the four function categories with the help of a simple program of addition of two numbers using functions in C programming.

In each program, three parts (P1, P2 and P3) are mentioned. Where P1 is for all about the input, P2 is for all the manipulation/ processing and P3 is for the result generation. Hope you will learn and enjoy functional programming with this simple program of addition of two numbers using Function.

Type-1: Function with no arguments and no return values.

The algorithm for the code c language mentioned below can be described as follows:

  • Declare a function prototype of chkSum() with no arguments and no return value.
  • Include the standard input/output header file
  • Define the main function.
    • Call the chkSum function.
  • Define the chkSum function.
    • Declare two integer variables a and b.
    • Print a message to enter a and b.
    • Scan the values of a and b from the user input.
    • Declare and initialize an integer variable sum as the sum of a and b.
    • Print the value of sum.
// function type-1: with no arguments and no return values
// Author- milindbhatt @ ezylearning.in
#include<stdio.h>
void chkSum(void); // fun declaration or prototype

int main()
{ 
chkSum();
return 0;
}
// fun def
void chkSum()
{//========================= p1=============================
int a, b;
 printf("\n enter a and b :  ");
 scanf("%d%d", &a,&b);
 //========================= p2=============================
 int sum= a+b;
 //========================= p3=============================
 printf("\n sum = %d", sum);
}

Type-2: Function with no arguments and with return values.

The algorithm for the code c language mentioned below is:

  • Define a function prototype for chkSum() that takes no arguments and returns a float value
  • Include the standard input/output library
  • Declare a float variable add and assign it the value returned by calling function chkSum().
  • Print the value of add with a message
  • Define the main function and call chkSum from it
  • In the chkSum function,
    • declare two integer variables a and b
    • Prompt the user to enter the values of a and b and read them using scanf
    • Declare a float variable sum and assign it the value of a plus b
    • Return the value of sum to the main()
// function type-2: with no arguments and with return values.
// Author- milindbhatt @ ezylearning.in
#include<stdio.h>
float chkSum(void);

int main()
{ 
float add=chkSum();
printf("\n my addition1 = %f",add);

return 0;

}// end main

float chkSum()
{//========================= p1=============================
int a, b;
 printf("\n enter a and b :  ");
 scanf("%d%d", &a,&b);
 //========================= p2=============================
 float sum= a+b;
 // ===========p3==========
 return sum;
}// end func

Type-3: Function with arguments and with no return values.

The algorithm for the code c language mentioned below is:

  • Include the standard input/output header file.
  • Declare a void function named chkSum that takes two integer parameters
  • Define the main function
    • Declare two integer variables x and y
    • Prompt the user to enter the values of x and y
    • Read the values of x and y using scanf function
    • Call the chkSum function with x and y as arguments.
  • Define the chkSum function
    • Declare an integer variable sum and assign it the value of a plus b
    • Print the value of sum using printf function.
// function type-3: Function with arguments and with no return
// Author- milindbhatt @ ezylearning.in
#include<stdio.h>
void chkSum(int a, int b); // fun declaration or prototype

int main()
{ //========================= p1=============================
 int x, y;
 printf("\n enter x and y :  ");
 scanf("%d%d", &x,&y);

chkSum(x,y);
return 0;
} //end main

// fun def
void chkSum(int a, int b)
{
 //========================= p2=============================
 int sum= a+b;
 //========================= p3=============================
 printf("\n sum type-3 = %d", sum);
}

Type-4: Function with arguments and with return values.

The algorithm for the code c language mentioned below is:

  • Declare a function prototype for chkSum that takes a float and an int as parameters and returns a float
  • Include the stdio.h header file
  • Define the main function
    • Declare and initialize three variables: a float a, an int b, and an int c with the value 90
    • Print a message asking the user to enter values for a and b
    • Scan the user input and store them in a and b
    • Call the chkSum function with a and b as arguments and store the return value in a float variable s
    • Print the value of s
  • Define the chkSum function
    • Return the sum of the two parameters x and y
// function type-4 with arguments and with return values
// Author- milindbhatt @ ezylearning.in
#include<stdio.h>
float chkSum( float , int ); // fun declaration or prototype
int main()
{ //========================= p1=============================
float a;
int b,c=90;
 printf("\n enter a and b :  ");
 scanf("%f%d", &a,&b);
 
 float s= chkSum(a,b);
 //========================= p3=============================
 printf("\n sum = %f", s);
 return 0;
}// end main

// def 
float chkSum(float x, int y)
{  //========================= p2=============================
return x+y; 
}

Conclusion

Any c program can be solved via any of the FOUR type/categories of functions. Most of the time user may opt any category of solution as per the demand of the project or as per the instructions of in the problem. Best way to solve is via “with argument and with return” because in this case function is responsible for the demanded processing work only, rest all has to be taken care by the collie function.


Recommended Reading

Leave a comment