QUIZGUM

Coding Class

Quizgum : Setting parameter defaults

Setting parameter defaults for functions

We learned how to create a function earlier.
You can declare parameters when you create a function.
You can set the default value of a parameter when no argument is specified in the function call part.
For example, if you have the following code:

  1. function plus($num, $num2, $num3) {
  2. $sum = $num + $num2 + $num3;
  3. return $sum;
  4. }
  5. echo plus();

In the code above, we didn't use any arguments.
This will cause an error.

How to set parameter defaults

  1. function 함수명($param = default, $param2 = default) {
  2. }

Now is it simple?
If there are no arguments, the default value will be used.
So let's create an example by setting defaults in the code we used above.

  1. <?php
  2. function plus($num = 100, $num2 = 40, $num3 = 90) {
  3. $sum = $num + $num2 + $num3;
  4. return $sum;
  5. }
  6. echo plus();
  7. ?>

This is the result of the code above.