What's the difference between optional and required parameters? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What's the difference between optional and required parameters?

Can you give examples of what these are and possibly explain simply for a beginner...

12th Dec 2016, 12:43 PM
Scott
Scott - avatar
2 Answers
+ 3
public int add(int req, int def = 20) { return req + def; } int a = add(10); int b = add(10, 20); int c = add(0, 30); In this example: a, b and c are all equal to 30. Default values simply allow you to exclude them when you make a call to the function.
12th Dec 2016, 12:55 PM
Kerrash
Kerrash - avatar
0
Thanks John. I'm noticing different people using different terms to explain the same thing. Your use of "default" makes more sense than "optional" or "required", whichever is meant to be.
16th Dec 2016, 12:19 PM
Scott
Scott - avatar