- ELM Tutorial
- ELM - Home
- ELM - Introduction
- ELM - Environment Setup
- ELM - Basic Syntax
- ELM - Data Types
- ELM - Variables
- ELM - Operators
- ELM - Decision Making
- ELM - Loop
- ELM - Functions
- ELM - String
- ELM - List
- ELM - Tuples
- ELM - Records
- ELM - Error Handling
- ELM - Architecture
- ELM - Package Manager
- ELM - Messages
- ELM - Commands
- ELM - Subscriptions
- ELM Useful Resources
- ELM - Quick Guide
- ELM - Useful Resources
- ELM - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Elm - Functions
Functions are the building blocks of an Elm program. A function is a set of statements to perform a specific task.
Functions organize the program into logical blocks of code. Once defined, functions may be called to access code. This makes the code reusable. Moreover, functions make it easy to read and maintain the program’s code.
Steps to using a function
There are three steps to using a function −
A function declaration tells the compiler about a function's name, return type, and parameters. The syntax for declaring a function is given below −
The function declaration specifies the following −
Name of the function.
Data type of the parameters. This is optional as a function may or may not have parameters.
Data type of the value, which the function will return. Functions in Elm must always return a value as Elm is a functional programming language. Unlike functions in other programing languages, Elm functions do not use the return keyword to return a value.
Function Definition or Function Implementation
A function definition provides the actual body of the function. A function definition specifies how a specific task would be done. The syntax for defining a function is as given below −
fn_name parameter1 parameter2 = statements
Invoking or Calling a Function
A function must be called so as to execute it. The syntax for calling a function is given below −
fn_name parameter1 parameter2
The following code defines a function greet. The function returns a string "Hello".
> greet = \ | if True then \ | "Hello" \ | else \ | "GoodBye" "Hello" : String > greet "Hello" : String
Parameters are a mechanism to pass values to a function. The values of the parameters are passed to the function at the time of function invocation.
The following example defines a function fn_add. The function accepts two numbers as parameters and returns their sum. Try the following in elm REPL −
> fn_add x y = x+y <function> : number -> number -> number > fn_add 10 20 30 : number
The following example defines a function sayHello. The sayHello function accepts and returns a String value as parameter and returns a String.
> sayHello name = "Hello "++ name <function> : String -> String > sayHello "Tutorialspoint" "Hello Tutorialspoint" : String >
To understand pipe operator |>, let us consider an example where we have a list of different strings ["a","b","c"] . Now we need a single string, which is separated by −
The following example shows how to do that with String.join
> String.join "-" ["a","b","c","d","e","f"] "a-b-c-d-e-f" : String
The same action can be performed by using a pipe operator |>. The pipe operator can be used to chain multiple function calls.
> ["a","b","c","d","e","f"] |> String.join "-" "a-b-c-d-e-f" : String > ["a","b","c","d","e","f"] |> List.reverse |> String.join "-" "f-e-d-c-b-a" : String
In the first example, we are chaining the list to join method. In the second case, the same list is piped to reverse function and thereafter piped to join. So, the list is displayed in reversed and joined.