This repository contains a simple C program that demonstrates the use of functions by adding two integers and printing the result.
A Function is a piece of code which will not run by its own. But when called the code in the function will be executed to give some output.
#include <stdio.h>
int add(int a, int b){
return a+b;
}
int main(){
int result;
result = add(5+4);
printf("%d",result);
return 0;
}
For writing a code more beautifuly we may utilize function prototypes.
In function prototypes the function is declared before the int main while the function be written below int main
This makes the code more readable
#include <stdio.h>
//this is a function prototype
int add(int a, int b);
int main(){
int result;
result = add(5+4);
printf("%d",result);
return 0;
}
int add(int a, int b){
return a+b;
}
Function Declaration:
The functionadd
is declared at the beginning of the program with a signatureint add(int a, int b);
. This informs the compiler that a function named add will be used and that it takes two integer arguments and returns an integer.Main Function:
Themain
function calls theadd
function with arguments5
and10
, stores the result in thesum
variable, and then prints the result.Function Definition:
Theadd
function is defined after themain
function. It takes two integersa
andb
, adds them together, and returns the result.