Learning F# — Part 4 — Functions

Last time we saw how we can create functions. This is the function we created.

> let f x = 2*x + 1;; val f : x:int -> int

We will see how to invoke this function. Notice that it takes an integer argument. So, to invoke the function we need to give it an integer and it will return back an integer after it has done its job. Functions do some work. It is a fundamental unit of program execution. You can execute a function by invoking it. This is how you execute the above function.

> f 4;; val it : int = 9

Notice that we used the name of the function, in our case it is f, and passed in an integer argument as 4, separated by a space. The compiler returns with the value 9. it is just a predefined identifier by the compiler which is bound to the value that was last evaluated. In our case 9, and it is an integer.

4 -> [f] -> 9

The above represents how our function works. We gave 4 as an input to our function and it gave 9 as an output.

Lets try this

f 200;; val it : int = 401

When we give 200 to the function f it returns 401. Our function is working correctly. Although you write a test to check if your function is working correctly. All programs must have tests. When we write our programs we will write tests as well. Let’s get back to our function here for now.

200 -> [f] -> 401

In this case the function takes in 200 and returns 401. Both integers, the input and output. We can represent our function like this.

int -> [f] -> int

So we have function named as f, which takes an integer as input and returns an integer as output. In other words, we say function f, maps an int to an int. For instance, f mapped 200 to 401.

We could have represented it this way as well

[f]: int->int

Very similar to what the compiler told us in the FSI, when we created the function

val f : x:int -> int

Functions are values, hence f is also a val(ue). The argument that f takes has been bound to x (that’s just the name we gave, if you recall). And of course, the mapping of int to int.

In the function definition, the stuff after the ‘=’ sign is called the body of the function. If the function accepts any arguments, then the function has parameters. The output of the function is its return value. The type of the return value is called, no surprise here, the return type. Most functions have names.

let f x = 2*x + 1;;

For our function,

Name — f,

Parameter(s) — x

Parameter type — x:int,

Return type- int,

Body — 2*x + 1

Every function would have those properties.

Signature; The signature of a function would give you enough information about the properties of a function. val f : x:int -> int. This is a signature of the function. It gives you a concise information about the function, all but the body.

Now go ahead and create a function which squares a number.

let squared n = n*n;;

This is the signature the compiler responds with

val squared : n:int -> int

This gives you enough information about the function. The name of the function, squared, definitely helps if somebody else is going to use your function. It paints a picture for the programmer who would use this function. That programmer could possibly be your colleague or it could be yourself. So from now on, make sure your function names are readable and does exactly what the function name says.

Execute your function to see if it works as expected.

squared 5;;

Looks good.

Now go ahead and create a function that takes two input numbers and returns the sum of its squares. Note that you can re-use the squared function you already created. Also, remember to give it a good name.

As a programmer there are quite few principles you would want to follow. This separates the good and bad programmers. DRY (Do not repeat yourself) is one such principle. By re-using the squared function you do not have to repeat the code for squaring operation. This becomes more evident for more complex functions. Read more about the principle here.

Here’s that function. Note the signature too.

> let sumOfSqaures x y = squared x + squared y;; val sumOfSqaures : x:int -> y:int -> int

Give the newly created function a spin by executing it.

> sumOfSqaures 3 4;;

Practice by creating more functions. Remember to keep it DRY.

Until next time. Cheers.


Originally published at vijeshsalian.wordpress.com on June 12, 2018.

Like what you read? Give Vijesh Salian a round of applause.

From a quick cheer to a standing ovation, clap to show how much you enjoyed this story.