How To Code? Types Of Functions In Programming
Types Of Functions In Programming:In previous post we briefly discussed what are functions in programming and how to use them in a generic way. Here, you will learn what are different types of functions.
Functions are divided into two categories based on where their definition exists.
- Built in functions
- User define functions
1. Built-in Functions
Built-in functions are those which are already defined in a library. All you need to do is to import the library and call the function. There is no need to define and declare the required function.
Steps to use Built-in function:Each and every language has a rich set of libraries developed for specific purposes. It is really hard to remember all of these libraries and functions and there is no need to remember at all. Here are listed some steps that I would suggest you to find a library and its functions for certain task.
- Understand both the problem and your solution
- Search a library suitable for your need
- Find your required function and read its documentation
2. User define functions
As the name suggests, user define functions are those which are defined by the user.
A user define function is declared by the user (the developer) and the definition is written before a function call is made to it.
Types of Functions:
Recall the input/parameter and output/return from previous post. Based on these 2 features, functions are of 4 types:
- Functions with no input no output
- Functions with only input
- Functions with only output
- Functions with both input and output
|Functions are like puzzle pieces. You can use it the way you want.|
1. Functions with no input no output
As far as I know, this type of function is used in very rare cases. It does not accept any input parameter and does not return any output value. But it does not mean it is useless. It depends upon your creativity how you use it.
How to use?
I use this type only when I need to display fix information for user or for debugging. It could also be used in combination with global variables which is considered the worst practice or with constants.
2. Functions with only input
Function with only input is declared and defined with a certain number of input parameters but no return value. It takes values from other scopes in the code but does not transfer the result to any other scope.
How to use?
The output value could be directly printed on screen, written to a file, stored in a reference parameter or passed to another function as input.
Again, it is your creativity how you use it.
3. Functions with only output
Functions with only output have only return value type and no input parameters.
How to use?
The input for this type of functions does not come from another scope. Either the global values or constant values are used (not recommended) or input is created by internal processing. For example input could be read from a file or any other function with a return value.
4. Functions with both input and output
It allows us to pass the parameters to the function while calling the function. This type of functions will return some value when it is called. Data Type of the return value depends on the return type of function declaration.
How to use?
This type of functions are fully dynamic functions which provide maximum control to the developer. It is most widely used type of function. Decoupling, one of the best programming practices is easily achieved using functions with both input and output.
Next: Function Overloading