# C - Functions

A function is a block of statements which executes only when it is called somewhere in the program. A function provides re-usability of same code for different inputs, hence saves time and resources. There are some in-built functions in C and one of the common function is main() which is used to execute code. Users can also create their own function which is also termed as user-defined functions.

## Create Function

In C, creating a function starts with defining return type of function followed by function's name and parenthesis containing function's parameter(s), if it has any. At last, it contains block of statements which is also called body of the function. Please see the syntax below:

### Syntax

//Defining function
return_type function_name(parameters)
{
statements;
}


return_type: A function can return value(s). The return_type is the data type of the value the function returns. If a function does not return anything in that case void is used as return_type.

### Example: A function with no parameter

In the below example, a function called MyFunction is created to print Hello World!. The function requires no parameters to run and has no return type, hence void keyword is used.

#include <stdio.h>

void MyFunction(){
printf("%s\n","Hello World!.");
}

int main (){
MyFunction();
return 0;
}


Output

Hello World!.


## Call Function

After defining the function, it can be called anywhere in the program with it's name followed by parenthesis containing function's parameter(s), if it has any and semicolon (;). As in the above example, the function is called inside main() function using the following statement:

MyFunction();


## Function Declaration

In C, declaration of a function starts with return type of function followed by function's name and parenthesis containing function's parameter(s), if it has any. Please see the syntax below:

### Syntax

//declaration of a  function
return_type function_name(parameters);


If the function is defined after it is called in the program, the C program will raise an exception. To avoid such situation, the function is declared before calling it, and the function can be defined anywhere in the program.

### Example:

In the below example, the function called MyFunction is defined after it is called in the program. Since the function is defined after it is called in the program, program will raise an exception. To avoid such situation, function must be declared before calling it.

#include <stdio.h>
//function declaration
void MyFunction();

int main (){
//function calling
MyFunction();
return 0;
}
//function definition
void MyFunction(){
printf("%s\n","Hello World!.");
}


Output

Hello World!.


## Parameter

A parameter (or also known as argument) is a variable which is used to pass information inside a function. In above example, the function does not has any parameter. But a user can create a function with single or multiple parameters. Value of a parameter can be further used by the function to achieve desired result.

### Example: A function with parameters

In the below example, the function called MyFunction is created which requires two integer numbers as parameters and print sum of two numbers in desired style. Please note that, the function returns nothing hence void is used as return type.

#include <stdio.h>
void MyFunction(int x, int y);

int main (){
int a = 15, b = 10;
MyFunction(a, b);
return 0;
}

void MyFunction(int x, int y){
printf("Sum of %i and %i is: %i\n", x, y, x+y);
}


Output

Sum of 15 and 10 is: 25


## Function to Return Values

A function can be used to return values. To achieve this, user must have to define return type in definition and declaration of the function. In the below example, the return type is int.

#include <stdio.h>
int MyFunction(int x, int y);

int main (){
int a = 15, b = 10;
int sum = MyFunction(a, b);
printf("%i\n", sum);
return 0;
}

int MyFunction(int x, int y){
return x+y;
}


Output

25


## Recursive function

A function which can call itself is known as recursive function. A recursive function generally ends with one or more boundary conditions.

### Example:

A function for calculating factorial using recursion method is described below.

#include <stdio.h>
int factorial(int x);

int main (){
printf("%i\n", factorial(3));
printf("%i\n", factorial(5));
return 0;
}

int factorial(int x){
if(x==0)
{return 1;}
else
{return x*factorial(x-1);}
}


Output

6
120