What is Function in C?

In c, we can divide a large program into the basic building blocks known as function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the C program. In other words, we can say that the collection of functions creates a program. The function is also known as procedureor subroutinein other programming languages.

Advantage of functions in C

Function Aspects

There are three aspects of a C function.

SN C Function Aspects Syntax
1 Function declaration return_type function_name (argument list);
2 Function call function_name (argument_list)
3 Function definition return_type function_name (argument list) {function body;}

The syntax of creating function in c language is given below:

syntax

            
return_type function_name(data_type parameter...){  
//code to be executed  
}  
            
        

Types of Functions

There are two types of functions in C programming:

  1. Library Functions: are the functions which are declared in the C header files such as scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  2. User-defined functions: are the functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Why Use Functions?

Functions in C provide several advantages, such as:

  1. Reusability: Once defined, functions can be used multiple times.
  2. Modularity: Functions break a program into smaller, manageable parts.
  3. Maintainability: Easier to debug and update individual functions.
  4. Organization: Functions make the program more structured and easier to read.

Types of Functions in C

In C, functions can be categorized into two types:

  1. Library Functions: These are pre-defined functions available in C libraries (e.g., printf(), scanf()).

  2. User-defined Functions: These are functions defined by the programmer to perform specific tasks.

Syntax of a Function

The general syntax of a function in C is as follows:

Example

                        
return_type function_name(parameters) {
// Body of the function
// Code to perform the task
}
                        
                    

Example of a Simple Function

Here is an example of a simple function in C:

                        
    #include <stdio.h>
    // Function declaration
    int add(int a, int b);
    
    int main() 
    {
        int result = add(5, 3);
        printf("Sum: %d", result);
        return 0;
    }
    
    // Function definition
    int add(int a, int b) {
        return a + b;
    }
                        
                    

Output:

Sum: 8

Syntax

                        
type_name variable = (type_name) expression;
                        
                    

Example of Explicit Type Casting:

Example

                        
#include <stdio.h>
int main() 
{
  float a = 5.75;
  int b = (int) a;  // Explicit type casting from float to int
  printf("Value of b: %d\n", b);
  return 0;
}
                        
                    

Output:

Value of b: 5