![]() |
Introduction to Functions |
|
Functions Fundamentals |
|
Creating a Function |
In the previous lesson, we had an introduction to functions but we used only built-in functions. Most of the times, you will want to create your own functions that perform a task of your choice. The fundamental formula used to create a function is:
let FunctionName Parameters = Function Body
You start with the let keyword, followed by a name for the function. The name of the function binds it to the value it would produce. The name follows the rules we saw in the previous lesson. Here is an example:
let calculate . . .
After the name of the function, type one or a list of parameters represented each by at least a name. If the function is using one parameter, simply type its name. Here is an example of a function that takes one parameter:
let calculate x . . .
If the function is taking more than one argument, separate them with spaces. Here is an example:
let calculate x y z . . .
If you want to specify the data type of a parameter, include the parameter in parentheses, type : followed by its data type. Here is an example:
let calculate (x:int) . . .
If the function takes more than one parameter and you want to specify the data type of each, include the combination of the variable name and its data type in parentheses. Here is an example:
let calculate (x:int) (y:int) (z:int) . . .
To define the function, after the parameter or the list of parameters, type the assignment operator "=" followed by a section that is referred to as the function's body. In the body section, use the parameter(s) as you see fit. Here is an example that indicates that a function named show receives a string parameter and will display the value of that parameter using the printfn function:
let show message = printfn "%s" message;
If a function is short, you can define it in one line. If not, you must use more than one function. In this case, the lines after the first must be indented. Here is an example:
let show message =
printfn "%s" message;
In the body of a function, you can declare a new variable or as many variables as you want. The variables declared in a function are called local variable. Here is an example of a local variable:
let calculate (x:int) (y:int) (z:int) =
let addition = x + y + y;
printfn "%d + %d + %d = %d" 2 4 6 addition
|
Calling a Function |
After creating a function, to see its result, you must call it. To call the function, type its name followed by a space and the parameter or the list of parameters. Here is an example:
let show message = printfn "%s" message; show "Welcome to the wonderful world of functional programming!";
If the function is taking more than one parameter, when calling it, type the name of the function followed by the value of each parameter. The values are separated by spaces. In the same way, if the function has a local variable, it is called the same way. Here is an example:
let calculate (x:int) (y:int) (z:int) =
let addition = x + y + y;
printfn "%d + %d + %d = %d" 2 4 6 addition;
calculate 2 4 6;
If you create function that uses a long body, which means its implementation spans various lines, and if you call that function many times, everytime the function is called, the compiler must "travel" from the section where the function is called to where it is defined. This travel can be done too many times, putting a big burden on the compiler. On the other hand, if the function is short, to cut this travel, you can ask the compiler to bring the definition of the function to where it is called. To do this, when defining the function, precede it with the inline keyboard. Here is an example:
let inline show message = printfn "%s" message; show "Welcome to the wonderful world of functional programming!";
or:
let inline show (message:string) = printfn "%s" message; show "Welcome to the wonderful world of functional programming!";
|
The Return Value of a Function |
When a function has performed its assignment, it may produce a new value. When calling the function, you can get that value and use it as you see fit, such as displaying its value as we have done so far. In some other cases, you can retrieve that value and store it in a variable. To do this, you can assign the function to a newly declared variable. Here is an example:
let calculate (x:int) (y:int) (z:int) = x + y + y; let result = calculate 2 4 6; printfn "%d + %d + %d = %d" 2 4 6 result;
Notice that you can create a function without specifying its return value. If you want to indicate the return type of a function, just before its assignment operator, type : followed by the data type. Here is an example:
let calculate (x:int) (y:int) (z:int) : int = x + y + y; let result = calculate 2 4 6; printfn "%d + %d + %d = %d" 2 4 6 result;
In a function that contains more than one expression, the return value of the function is the last expression it performs.
|
Recursive a Function |
A recursive function is a function that calls itself. Usually, the function performs an operation first, then calls itself to perform the operation again. Because this can be done over and over again, the function must define when and how to stop. To create a recursive function, you use one of the following formulas:
An anonymous function, also called a lambda expression, is a function that doesn't have a name. To create an anonymous function, use the following formula:
fun arguments -> expression
You start with the fun keyword. If your function needs argument, specify them as fun. This is followed by the -> operator and the purpose of the function.
|
|
||
| Previous | Copyright © 2010-2011 FunctionX, Inc. | Next |
|
|
||