C Tutorial C References

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. C has many built-in functions 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.

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 ;.

Syntax

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

//Calling function
function_name(parameters);

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 example below, a function HelloWorld is created to print Hello World!. It requires no parameters to execute and has no return type, therefore void keyword is used.

#include <stdio.h>
 
void HelloWorld(){
  printf("%s\n","Hello World!"); 
}

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

The output of the above code will be:

Hello World!

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. 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 example below, 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!");  
}

The output of the above code will be:

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 example below, 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);
}

The output of the above code will be:

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 example below, 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; 
}

The output of the above code will be:

25