C Programming language-C Functions

C Functions

C Functions are the block of code which is executed repeatedly whenever we need it.

Benefits of C Functions 

  • The Functions provide reusable code.
  • The Functions are highly modularIt makes the Debugging and code understanding easier.
  • The functions are independent of the main code.
  • The larger programs can be modularized into smaller functions.
Types of Functions

There are two types of functions in C:
  • Built-in Functions: The built-in functions are provided by the system and stored in the C library. For example: printf(), scanf(), strcpy, strlen, strcat, etc.
  • User-Defined functions: These user-defined functions are defined by the user while writing the program. 
Parts of Functions

  • Function Declaration
  • Function Definition
  • Function Call
Function Declaration

The function declaration is declaring the function before defining it. This tells the compiler about a function name and how to call the function. The actual function is defined separately.
The Function declaration is only required when you define a function in one source file and call that function in another file. In this case, you have to declare functions at the top of the file calling the function.
Syntax

dataType functionName (Parameter List);

Example

int sum(int a, int b);
int square(int a);
Function Definition

The Function Definition in C consists of the following parts as Function header and the Function body. The following are given in detail such as:
  • Return Type: A function may return a value. The return_type will be the data type of the value that is returned. Some functions may not return a value, such function has the return type “void”.
  • Function name: This will be the actual function name of the function. This is name along with the parameters are used when you call the function.
  • Parameters: A parameter is a placeholder for the function. When the function is invoked, the parameter is passed along with function name. The parameter values are also called as arguments. The parameters are optional, function can be without parameters as well.
  • Function Body: The function body contains the actual logic. It is written in various statements that define what the function does.
Example:
int max(int a, int b)
{
    if (a>b)
        return a;
    else
        return b;
}
Function Call

When you define the function, the program tells what the function has to do. To use the function in your program, you have to call the function to perform the defined task. When you call the function, the control will be transferred to the called function. A called function will perform the task and once done, the control will be transferred back to the main program. To call the function, you need to pass the arguments enclosed with the parentheses along with the function name. When the function returns the value, store the return value to the parameter.

Example:

The following is the program which gives the function definition along with the main program:
#include<stdio.h>

int max(int a, int b) //Function Definition
{
    if (a>b)
        return a;
    else
        return b;
}

void main()
{
    int x=10, y=20;
    int value = max(x,y); //Function Call
    printf("Max is %d", value);
}

Output:

Max is 20