User defined functions are defined by the user. C allows us to create functions according to our need. If there is no appropriate library function for specific requirement, we can create our own function to perform the desired operation. Once a function is defined, we can easily call and use the function whenever it require.
The User defined functions are of four types:
· Function with no arguments and no return value
· Function with no arguments and a return value
· Function with arguments but no return value
· Function with arguments and with return value
Function with no arguments and no return value:
Function with no argument means the called function does not receive any data from calling function and Function with no return value means calling function does not receive any data from the called function. So there is no data transfer between calling and called function.
Such functions can either be used to display information or or they are completely dependent on user inputs.
Output:
In the above program, sum();function calculates sum and no arguments are passed to this function. The return type of this function is void, hence return nothing.
Function with no arguments and a return value
Function with no arguments means called function does not receive any data from calling function and function with a return value means one result will be sent back to the caller from the function.
Such functions are used to perform specific operations and return their value.
Output:
In the above program, function sum does not take any arguments and has a return value as an integer type. It takes a and b as inputs from the user and returns them.
Function with arguments but no return value:
Here function will accept data from the calling function as there are arguments, however, since there is no return type nothing will be returned to the calling program. So it’s a one-way type communication.
Such functions are used to display or perform some operations on given arguments.
Output:
This program calculates the sum of two numbers and prints the result.
Function with arguments and with return value:
Function with arguments and with return value means both the calling function and called function will receive data from each other. It’s like a bi-directional communication.
These functions are used to perform specific operations on the given arguments and return their values to the user.
Output:
In the above program, function sum takes two arguments as a and b, and has a return value as an integer type. The main function takes input a and b from the user and calls the sum function to perform a specific operation on the given arguments and returns the value.