Quite often, you want to write a method that compute some value and
returns it. Methods returning results are called functions.
Writing a function is not really harder than writing a simple method.
[!java|c]You simply have to specify the data type of expected results before the function name (where we previously had void
).[/!]
[!scala]You simply have to add a colon (:) after the parenthesis and write the type of data that your function 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 function body to
specify that the computation is done (the function is not further executed), and that
the result is the value following the return
keyword.
[!java|c]double pi() {[/!][!scala]def pi(): Double = {[/!][!python]def pi():[/!] return 3.14159[!java|c];[/!] [!java|scala|c]}[/!]
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 fact, 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
function without finding any return
, it cannot know what actual
value to give back to the function caller.
Moreover, return
interrupts immediately the function execution
(why bother looking further when you know the function result?). So, if there is
some code after a return
, it must be an error and the compiler
warns you.
[!java|scala|c][!java]boolean [/!][!c]int [/!][!scala]def [/!]isFrontFree()[!scala]:Boolean =[/!] { if (isFacingWall() == [!java|scala]true[/!][!c]1[/!]) { return [!java|scala]false[/!][!c]0[/!]; /* no code allowed here */ } else { return [!java|scala]true[/!][!c]1[/!]; /* 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[/!]
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 function is to use a boolean variable called
seenBaggle
indicating whether or not we saw a baggle so far. Its
initial value is '[!python]False[/!][!scala|java]false[/!][!c]0[/!]'.
Then, move 6 steps forward (the world contains 7 cells and we already are
on 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 function haveBaggle
works, proceed to next exercise.