UncleCoder.com

UncleCoder.com

Free programming examples and instructions

C program to find sum of two numbers using function

C program to find sum of any two numbers using function

by Krishna viswambharan


Posted on 13 Sep 2017 Category: C Views: 181

Edited on 27 Sep 2017



A function is a self-contained program segment that carries out some specific, well-defined task.  When a program calls a function, the program control is transferred to the called function. A called function performs a defined task and when its return statement is executed.

General form of a function definition in C programming language is as follows −

return_type function_name( parameter list ) {
   body of the function
}

A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.

Below is the C code to implement the sum of two numbers using function.

#include <stdio.h>
#include <conio.h>
int sum(int,int);  //function prototype or function declaration
void main()
{
    int x,y,s;
    printf(“enter the first number :”);
    scanf(“%d”,&x);
    printf(“enter the second number :”);
    scanf(“%d”,&y);
    s=sum(x,y);       //function call
    printf(“sum=%d”,s);
    getch();
}


int sum(int p,int q)    //function definition
{
   int c=0;
   c=p+q;
   return(c);
}

output



Leave a Comment:


Advertisement