A function is a block of statements that performs a specific task.
C function contains set of instructions enclosed by “{ }” which performs specific operation in a C program.
The function in C language is also known as procedure or subroutine in other programming languages.
A function can be called many times. It provides modularity and code reusability.
There are many situations where we might need to write same line of code for more than once in a program. This may lead to unnecessary repetition of code, bugs and even becomes boring for the programmer. So, C language provides an approach in which you can declare and define a group of statements once in the form of a function and it can be called and used whenever required.
C program has at least one function, which is main().
Library functions are those functions which are already defined in C library, example printf(), scanf(), strcat() etc. You just need to include appropriate header files to use these functions. These are already declared and defined in C libraries.
A User-defined functions are those functions which are defined by the user at the time of writing program. These functions are made for code reusability and for saving time and space.
a function must also be declared before its used. Function declaration informs the compiler about the function name, parameters is accept, and its return type. The actual body of the function can be defined separately. It's also called as Function Prototyping.
A function may return a value. The return_type is the data type (int, float, char, double) of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void.
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.
Function name is an identifier and it specifies the name of the function. The function name is any valid C identifier.
The functionName is name of the function to be called.
The parameter list declares the type and number of arguments that the function expects when it is called. Also, the parameters in the parameter list receives the argument values when the function is called.
Argument list contains variables names along with their data types. These arguments are kind of inputs for the function. For example – A function which is used to add two integer variables, will be having two integer argument.
The function body contains a collection of statements that define what the function does.
#include<stdio.h>
void show(){#include<stdio.h>
//defining function#include<stdio.h>
int multiply(int a, int b); // function declarationArguments are the values specified during the function call, for which the formal parameters are declared while defining the function.
It is possible to have a function with parameters but no return type. It is not necessary, that if a function accepts parameter(s), it must return a result too.
While declaring the function, we have declared two parameters a and b of type int. Therefore, while calling that function, we need to pass two arguments, else we will get compilation error. And the two arguments passed should be received in the function definition, which means that the function header in the function definition should have the two parameters to hold the argument values. These received arguments are also known as formal parameters. The name of the variables while declaring, calling and defining a function can be different.
A function may or may not return a result. But if it does, we must use the return statement to output the result. return statement also ends the function execution, hence it must be the last statement of any function. If you write any statement after the return statement, it won't be executed.
The datatype of the value returned using the return statement should be same as the return type mentioned at function declaration and definition. If any of it mismatches, you will get compilation error.
#include<stdio.h>
// function prototype, also called function declarationTrending Tutorials