X
X

Generator Yielding Values

Generators have the ability to values yield a lot like return statements, allowing the use of value’s outside the functions’ execution context. But yielding out values is what we’ve already discovered but now we want to yield in values which means you can change values mid execution. In essence we can yield out values and yield in values with generator functions.

Yielding values outside of the function is much like using a return statement where a value is allowed to be used outside of the function. But what about yielding values inside the function effectively reversing this process, with generators we can send them into the function.

The answer is, yes you can! When invoking a generator we know it creates a control object to control the execution of the generator function. This control object has a send method that can send a value into the generator function rather than receiving a value outside of the function.

Take a look at this example below where we create a variable, next we yield a value and assign it to the variable; finally the $distance variable is echo out in the string. We repeat this process twice in the generator function:

function satNav( ){
	$distance = 0;
	echo ‘Start from driveway. <br>’;
	$distance = yield;
	echo $distance . ‘  miles. <br> Now take a left. <br>’;
	$distance = yield;
	echo $distance . ‘ miles. <br> Destination reached.’;
}

You now have two yield statements but notice they aren’t yielding a value like there was before. This time we yield a value back to send in a value rather than receive the value. In order to start executing and sending in values we need to create the control object and place it inside of a variable.

$control = satNav( );

Now we’ve invoked the satNav function, creating the control object, lets start the execution…

$control->current( );

… This will execute the first block of code…

$distance = 0;
echo ‘Start from driveway. <br>’;
$distance = yield;

…This will create the $distance variable and echo out ‘Start from driveway.’ Now we’re at our first stop sign but again this stop sign doesn’t give a value, instead it wants a value. Just to note you could continue by pushing the accelerator by using the next method. But we don’t want to do that instead we want the function to retrieve a value. Think of this as you stopping at a yield or stop sign, get out of your car and fetch a package or value and get back in your car. This means we can yield a value back within our execution context instead of yielding the value outside of the function.

So we’re currently stopped and we need to get out of the car and fetch a package. We do this via the control object and use the send method, so for example lets yield the value 50…

$control->send( 50 );

… Now this value is yielded back to our function and assigned to the distance variable…

$distance = yield;

… Finally after the value has been yielded and assigned we continue on executing our generator function…

echo $distance . ‘  miles. <br> Now take a left. <br>’;

… This will produce the following output…

50 miles. 
Now take a left. <br>’;

… Then we’re at the next yield sign ready to finish our destination …

$distance = yield;
echo $distance . ‘ miles. <br> Destination reached.’;

… In order to continue we need to invoke the send method again to yield or pass in a value, this time we’ll yield the value 100, then continue to drive the car or execute our generator function…

$control->send( 100 );

… We can now get out of our car retrieve the value of 100 and get back in the car and press the accelerator pedal. This will execute the last command in the execution context printing out the yielded value contained within the distance variable…

echo $distance . ‘ miles. <br> Destination reached.’;

… Which’ll produce …

100 miles.
Destination reached.

So hopefully now you can see that not only does a generator function have a controllable execution context that can be paused but also it can have values sent in mid execution. Please continue on to the next lecture where we’ll look at datatypes and what you can yield within a generator function.

Latest Posts