User defined function in C language

These are the functions which are created by the C programmer.

There are three aspects of a user defined function in C language.

  1. Function declaration
    • A function must be declared globally in a C program to tell the compiler about the function name, function parameters, and return type.
    • The actual body of the function can be defined separately.
    • Syntax: return_type function_name (argument list);
    • Example: int max(int num1, int num2);
    • Parameter names are not important in function declaration only their type is required, for example: int max(int, int);
  2. Function call
    • Function can be called from anywhere in the program.
    • While creating a C function, you give a definition of what the function has to do. To use a function, you will have to call that function to perform the defined task.
    • The parameter list must not differ in function calling and function declaration.
    • We must pass the same number of arguments as it is declared in the function declaration.
    • To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value.
    • Syntax: function_name (argument_list)
    • When a program calls a function, the program control is transferred to the called function.
  3. Function definition 
    • It contains the actual statements which are to be executed.
    • It is the most important aspect to which the control comes when the function is called.
    • Notice that only one value can be returned from the function.
    • Syntax: return_type function_name (argument list) {function body;}

Defining a Function

return_type function_name( parameter list ) {
   body of the function
}
User defined function in C language

The return_type is the data type of the value the function returns.

Parameters are optional; that is, a function may contain no parameters.

A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter.

The parameter list refers to the type, order, and number of the parameters of a function.

The function body contains a collection of statements that define what the function does.

The function name and the parameter list together constitute the function signature.

Return Value

A C function may or may not return a value from the function.

If you don’t have to return any value from the function, use void for the return type.

void hello(){  
printf("hello c");  
}  

If you want to return any value from the function, you need to use any data type such as int, long, char, etc.

The return type depends on the value to be returned from the function.

In the below example, we have to return 10 as a value, so the return type is int.

int get(){  
return 10;  
}  

If you want to return floating-point value (e.g., 10.2, 3.1, 54.5, etc), you need to use float as the return type of the method as shown in below example.

float get(){  
return 10.2;  
}