next up previous
Next: Doing something with our Up: The Frightened Freshers Guide Previous: Introduction

What is a function?

A lot of the time when programming in haskell you will come across the wonderous word function. A function is something that takes one or more inputs, does some processessing and then outputs something else. In haskell, when defining a function you need to tell the compiler what your inputs and outputs are and of what type they are.

Here is a simple example for a function called addTwoNumbers that shows the function declaration. language=Haskell
\begin{lstlisting}
addTwoNumbers :: Int->Int->Int
\end{lstlisting}

You can see from this that the function is called addTwoNumbers and that it two Int inputs and a single Int output. In a more general form, the functions are declared as functionName :: type1parameter->type2parameter->returnType, note that you may have as many inputs as you like but there is only ever a single thing returned.

All functions that we know of will be declared in this way with a method name, a pair of colons and then the inputs and output of the method.

Your function declarations should include a number of things. Firstly, a type declaration to name the function and decide on what inputs and outputs your function will take and return. A comment describing what the function does is also very important. Examples showing what happens when certain things are inputted, what is outputted. Then the actual function definition itself (Which we will see more of in a second) and finally testing the function to see if the function agrees with the examples.


next up previous
Next: Doing something with our Up: The Frightened Freshers Guide Previous: Introduction
Tom Carlson 2006-04-11