 Rather than just use the operations predefined in the language, programmers also define their own operations, called functions. Functions are also sometimes called routines, subroutines, procedures, or in some context, methods. Function though is the most common term. Once the programmer has defined a function and given it a name, they can invoke or call as we say, that function just like they invoke one of the built-in operations. In parentheses, you write the name of the function, followed by however many operands are expected by the function. To create a function in Pigeon, we use a function statement, beginning with a reserved word function. That's followed by the name you choose for the function and identifier, and after that parameters, which we'll explain in a moment, and then indent it on the following lines, you write the body, a series of statements that execute when the function is invoked. So here for example, we create a very simple function called Eric. This function Eric has no parameters and it just has one statement in its body, print hello. So having created this function, we can then invoke it by simply putting its name in parentheses. And because it takes no operands, we just write the function name in parentheses and that's it. When we invoke this function, Eric, all it does is print hello. Now functions like operations can take input values, but rather than calling those input values operands, we usually call them arguments. And when we invoke a function, the arguments are passed to parameters, which are variables in the function that receive the input values. So here, for example, we create a function we call Ryan, and we give it two parameters, the first called bat, and the second called goat. Then in the body of this function, what we'll do is subtract the value of bat from goat and print the resulting value. So if we then invoke the function with the arguments four and negative nine, the value four gets passed to the parameter bat, and the value negative nine gets passed to the parameter goat. And so in the first invocation of this function, when we subtract bat from goat, four from negative nine, we get negative 13, and the function will print negative 13. In the second invocation of Ryan here, the argument three is passed to bat, and the argument five is passed to goat. So when we subtract bat from goat, we get two. And so the function prints the value two. Operations can not only take input values, they can return an output value. To return an output value, we use a return statement. Here for example, as a function Jerry, which simply returns the value three. When we invoke Jerry here, it returns the value three, and so this code prints three. Now let's write a function that might actually be useful, a function that computes and returns a factorial. In case you don't recall, a factorial of a number takes that number and multiplies it by all integers down to one. So the factorial of three, for example, is three times two times one. The factorial of zero, however, is a special case which always returns one. So here's one way we could write a factorial function in Pigeon. Let's call the function factorial and give it a single parameter named n. In the body, we use a loop to compute the factorial and store the result in the variable val, which is then returned in the last statement of the body. So if we call the factorial function with the argument four, it'll return 24 because four times three times two times one is 24. And then if we call the function with the argument five, it'll return 120 because five times four times three times two times one is 120. Let's step through exactly what goes on when the argument is four. The parameter n starts with a value of four and then we assign to the variable val the number one. Then we enter the loop if n is greater than zero and n is currently four, so the condition tests true. In the loop, we multiply n times val, that's one times four, yielding four, which we assigned to val. Then we decrement n by subtracting one from n, yielding three and assigning the result to n. So when we test the condition again, is n greater than one? Well, yes, because three is greater than one, so we enter the loop again. And we multiply n times val, n is three, val is four, so that's 12, which we assign to val, and then we decrement n again. When we now test the condition n is two, still greater than one, so we enter the loop once more and multiply two times 12, yielding 24, which we assigned to val. We again decrement n down to one, but now the condition tests false because one is not greater than one, so we exit the loop and return val, which now has the value 24. So our factorial function invoked with the argument four returns 24, and that's what it should return because the factorial of four is 24. Quickly though, consider the special case of zero. If we call the function with an argument of zero, val is assigned one as usual, but then the condition of the loop tests false right off the bat, so we never enter the loop at all, instead just returning the value one, which is the correct answer for the factorial of zero.