Functions [4/9]
Functions
User-defined functions are created by the keyword fn.
The given program defines a function named triple. The function triple has one parameter, which is named a and it is of type num.
The function triple is called three times in this program. Each function call causes the function to be executed.
The execution of function triple produces a return value. The return value of a function is given after the two colon symbols (::). The return value of function triple is equal to the result of expression a*3.
The type of return value is given after the keyword fn and before the function name. The type of return value is num.
Functions are a crucial aspect of programming languages. Functions describe computations.
The given program defines a function named square. The function square has one parameter, which is named x and it is of type num.
Return value of function square is computed as a result of the expression x*x. Therefore, the expression square(5) evaluates to value 25 (in the same manner as, for example, expression 4+3 evaluates to value 7).
The type of return value is given after the keyword fn. The type of return value is num.
The function square is called four times in this program. In the first call, the argument of function square equals 5, in the second call the argument equals 2, and in the third call it equals 10.
On each call of a function, the values of arguments get assigned to the function's parameters. In the last call of function square, the argument is the expression 3+1. The value 4 of the argument gets assigned to the parameter x.
This example program defines a function Mul. The function Mul has two parameters, named a and b, both of type num.
In the first call of function Mul, the value 4 of the first argument gets assigned to parameter a, and the value 3 of the second argument gets assigned to parameter b.
A function call is a primary expression, therefore it can be used as a part of a more complex expression.
Here is an alternative way to write a definition of the function Mul.
The function body of function Mul is given as a statement block. The result of the function is given by the return statement.
The function body is executed once on each call of the function.
This program defines a function named Greater. The function Greater has two parameters, named a and b.
The function Greater has two parameters, therefore it accepts two arguments. It returns the greater argument of the given two.
The user-defined function IsBetween returns the value true when the value of parameter x is between values of parameters a and b.
The type of return value of function IsBetween is bool. Therefore, this function can only return values true or false.
The first call of function IsBetween returns true because 4 is between 2 and 9.
The second call of function IsBetween returns true because 5 is between 1 and 8.
The third call of function IsBetween returns false because 10 is not between 1 and 8.
Since the execution of a function is completed when the return statement is executed, the function IsBetween can be written in this shorter way.
Commented out at the bottom is an even shorter way to write function IsBetween. To explain it, let's say that parameter a=3, b=7, x=10. The result of the function is the value of expression a<x and x<b. In this case, the expression a<x and x<b evaluates to false. Therefore, the value false is the return value of the function when a=3, b=7, x=10.
Here is an improvement of the function IsBetween. It allows the values of parameters a and b to be given in the opposite order.
Commented out is an demonstration of an even shorter way to write this function. The parentheses were added only for clarity of expression; they can be removed because the operator and has greater priority than the operator or.
This program will turn the disc yellow when the pointer is above the disc.
The user-defined function isInsideDisc accepts three arguments, and returns a value of type bool, indicating whether a given point p is inside a given disc. The signature of this function is:
- bool isInsideDisc(
- point2D center,
- num radius,
- point2D p
- )
In the function isInsideDisc, the member function .dist returns the distance between points center and p. This member function was demonstrated in the previous chapter.
Exercise:
(Challenging!) Can you write the function isInsideDisc in an even shorter way, i.e. without the statement block for function body?
Solution
fn bool isInsideDisc(point2D center, num radius, point2D p)
:: center.dist(p) < radius;
<< F2:Prev - - F4:Next >>