Home >>C Tutorial >C Functions

Functions in C

Functions in C

In the C Language users can divide a big program into the basic building blocks that are called as function. The function basically consists of a set of programming statements that are enclosed by {}. A user can call the function multiple times in order to provide reusability and modularity to the C program. In simple words, it can be understood as the collection of functions creates a program. In other programs it is also known as procedure or subroutine.

Let's have a look at the advantages of the functions in C

Here are some of the advantages of the functions in C language.

  • Users can avoid the rewriting of same logic/code repeatedly in a program, by using the functions.
  • C functions can be called any number of times in a program and from any place in a program, by the users.
  • Tracking of a large C program can become extremely easy when the program is divided into multiple functions.
  • The main achievement of the C functions is the reusability of the code.
  • Function calling is always treated as an overhead in the C programs.

Function Aspects in C

There are generally three aspects of a C function.

  • Function declaration : In order to tell the compiler about the function name, function parameters, and return type, the function should be declared globally in the C program.
  • Function definition : The actual statements that are to be executed are constituted in the function definition. It is known to be the most important aspect to which the control comes whenever the function is called. The noticeable point here is that only one value can be returned from the function.
  • Function call : The functions in C can be called from any place in the program. The real catch here that the parameter list must not differ in function calling and function declaration. The users have to pass the exact same number of functions as it is declared in the function declaration.

Here is the syntax for creating a function in the C language:

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

Types of Functions in the C language:

Here are the two types of functions in C programming language:

  • Library Functions : These are the functions that are declared in the C header files like: scanf(), printf(), gets(), puts(), ceil(), floor() etc.
  • User-defined functions : These are the functions that are created by the user in order to use it multiple times. It generally reduces the complexity of a large programs and optimizes the code.

Return Value

The value from the function may or may not be returned by the C function. It is recommended to use the void for the return type then the user don’t need to return any value from the function.

Here is an example of the C function that doesn't return any value from the function.

void hello()
{  
printf("hello Phptpoint");  
}  

In order to return any value from the function, the users need to use any data type such as int, long, char, etc. The return type generally depends on the value to be returned from the function.

Here is an example with the return value:

int get()
{  
return 10;  
}  

In the above mentioned example the users have to return 10 as a value hence, the return type is int. In order to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), users need to use float as the return type of the method.

float get()
{  
return 10.2;  
}  

To get the value of the function, the function needs to be called.

Let's have a look at the different aspects of function calling:

Any argument may or may not be accepted by a function and it may or may not return any value. Here are the four different aspects of function calls that are based on the above mentioned facts:

  • function without arguments and without return value
  • function without arguments and with return value
  • function with arguments and without return value
  • function with arguments and with return value

Function example of without argument and return type value

#include<stdio.h>  
void Demos();  
void main ()  
{  
    printf("Welcome ");  
    Demos();  
}  
void Demos()  
{  
    printf("PHPTPOINT ");  
} 
Output :Welcome PHPTPOINT

Example 2:(Write a Program to print sum of two number)

#include<stdio.h>  
void add();  
void main()  
{  
    add();  
}  
void add()  
{  
    int x,y;   
    printf("\nEnter Your  two numbers");  
    scanf("%d %d",&x,&y);   
    printf("The sum of two nummber is %d",x+y);  
}
Output :
Enter Your two numbers 10 20
The sum of two nummber is 30

Example 3:(Write a Program to print sum of two number using argument type)

#include<stdio.h>  
void add(int, int);  
void main()  
{  
    int x,y,z;   
    printf("\nEnter Your two numbers here :");  
    scanf("%d %d",&x,&y);  
    sum(x,y);  
}  
void sum(int x, int y)  
{  
    printf("\nThe sum of two number is %d",x+y);      
}  
Output :
Enter Your two numbers 10 20
The sum of two nummber is 30

Library Functions in C

Function that are inbuilt in C and are grouped and placed at a common place known as the library is called as Library functions. Such functions are generally used to perform some specific operations. For instance: printf is basically a library function used to print on the console. All the C standard library functions are generally defined inside the different header files that are saved with the extension .h. The users need to include these header files in the program to make use of the library functions defined in these header files. For instance, in order to use the library functions such as printf/scanf users need to include stdio.h in our program that is a header file containing all the library functions regarding standard input/output.

Here is the list of mostly used header files:

Header file Description
stdio.h This header is a standard input/output header file and It contains all the library functions regarding standard input/output.
conio.h This header is a console input/output header file.
string.h It header contains all string related library functions like gets(), puts(),etc.
stdlib.h This header file generally constitutes all the general library functions like malloc(), calloc(), exit(), etc.
math.h This header file generally contains all the math operations related functions like sqrt(), pow(), etc.
time.h This header file in C generally contains all the time-related functions.
ctype.h This header filein C generally contains all character handling functions.
stdarg.h The Variable argument functions are generally defined in this header file.
signal.h All the signal handling functions are generally defined in this header file.
setjmp.h This header file generally contains all the jump functions.
locale.h This header file generally contains locale functions.
errno.h This header file generally contains error handling functions.
assert.h This header file generally contains diagnostics functions.

No Sidebar ads