In C, executable statements are written inside a function.
A C function is no more than a collection of statements to perform a specific task.
- Every C program has at least one function called
main()
- Bring modularity to your code with the use of functions.
- Using functions to break down the functionality of a program can make it easier to debug, modify and it increases the maintainability of the code.
- Minimize code size and redundancy.
- Provide abstraction with the use of functions.
In C, functions first have to be declared before they are used.
#include <stdio.h>
int myFunctionPrototype(int x, char chr, float num);
int main(){
int OperationResult;
OperationResult = myFunctionPrototype(32, 'C', 34.2);
return 0;
}
int myFunctionPrototype(int x, char chr, float num){
// do some stuff
return result;
}
A prototype let's the compiler know about:
- The return data type. >>
int
- The arguments list (and their data type). >>
int x
, char chr
, float num
- The order of arguments being passed to the function. >> First an
int
, then a char
and finally a float