# F Sharp Programming/Values and Functions

Compared to other .NET languages such as C# and VB.Net, F# has a somewhat terse and minimalistic syntax. To follow along in this tutorial, open F# Interactive (fsi) or Visual Studio and run the examples.

## Declaring Variables

The most ubiquitous, familiar keyword in F# is the let keyword, which allows programmers to declare functions and variables in their applications.
For example:
This declares a variable called x and assigns it the value 5. Naturally, we can write the following:
z now holds the value 15.
A complete program looks like this:
The statement printfn prints text out to the console window. As you might have guessed, the code above prints out the values of x, y, and z. This program results in the following:
   Note to F# Interactive users: all statements in F# Interactive are terminated by ;; (two semicolons). To run the program above in fsi, copy and paste the text above into the fsi window, type ;;, then hit enter.

### Values, Not Variables

In F#, "variable" is a misnomer. In reality, all "variables" in F# are immutable; in other words, once you bind a "variable" to a value, it's stuck with that value forever. For that reason, most F# programmers prefer to use "value" rather than "variable" to describe x, y, and z above. Behind the scenes, F# actually compiles the "variables" above as static read-only properties.

## Declaring Functions

There is little distinction between functions and values in F#. You use the same syntax to write a function as you use to declare a value:
add is the name of the function, and it takes two parameters, x and y. Notice that each distinct argument in the functional declaration is separated by a space. Similarly, when you execute this function, successive arguments are separated by a space:
This assigns z the return value of this function, which in this case happens to be 15.
Naturally, we can pass the return value of functions directly into other functions, for example:
This program outputs:
Notice that I have to surround the calls to add and sub functions with parentheses; this tells F# to treat the value in parentheses as a single argument.
Otherwise, if we wrote printThreeNumbers 5 add 10 7 sub 20 8, its not only incredibly difficult to read, but it actually passes 7 parameters to the function, which is obviously incorrect.

### Function Return Values

Unlike many other languages, F# functions do not have an explicit keyword to return a value. Instead, the return value of a function is simply the value of the last statement executed in the function. For example:
This function takes an integer parameter and returns a string. As you can imagine, the F# function above is equivalent to the following C# code:
Just like C#, F# is a strongly typed language. A function can only return one datatype; for example, the following F# code will not compile:
If you run this code in fsi, you get the following error message:
The error message is quite explicit: F# has determined that this function returns a string, but the last line of the function returns an int, which is an error.
Interestingly, every function in F# has a return value; of course, programmers don't always write functions that return useful values. F# has a special datatype called unit, which has just one possible value:. Functions return unit when they don't need to return any value to the programmer. For example, a function that prints a string to the console obviously doesn't have a return value:
This function takes unit parameter and returns. You can think of unit as the equivalent to void in C-style languages.

### How to Read Arrow Notation

All functions and values in F# have a data type. Open F# Interactive and type the following:
F# reports the data type using chained arrow notation as follows:
Data types are read from left to right. Before muddying the waters with a more accurate description of how F# functions are built, consider the basic concept of Arrow Notation: starting from the left, our function takes two int inputs and returns a string. A function only has one return type, which is represented by the rightmost data type in chained arrow notation.
We can read the following data types as follows:
int -> string
    takes one int input, returns a string
float -> float -> float
    takes two float inputs, returns another float
int -> string -> float
    takes an int and a string input, returns a float
This description is a good introductory way to understand Arrow Notation for a beginner—and if you are new to F# feel free to stop here until you get your feet wet. For those who feel comfortable with this concept as described, the actual way in which F# is implementing these calls is via currying the function.

### Partial Function Application

While the above description of Arrow Notation is intuitive, it is not entirely accurate due to the fact that F# implicitly curries functions. This means that a function only ever has a single argument and a single return type, quite at odds with the previous description of Arrow Notation above where in the second and third example two arguments are passed to a function. In reality, a function in F# only ever has a single argument and a single return type. How can this be? Consider this type:
float -> float -> float
since a function of this type is implicitly curried by F#, there is a two step process to resolve the function when called with two arguments
* a function is called with the first argument that returns a function that takes a float and returns a float. To help clarify currying, lets call this function funX (note that this naming is just for illustration purposes—the function that gets created by the runtime is anonymous).
* the second function ('funX' from step 1 above) is called with the second argument, returning a float
So, if you provide two floats, the result appears as if the function takes two arguments, though this is not actually how the runtime behaves. The concept of currying will probably strike a developer not steeped in functional concepts as very strange and non-intuitive—even needlessly redundant and inefficient, so before attempting a further explanation, consider the benefits of curried functions via an example:
this type has the signature of
int -> int -> int
then this function:
with the type signature of (int -> int). Note that the body of add5ToNumber calls addTwoNumbers with only one argument—not two. It returns a function that takes an int and returns an int. In other words, add5toNumber partially applies the addTwoNumbers function.
This partial application of a function with multiple argument exemplifies the power of curried functions. It allows deferred application of the function, allowing for more modular development and code re-use—we can re-use the addTwoNumbers function to create a new function via partial application. From this, you can glean the power of function currying: it is always breaking down function application to the smallest possible elements, facilitating greater chances for code-reuse and modularity.
Take another example, illustrating the use of partially applied functions as a bookkeeping technique. Note the type signature of holdOn is a function (int -> int) since it is the partial application of addTwoNumbers
Here we define a new function holdOn on the fly just to keep track of the first value to add. Then later we apply this new 'temp' function holdOn with another value which returns an int. Partially applied functions—enabled by currying—is a very powerful means of controlling complexity in F#. In short, the reason for the indirection resulting from currying function calls affords partial function application and all the benefits it supplies. In other words, the goal of partial function application is enabled by implicit currying.
So while the Arrow Notation is a good shorthand for understanding the type signature of a function, it does so at the price of oversimplification, for a function with the type signature of
is actually (when taking into consideration the implicit currying):
In other words, f is a function that takes an int and returns a function that takes an int and returns an int. Moreover,
is a simplified shorthand for
or, in very difficult to decode English: f is a function that takes an int and returns a function that takes an int and returns a function that takes an int and returns an int. Yikes!

### Nested Functions

F# allows programmers to nest functions inside other functions. Nested functions have a number of applications, such as hiding the complexity of inner loops:
The outer function sumOfDivisors makes a call to the inner function loop. Programmers can have an arbitrary level of nested functions as need requires.

### Generic Functions

In programming, a generic function is a function that returns an indeterminate type t without sacrificing type safety. A generic type is different from a concrete type such as an int or a string; a generic type represents a type to be specified later. Generic functions are useful because they can be generalized over many different types.
Let's examine the following function:
F# derives type information of variables from the way variables are used in an application, but F# can't constrain the value x to any particular concrete type, so F# generalizes x to the generic type 'a:
'a -> int
    this function takes a generic type 'a and returns an int.
When you call a generic function, the compiler substitutes a function's generic types with the data types of the values passed to the function. As a demonstration, let's use the following function:
Which has the type 'a -> 'b -> 'b, meaning that the function takes a generic 'a and a generic 'b and returns a 'b.
Here are some sample inputs and outputs in F# interactive:
throwAwayFirstInput 5 "value" calls the function with an int and a string, which substitutes int for 'a and string for 'b. This changes the data type of throwAwayFirstInput to int -> string -> string.
throwAwayFirstInput "thrownAway" 10.0 calls the function with a string and a float, so the function's data type changes to string -> float -> float.
throwAwayFirstInput 5 30 just happens to call the function with two ints, so the function's data type is incidentally int -> int -> int.
Generic functions are strongly typed. For example:
The generic function throwAwayFirstInput is defined again, then the add function is defined and it has the type int -> int -> int, meaning that this function must be called with two int parameters.
Then throwAwayFirstInput is called, as a parameter to add, with two parameters on itself, the first one of type string and the second of type int. This call to throwAwayFirstInput ends up having the type string -> int -> int. Since this function has the return type int, the code works as expected:
However, we get an error when we reverse the order of the parameters to throwAwayFirstInput:
The error message is very explicit: The add function takes two int parameters, but throwAwayFirstInput 5 "this is a string" has the return type string, so we have a type mismatch.
Later chapters will demonstrate how to use generics in creative and interesting ways.