Functions in C

In this section of the tutorial, we will learn about one of the most important and useful aspects in C programming language i.e. Functions. A function is a simplified form of a block of code that acts as a building block of a large and complex program.

What are functions?

Functions are the set of code used in different parts of a program by calling the function, which makes it easy for a user to write large and complex program.

  • Function provides reusability and portability by calling the same block of codes multiple times anywhere in a program.
  • It saves the time of a user and increases the future scope of maintaining and debugging the program easily.

Why do we use functions?

Function makes our task easy when we write big programs. A program can be created using a collection of different functions each having a specific task to perform.

  • Function helps in the reusability of the same block of code by calling a function multiple times in a program when needed.
  • Function saves the time of a user by reusing the same function again at any part of the program by calling the function.
  • Function has a structured form of declaring, defining and calling a block of code which helps in easily writing a large purpose-specific program easily.
  • Function helps in easily accessing a set of code at any time in the future due to its simplified divided form in a large program.
  • Function makes it easy to maintain and understand by other programmers in a project.

What are the types of functions?

In C programming language, functions are divided into two categories on the basis of their use in writing a program.

  • Library functions
  • User-defined functions

Library functions

A library function is pre-written in inbuilt header files or C library which is used by the user in a program. It provides an ease to the user to directly call the function from the built-in libraries in order to perform useful tasks in a program just by passing the values in the argument without writing a single line of code.

A list of different functions that are usually used in a program with their header files are in the table listed below.

How to use library functions?

Library functions are present in built-in libraries of C language. We just have to include the header file containing a particular function and call it inside the function main() of the program.

Below is an example to demonstrate the use of library function in a program.

Example: Program to find the square root of a number using sqrt() function.

Sample code:

#include <stdio.h>
#include <math.h>
int main()
{
    int number = 9;
    int Total;
    Total=sqrt(number);
    printf("Square root of %d = %d",number,Total);
    return 0;
}

Output:

Square root of 9 = 3

In line 8 of the above code, sqrt() function is used to find the square root of a number. Function sqrt() is a built-in library function directly called from the header file math.h in line 2.

Similarly, in line 10, printf() function is used to display the result on the output screen. Function printf() is a built-in library function directly called from the header file stdio.h in line 1.

User-defined functions

A user-defined function is a block of codes written by the user during the development of the program. Later it can be merged and included in the inbuilt library of C programming language.

  • User-defined functions are the functions that are independently coded and later combined into a single unit to form a large program.
  • It is easy to maintain, understand and debug codes in smaller parts which is why functions are used to create bigger are complex programs.
  • At every instance of building a program, a function is more preferably used because of its reusability which helps in saving both time of the user and memory of the computer.
  • A common example of user-defined function is function main(). Function main() is enclosed by {} and inside it, there are different codes written by the user as per the requirement which is eventually executed to carry out a specific task.

Elements of user-defined functions

Before using a user-defined function in a program, there are three basic elements that must be present in the program to make use of the user-defined function, namely:

  • Function definition
  • Function call
  • Function declaration

Function definition

Definition of a function includes the following six elements:

  • Function type It specifies the type of value (int, float or double) that the function is expected to return to the program calling the function. If no return type is specified, then the function will return int type by default. Also, void type is used for function returning nothing.
  • Function name – It should be a meaningful name reflecting the nature of the function. It should follow the rules of identifiers in C.
  • List of parameters – It passes the data value sent by the calling program to the function.
  • Local variable declarations – It specifies the variable used by the function body.
  • Function statements – It performs the task of the function.
  • Return statement – It returns the value evaluated inside the called function to the main program.

The format of function definition can be divided into two parts:

Header : It includes function type, function name and parameter list.

Body :  It includes local variable declarations, function statements and return statement.

Function call

A function is called by using the function name inside the main function or anywhere in the program. It passes the list of parameters to the called function and then the control is transferred to the called function.

Below is an example to demonstrate the function call in a program.

When the function Add() is called inside the function main(), the control is transferred to the called function Add(). Then the called function Add() sends the parameter values 5 and 3 to the function body to perform the calculations and then the result is returned to Sum.

Function declaration

A function declaration is the most important part of using a user-defined function in a program. A function must be declared, before they are called in a program.

A function declaration consists of four parts:

  • Function type
  • Function name
  • Parameter list
  • Terminating semicolon

A function is declared in the following format: function-type function-name(parameter list)

Rules for declaring a function:

  • Parameter list must be separated by commas.
  • The types must be the same as the types of parameter in the function definition, in number and order.
  • If there is no value to return, then return type is void.

Below is an example to show the use of user-defined function in a program.

Example: Program to add two numbers using user-defined function.

Sample code:

#include<stdio.h>
int Add(int,int);
int main()
{
  int Sum;
  Sum = Add(5,3);
  printf("%d",Sum);
  return 0;
}
int Add(int x,int y)
{
  int n;
  n=x+y;
  return n; 
}

Output:

8

In the next section, we will discuss about the types of function in detail.