Calling a function — expression

Look at the code from one of the previous lessons:

$distance = calculateDistance('Lannisport', 'Bayasabhad');

What can we say about it? What conclusions can we draw? Finish the following statemens:

  • $distance — is a __________
  • the function calculateDistance is called with ___________ 'Lannisport' and 'Bayasabhad'
  • the function calculateDistance returns a value, type of which is - _____.
  • after the code is executed $distance will store a ________, returned by the call of the calculateDistance function

In order to grasp the material of this lesson, let’s refresh the term expression from the module about variables. As you remember, an expression is code that turns into a value when the program is executed.

Which of the following code fragments are expressions?

  1. 42
  2. 10 * 45
  3. "Kings" . "road"
  4. calculateDistance('Lannisport', 'Bayasabhad')

We could tell right away that mumbers and mathematical operations are expressions. The expression 42 will turn into value 42, the expression 10 * 45 - into value 450.

String concatenaton is an expression as well, and it will turn into the corresponding value (a new string);

Now, look at number four! It is an expression too! One reason why programming languages are so powerful and flexible is the fact that a function call is an expression.

Let’s go back to the first example:

$distance = calculateDistance('Lannisport', 'Bayasabhad');

$distance will store the result of the expression. Unlike an operation (for example, 10 + 12), where the calculation is obvious, function hides from us the inner calculations, and we only see the result. That’s why we say that function “returns” a result. We can apply this term to ordinary operations. We can say, for example, that string concatenaton returns a new string.

What does count as an expression and what does not? It may seem to you another boring detail from a programming manual. However, it’s a very important question. Everything that works as an expression can be used in other expressions and anywhere an expression is expected as input. Recognizing expressions in code - is a skill a developer needs every day.

Here is the function numberOfKnights, that receives a name of a castle in the kingdom and returns the number of knights in that castle. Knowing that a function call is an expression, we assume that the following code will work:

<?php

$result = 4 + numberOfKnights("Winterfell");

And it will, because we are adding two expressions: the expression 4 and the expression numberOfKnights("Winterfell"). Eventually it comes down to 4 + some number.

It means that the following code will work as well:

<?php

$result = numberOfKnights("Winterfell") + numberOfKnights("Oldtown");

Here we have two function calls, but every call is an expression, which comes down to addition of two values - two numbers (the number of knights in Winterfell and the number of knights in Oldtown).

instructions

Arya is going to travel from Winterfell to The Eyrie to visit Lysa Arryn, but first she wants to stop by The Twins to get revenge on the Freys. She needs to calculate the overall route length.

Unfortunately, the calculateDistance function can only calculate distance between two points. So you need to calculate the distance from Winterfell to The Twins first, and then the distance to The Eyrie.

Print to screen the overall route length for Arya. Remember, that the function calculateDistance receives two arguments and returns a number.


Exercise available only for signed users.

Please sign in with your GitHub account, this is necessary to track the progress of the lessons. If you do not have an account yet, now is the time to create an account on Github.