Describe the concept of functions in C and explain different types of functions.

In C, a function is a self-contained block of code that performs a specific task. Functions are used to break down a program into smaller, reusable sections, making the code more organized and modular. The function is defined by its name, return type, parameters, and body.

Functions in C can be categorized into two types:

  • Standard Library Functions: These are built-in functions provided by C, like printf(), scanf(), and strlen(), which perform common operations.
  • User-defined Functions: These are functions created by the programmer to perform specific tasks. The syntax is:

return_type function_name(parameters) {
// function body
}

Functions can also be categorized based on whether they return a value:

  • Void Functions: These functions do not return a value. Example:
void printMessage() {
printf(“Hello, World!”);
}
  • Non-Void Functions: These functions return a value of a specified type. Example:

int add(int a, int b) {
return a + b;
}