What is functions in C ? How to implement in Turbo c and Code Blocks ( GCC compiler )

A function is a block of statements that performs a specific task. Suppose you are building an extensive application in C language and your programs become bigger and cannot be easily understand . In such case you can divide the program in form of modules called function . 

you can use the same set of statements every time you want to perform the task by these created modules , this will help you in reducing the complexity of the program and makes it easily distinguish it's working 

we will discuss the advantage of function briefly in this post.

Now Let's move to type of Function :

1 . Predefined Function's / Library Function's : These are the functions which already have a definition in header files (.h files like stdio.h), so we just call them whenever there is a need to use them. 

for example : gets() , puts() , scanf() , printf() etc.

2 . User defined Function (UDF) : The functions that we create in a program are known as user defined functions.

Before we will learn how to create user defined functions and  to use them in C Programming we must get to know Why there is need of functions in C ?

Why we need functions in C

Functions are used because of following reasons –
a) To improve the readability of code.
b) Improves the reusability of the code, same function can be used in any program rather than writing the same code from scratch.
c) Debugging of the code would be easier if you use functions, as errors are easy to be traced.
d) Reduces the size of the code, duplicate set of statements are replaced by function calls.

Now we are in position to deal with UDF.

Syntax of a function

return_type function_name (argument)
    // statement of the function or definition of function

return_type: Return type can be of any data type such as int, double, char, void, short etc. 

function_name: It can be anything, however it is advised to have a meaningful name for the functions so that it would be easy to understand the purpose of function just by seeing it’s name.

argument list: Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.

Block of code: Set of C statements, which will be executed whenever a call will be made to the function.

Let's understand the above term one by one :-)

let's take a example that we want to add two integer variable using UDF 

Since,the function is going to add two integer variable , therefore take it's return type as int by default , However you can also take its return type as void .

The function should add two number , therefore it must have a meaningful name like sum , add ,addition .which easily describes about the function that what it does ?

let's name the function  as sum

return_type sum(argument list)

This function is going to add two integer variable therefore we have to take two integer input from user in oder to add two integer variable therefore it take two integer argument

return_type sum(int a, int b)

After addition the result must be of type integer 

for example 2 & 3 are integer variable and their addition is also a integer i,e 5 is an integer, therefore the result is always inetger

from this we can conclude that the function must return a integer value and now we got by return type .

int sum(int a, int b)

Now you have a question comes to your mind that how i will use or call this function ?

Ans : To understand this Consider a example given below .

Example1: Creating a user defined function addition()

#include <stdio.h>
int sum(int num1, int num2)
     int addition;
     /* Arguments are used here*/
     addition = num1+num2;

     /* Function return type is integer so we are returning
      * an integer value, the sum of the passed numbers.
     return addition;

int main()
     int a, b;
     printf("Enter number 1 and number 2: ");

     /* Calling the function here, the function return type
      * is integer so we need an integer variable to hold the
      * returned value of this function.
     printf ("Output: %d", sum(a,b));

     return 0;


Enter number 1: 1
Enter number 2: 6
Output: 7

Example2: Creating a void user defined function that doesn’t return anything

#include <stdio.h>
/* function return type is void and it doesn't have parameters*/
void intro()
    printf("Hello World\n");
    printf("My name is Vishal\n");
    printf("How are you?");
    /*since we are not returning any data type like int ,double,float ,char etc therefore
     * return type is void

int main()
     /*calling function*/
     return 0;


Hello World
My name is Vishal
How are you?

Few Points to Note regarding functions in C:
1) main() in C program is also a function.
2) Each C program must have at least one function, which is main().
3) There is no limit on number of functions; A C program can have any number of functions.
4) A function can call itself and it is known as “Recursion“. you can find the separate post over this.

C Functions Terminologies that you must remember

return type: Data type of returned value. It can be void also, in such case function doesn’t return any value.

Note: for example, if function return type is char, then function should return a value of char type and while calling this function the main() function should have a variable of char data type to store the returned value.

Recommended Book Must Buy :

To improve your skill and Knowledge you must read Let Us C 16TH EDITION  because this book is totally recommended for beginners and you can improve your skills by attempting example and question inside it 

Note : If you want this book with solution . what you have to do is to click here .

Post a Comment