xxxxxxxxxx
returnType functionName(type1 argument1, type2 argument2, )
{
//body of the function
}
xxxxxxxxxx
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
xxxxxxxxxx
#include <stdio.h>
// Here is a function declaraction
// It is declared as "int", meaning it returns an integer
/*
Here are the return types you can use:
char,
double,
float,
int,
void(meaning there is no return type)
*/
int MyAge() {
return 25;
}
// MAIN FUNCTION
int main(void) {
// CALLING THE FUNCTION WE MADE
MyAge();
}
xxxxxxxxxx
// main.c
#include <stdio.h>
int main(void) {
//calling function from sayHello.c
sayHello();
return 0;
}
// sayHello.c
#include <stdio.h>
void sayHello() {
printf("Hello World!");
}
xxxxxxxxxx
#include <stdio.h>
void functionName()
{
..
..
}
int main()
{
..
..
functionName();
..
..
}
xxxxxxxxxx
A function is a self-contained block of statements that perform a
coherent task of some kind. Every C program can be thought of as
a collection of these functions