In this one i am gonna talk about how we can create and call functions in F#.
- In F# functions are also values which is evident because they use the same syntax with the let keyword as assignments.
- When you are writing functions that span multiple lines you need to use indentation to define the function structure.
- Functions are nestable in F#.
- In F# we don’t need to expliclitly define the return value of the function. The last expression which is evaluated in the function would become the function’s return value automatically.
- As you have already seen in the previous posts that the inference engine of F# is pretty great and as you would expect input parameters and return values are inferred automatically by the compiler and hence the signature of the function is when we are calling a function in F# automatically defined.
- When we are calling a function in F# we don’t need to put any delimiters. We need to use the parentheses for specifying priority only for we are making nested calls.
- Lets declare a function and follow it with space and followed by the parameter followed by the evaluation and followed by the return value.
- Now lets create a function with two input parameters. Now this function will be a multi line function. keep in mind when you are writing a multi line function then indentation is very important and if you do not follow the indentation then you might get the following warning.
- Now lets talk about the add function. As you could see in the image below when i take my mouse over to the intAdd function then we see that intAdd is a value if type int which goes to int goes to int. Let me explain what i mean by that. Add is a value of type int as all values in F# have a type. int goes to int goes to int means there are are two int parameters coming in and one int value coming out. It might seem odd to you that the two int inputs are separated by the go to operator but thats how it is in F# as all the functions are in carried format. We would talk more about that later.
- As you know that functions are just values we could nest them according to our requirement. Lets create a new function intAddNested. Here intAddNested is the function and we have nested a value inside it with result as the return value.
- Now lets call the function that we just created. We dont need to use paranthesis in the same way as we don’t need to use the paranthesis while declaring the function. To call the function just type the following code in your F# program. Here add5and3 is the name of the type and the call to the function is followed by the =.
- Now lets see how we can use the return value from one function in the call to the other function. Here we could see we have used the return value of the intSquare function in intAdd function. As in standard maths the part of the function call inside the paranthesis is called first and then the other part o fthe function is executed.
- You can select all the code and send it to interactive to evaluate your code.
Any questions, comments and feedback are most welcome.