Function is a code block that executes certain task.
A void function doesn't return any value.
Example:
Output:
Via the return keyword a result can be returned.
Let's have an example in which rectangle's area is calculated by passing arguments about it's sides.
There are couple of things to mark:
lines 5 - 7
widthArg
heightArg
calcRectangleArea
line 2
area
If arguments count is mismatched an exception is thrown.
The function calcTrapezoidArea expects 3 arguments but only 2 are passed to it thus exception is thrown.
calcTrapezoidArea
Default value may be specified for function parameters.
Let's look at the following:
At line 5 the function argument is not passed thus it takes the default value - 3 specified at line 7.
line 5
3
line 7