C programming - What is a Function

https:/‮/‬www.theitroad.com

In C programming, a function is a self-contained block of code that performs a specific task. Functions in C are used to break down large programs into smaller, more manageable pieces, and to avoid repeating code throughout a program.

Functions in C typically have a name, a return type, a list of parameters, and a body of statements. The return type specifies the data type of the value that the function returns, or void if the function does not return a value. The list of parameters specifies the type and number of arguments that the function expects to receive. The body of statements contains the actual code that the function will execute when it is called.

Here's the basic syntax of a function in C:

return_type function_name(parameter_list)
{
    // statements to be executed
    return value;
}

In this example, return_type specifies the data type of the value that the function returns, function_name is the name of the function, and parameter_list specifies the type and number of arguments that the function expects to receive. The body of the function contains the actual code that will be executed when the function is called, and the return statement specifies the value that the function will return.

Here's an example of a simple function in C that calculates the square of a number:

int square(int num)
{
    int result = num * num;
    return result;
}

In this example, the function is named square, it takes an integer argument num, and it returns an integer value that is the square of num. When the function is called with a value of 5 for the num argument, it will return a value of 25.

Functions are an important concept in C programming, as they allow you to write modular, reusable code that is easier to read and maintain.