In C programming, a function is a set of statements that perform a specific task. Every C program has at least one function, which is the main() function. Functions are the basic building blocks of a C program and provide modularity and code reusability. They enable reusability and reduce redundancy, make a code modular, provide abstraction functionality, and make the program easy to understand and manage.
A function declaration tells the compiler about a functions name, return type, and parameters, while a function definition provides the actual body of the function. The general form of a function definition in C programming language is as follows:
return_type function_name( parameter list ) {
body of the function
}
There are two types of functions in C programming:
-
Standard library functions: These are built-in functions in C programming that are defined in header files. Examples include printf(), scanf(), ceil(), and floor() .
-
User-defined functions: These are functions that a developer or user declares, defines, and calls in a program. This increases the scope and functionality and reusability of C programming as we can define and use any function we want.
To create a user-defined function, we specify the name of the function, followed by parentheses () and curly brackets {}. For example:
void myFunction() {
// code to be executed
}
The void keyword means that the function does not have a return value. We can call a function by writing its name followed by two parentheses () and a semicolon.
Variadic functions are functions that may take a variable number of arguments and are declared with an ellipsis in place of the last parameter. Using them in a function declaration means that the function will accept an arbitrary number of parameters after the ones already defined.