Functions in C: Usage and Examples

Rumman Ansari   Software Engineer   2024-07-05 03:44:02   13313  Share
Subject Syllabus DetailsSubject Details 18 Questions 12 Program
☰ TContent
☰Fullscreen

Table of Content:

What is function

A function is a self-contained block of statements that perform a coherent task of some kind. Every C program has at least one function, which is main() function.

Suppose you are building an application in C language and in one of your program, you need to perform the same task more than once. In such case, you have two options –

a) Use the same set of statements every time you want to perform the task
b) Create a function to perform that task, and just call it every time you need to perform that task.
Using option (b) is a good practice and a good programmer always uses functions while writing codes in C.

Why we use function in C

A function is much important when we have to do a long project. Let we have a math function (sum, sub,mul etc.) in our project, we need the same calculation several times. if we have a function. We can do it by a single line (by calling the function). Otherwise, we will have to rewrite the same code of calculation again. And function got arbitrarily long. Above this reason we use function in C program and we don’t write all codes in main () function.

Functions are used because of following reasons –
a) To improve the readability of code.
b) 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.

Types of functions

  1. Predefined standard library functions
  2. User Defined functions

1) Predefined standard library functions

Predefined standard library functions such as printf(),  scanf(), puts()gets() 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.

2) User Defined functions – 

The functions that we create in a program are known as user-defined functions.

The General Form of a Function

return_type function_name (argument list)
{
    Set of statements – Block of code
}

return_type: Return type can be of any data type such as int, double, char, void, short etc. Don’t worry you will understand these terms better once you go through the examples below.

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 the function just by seeing its 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.

Predefined standard library functions

The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, I/O processing, string handling etc.

These functions are defined in the header file. When you include the header file, these functions are available for use. For example:

The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in header"stdio.h" file.

There are other numerous library functions defined under "stdio.h", such as,  scanf() fprintf()getchar() etc. Once you include "stdio.h" in your program, all these functions are available for use.

Program

 #include
 void main(){
 printf("Predefined standard library functions");
 }
 

Predefined standard library functions

As mentioned earlier, C allows programmers to define functions. Such functions created by the user are called user-defined functions.

Depending on the complexity and requirement of the program, you can create as many user-defined functions as you want.

How user-defined function works?

 #include 
void functionName()
{
    ... .. ...
    ... .. ...
}

int main()
{
    ... .. ...
    ... .. ...

    functionName();
    
    ... .. ...
    ... .. ...
}
 

The execution of a C program begins from the main() function.

When the compiler encounters functionName(); inside the main function, control of the program jumps to

 void functionName()

And, the compiler starts executing the codes inside the user-defined function.

The control of the program jumps to statement next to functionName(); once all the codes inside the function definition are executed.

function in c

Program

 #include 
 
/* function declaration */
int add(int num1, int num2);
 
int main () {

   /* local variable definition */
   int a = 100;
   int b = 200;
   int ret;
 
   /* calling a function to get add */
   ret = add(a, b);
 
   printf( "value is : %d\n", ret );
 
   return 0;
}
 
/* function returning the add between two numbers */
int add(int num1, int num2) {

   /* local variable declaration */
   int result;
  
   result = num1+num2;
 
   return result; 
}
 

Output

 value is : 300
Press any key to continue . . .
 
 

Advantages of user-defined function

  1. The program will be easier to understand, maintain and debug.
  2. Reusable codes that can be used in other programs
  3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers.
MCQ Available

There are 11 MCQs available for this topic.

11 MCQTake Quiz