C : Programming Language

Functions

Function is a set of instructions or block of code which is written to perform a task and given a name to be identified as what it has been intended to do. Name of function is used to call it when it is required. It could take input parameters to perform on and could return the output. Say for example we want to write a perform a task of adding two numbers and get the result after addition.

Example:

printf("Hello World")
If you remember our first example was to print "Hello World" on console and there we used printf function as above.

Function Definition

To define a function we need three things. First thing is name of the function second thing is input parameters and third one is return type. For example we want to define a function which will add two integers. Lets name this function as add and parameters as a and b of type Int. Below is the add function defined which takes two Integers and returns the sum of both the Integers.

int add(int a, int b) {
return a+b;
}

We use return type as int is return type in above example then name of the function which is add then in brackets the input parameters which are a and b here. You can see int is written before a and b which indicated that input parameters a and b here are of type int. Return type int means that this function will return some value as the output and that would of of type int.

Function Call

To call a function when we need, we simply need to call it using it's name and provide required parameters. Also if function is returning some value then we need to hold that in suitable data storage. Lets call our add function.

sum = add(10, 20)