
Then we can have other function statements to perform some task inside the function.įinally we can have the return statement if the function returns any value. We can declare local variables inside the body which is only accessible inside the function. The list of parameters in the function definition consists of the actual parameter names and do not have to match the parameter names in the function declaration. The function name in the function definition is same as the function name in the function declaration part. And we already know that the return type tells us about the type of value returned by the function. The return type in function definition must match the return type in function declaration.

Function definition is also known as function implementation.įunction definition consists of 6 parts, three under Function Header and three under Function Body. This is the part were we implement the function. The terminating semicolon marks the end of the function declaration. Parameter name in the function declaration and in function definition do not need to be the same. In the following example we are not writing the parameter names. Parameter names are optional and can be ignored in the function declaration. In the following example the greetings() function takes no parameter and returns nothing. If a function takes no parameter then set it to void. Parameter list must be separated by comma. Name of the function can be any valid identifier like display(), result() etc.ĭon't use reserved keywords to name a function. We set the return type to void if the function returns nothing. If a function is going to return lets say an integer value then the return type will be set to int. Return type tells us about the type of value returned by the function. In the following example we are declaring a function named findArea having return type float and it takes two parameters length and width of type float.įloat findArea(float length, float width) Like variables, in C we have to first declare a function before calling it.įunction declaration is also known as the function prototype and it consists of 4 parts given below. This will help us in developing the code easily and will also help in testing and debugging in case of any error.Ī user defined function consists of three parts. Or we can divide the program into subprograms using function like findArea(). The idea is to break the program into modules (subprogram) and develop the program in small pieces and then join them together to solve the problem.įor example, if we have a program to find the area of a rectangle then we can either write the entire code inside the main() function. These subprogram are designed to perform specific task and hence it makes the code easier to develop, test and debug.

User defined functions helps us to break our program into subprograms. Whereas, User defined functions are the ones that we have to write in order to use it. Library functions are already created and ready for use and we don't have to write it. The main() function is user defined function whereas, printf() and scanf() are library functions.ĭifference between Library and User Defined functions These are the functions created by the user like for example greetings() or displayResult() etc. These are the functions that we get from the C library like strlen(), concat() etc. In this tutorial we will learn about functions in C programming language.Ī function in C is a block of code consisting of some statements that performs a specific task.įor example, we can have a greetings() function that will print greetings message whenever it is called.
