## Functions

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]}[/!]

Si può anche utlizzare la parola chiave `return` solamente per interrompere l'esecuzione del metodo, non ritornando alcun risultato. Ovviamente in questo caso non bisognerà specificare un valore di ritorno.

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[/!]

### Traguardo dell'esercizio

You will once again write a function 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 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).

Alla fine dovremo tornare indietro di 6 passi e ritornare il valore della variabile `baggleVisto` al chiamante.

Questo esercizio si distinge un po' perché ci sono tre mondi iniziali, ognuno con il suo specifico objective. Il tuo codice deve funzinoare per tutti loro. Nota che il menù scorrevole world selection (al di sotto dello speed slider) permette di cambiare il mondo osservato.

When your function `haveBaggle` works, proceed to next exercise.