C Functions
Functions in C are a group of statements that perform a single task. They serve to improve the readability, modularity, and reusability of code by dividing a program into pieces.
Key points about functions
- Function Declaration
- Function Definition
- Function Calls
Function Declarations
A function is first declared before it can be used. The declaration indicates the return type, name, and parameters of the function.
Syntax:
return_type function_name(para_1, para_2){
// block of the function
}
Function Definition
Function definitions include the implementation of the actual function.
Function Call
A function is invoked by name and then by parentheses.
Example:
#include
<stdio.h>
// Function declaration
void
func();
int
main()
{
func();
// calling the function
return
0;
}
// Function definition
void
func()
{
printf("Execution Succesfull.");
}
Output
Execution Succesfull.
Types of functions
- Library Functions
- User Defined Functions
Library Functions
Library functions are previously defined functions in the C programming language. They are defined in header
files.
Exp: printf(), scanf(), etc
User Defined Functions
User-defined functions are functions that the programmer defines to perform special jobs. The programmer defines
such functions according to his or her requirements and can be utilized multiple times in the program.
Exp: Any function defined by the programmer.