Can someone please explain me why there is an error?

image

Basically there is nothing wrong with that. But, it does nothing. ( You did not send anything into the function. ) I will explain functions a bit.

First, the $a and $b variables in your function are LOCAL variables only. They exist only INSIDE the function itself. You set up a function like this:

function SOME-FUNCTION-NAME($ARGUMENT-1, $ARGUMENT-2, $ARGUMENT-999) { some code }

The inputs sent to the function are variables in your in-line code. You can call a function a million times if needed. In your example, you might need to add values a million times and you can do that. And, since it is a function, you can use it any place you need to add values. Sometimes several per line.

Now the “ARGUMENTS” can be one or many depending on your needs. In your case you only need two.
These are passed values only. Meaning they come from outside calls. These are passed into the function. They are NOT global. This means they are used inside the function only and do NOT exist outside the function. So, the $a and $b you use inside the function are NOT the same as the ones in the rest of your code.

Also, you return a value. This is assigned to the function name itself. So, in your example, to actually do anything with the results, you would need to do it this way:

echo addition(8, 13);

OR

$ernie=8;
$idontknow=13;
echo addition($ernie, idontknow);
**** In this example, the $ernie is placed into the $a in the function. It is NOT the same variable as any $a in the rest of your code. The $a, $b in your function is for that function only.

Hope this helps. By the way, there is a way to make the $a and $b a GLOBAL variable, but we will cover that another time…

1 Like

try this -

$a = 1;
$b = 1;
echo addition($a, $b); 
1 Like
Sponsor our Newsletter | Privacy Policy | Terms of Service