Methods returning a result

Writing a method returning a result is not really more work than writing a method without any result. [!java]You simply have to specify the data type of expected results before the method name (where we previously had void).[/!] [!scala]You simply have to add a column (:) after the parenthesis and write the type of data that your method will return, and add an equal sign (=). This syntax is actually rather close to defining a variable, with its type, that is actually a function.[/!] You can use the return instruction anywhere in your method body to specify that the computation is done (the method is not further executed), and that the result is the the value following the return keyword.

[!java]double pi() {[/!][!scala]def pi(): Double = {[/!][!python]def pi():[/!]
    return 3.14159[!java];[/!]
[!java|scala]}[/!]

Actually, you can also use that return keyword in methods that do not return any result, to interupt the computation. Of course, you should not provide any value to return in that case.

It is possible to have several return instructions in several branches of a conditional. In fac, it is forbidden to have any execution path of your body without any return, or to write some code after the return instruction. Indeed, if the machine reaches the end of the method without finding any return, it cannot know what actual value to give back to the method caller. Moreover, return interrupts immediately the method execution (why bother looking further when you know the method result?). So, if there is some code after a return, it must be an error and the compiler warns you.

[!java|scala][!java]boolean [/!][!scala]def [/!]isFrontFree()[!scala]:Boolean =[/!] {
    if (isFacingWall() == true) {
        return false;
        /* no code allowed here */
    } else {
        return true;
        /* here neither */
    }
    /* even here, forget it */
}[/!][!python]def isFrontFree():
    if isFacingWall() == True:
        return False
        # no code allowed here
    else
        return True
        # here neither
# even here, forget it[/!]

Exercise goal

You will once again write a method that the buggle will use. Its name must be haveBaggle, and it returns a boolean value indicating whether the row in front of the buggle contains a baggle or not. The buggle will use it to search the first row containing a baggle, and stop here.

The easier for this method is to use a boolean variable called seenBaggle indicating whether or not we saw a baggle so far. It initial value is 'false'.

Then, move 6 steps forward (the world contains 7 cells and we already are one one of them). For each cell, if it contains a baggle, we store true in sawBaggle (and we don't do anything but moving forward if not).

At the end, we move back by 6 steps, and we return the value of seenBaggle to the caller.

This exercise is a bit different since there is two initial worlds, each with a specific objective. Your code must work for each of them. Observe that the world selection scrolling menu (right below the speed slider) allows to switch the observed world.

When your method haveBaggle works, proceed to next exercise.