Home >>C Tutorial >C Functions
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.
Here are some of the advantages of the functions in C language.
There are generally three aspects of a C function.
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 }
Here are the two types of functions in C programming language:
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.
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.
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:
#include<stdio.h> void Demos(); void main () { printf("Welcome "); Demos(); } void Demos() { printf("PHPTPOINT "); }
#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); }
#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); }
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.
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. |