Functions in C Language With Examples

Function in C programming is a reusable block of code that performs a specific task. In every C program, there is at least one function, which is main(), and even the most trivial programs may define additional functions. In this tutorial, we will learn about functions in C Language with the help of examples.

There are many situations where we might need to write same line of code for more than once in a program. This may lead to unnecessary repetition of code. So, C language provides an approach in which you can declare and define a group of statements once in the form of a function and it can be called and used whenever required.

Function Structure in C Language:

A function definition consists function header and a function body.

return_type function_name (parameters)
    //body of the function
  • Return type: The function's return type is always listed at the beginning. However, the void keyword is used as the function's return type if there is no return value.
  • Function Name: The function's name, that must be unique.
  • Parameters: Values that are passed during the function call.

Types of Functions in C Language:

There are two types of function in C programming:

Type of Functions in C Language

1. Pre-defined or standard library functions:

In C programming, built-in functions are the standard library functions. In header files, these functions are declared. For example:

  • A standard library function called printf() is used to display formatted output on a screen (display output on the screen). The header file stdio.h has a declaration for this function.
  • Therefore, we must use #include<stdio.h> to include the stdio.h header file in order to use the printf() method.
  • The sqrt() function determines a number's square root. The header file math.h contains the function's definition.

2. User-defined functions:

The functions that we create in a program are known as user defined functions or in other words you can say that a function created by user is known as user defined function. It reduces the complexity of a big program and optimizes the code.

You can also create functions as per your need. Such functions created by the user are known as user-defined functions.

Example 1: User-defined function to calculate two numbers sum:


int addition(int a, int b);     // function declaration

int main()
    int i, j, result;
    printf("Please enter 2 numbers: \n");
    scanf("%d%d", &i, &j);

    result = addition(i, j);        // function call
    printf("The result of addition is: %d", result);

    return 0;

int addition(int a, int b)
    return (a+b);       // function defintion, this can be done in one line

Output :

Please enter 2 numbers: 4 6 The result of addition is: 10

Advantages of defining Functions in C:
  1. The programme will be simpler to understand, maintain, and debug.
  2. Codes that can be reused in other programmes
  3. Large programmes can be broken down into smaller modules. As a result, a large project can be divided among several programmers.