Function with Default Parameters
Learn about the default parameters of functions.
We'll cover the following
Challenge
Write a program that defines a function that receives 5 arguments of types char
, int
, long
, float
, and double
in that order. If we call this function in three different ways (once, with values for all five arguments, then with values for only the first four arguments, finally with values for only the first three arguments) the program compiles and runs fine. If the last argument is omitted, it should take on the value 6.28 by default. If the last two arguments are omitted, the fourth argument should be taken as 3.14 and the fifth argument as 6.28 by default.
Sample run
Here’s what you should see when you run the program.
1. First call with all 5 arguments:
A 12 123 0.5 -1.5
2. Second call with first 4 arguments:
B 1234 12345 4.5 6.28
3. Third call with only first 3 arguments:
C 987 9876 3.14 6.28
Coding exercise
Try to solve this challenge on your own. If you can’t solve it, you can look at the solution and explanation below.
Get hands-on with 1400+ tech skills courses.