Functions in C


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.



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