- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Haskell Program to create a function with arguments and a return value
In this article, we are going to understand how to create a function with argument and a return value in Haskell using a user-defined function. The user-defined functions are defined that will contain the function definition with some returning value and is being called by passing desired arguments to it. These functions perform various operations as per the definition.
In all the examples, we are going to define user-defined functions to perform certain tasks that will return some value and are passed with some arguments like, add, mult, maxOfTwo and other functions.
Algorithm
Step 1 − The user defined function is defined by writing its definition with a return value.
Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the user defined function is being called by passing the desired arguments to it.
Step 3 − The result is printed to the console, after the function is being called.
Example 1
In this example, the add function takes two Int arguments a and b and returns their sum as its output. The main function demonstrates how to use the add function, by passing x and y as arguments and printing the result. The return type of the add function is Int.
add :: Int -> Int -> Int add a b = a + b main :: IO () main = do putStrLn "Sum of two numbers:" let x = 5 let y = 10 let total = add x y putStrLn (show x ++ " plus " ++ show y ++ " is " ++ show total)
Output
Sum of two numbers: 5 plus 10 is 15
Example 2
In this example, the mult function takes two Int arguments a and b and returns their product as its output. The main function demonstrates how to use the mult function, by passing x and y as arguments and printing the result. The return type of the mult function is Int.
mult :: Int -> Int -> Int mult a b = a * b main :: IO () main = do putStrLn "Product of two numbers:" let x = 5 let y = 10 let product = mult x y putStrLn (show x ++ " times " ++ show y ++ " is " ++ show product)
Output
Product of two numbers: 5 times 10 is 50
Example 3
In this example, the maxOfTwo function takes two Int arguments a and b and returns the maximum of them as its output. The main function demonstrates how to use the maxOfTwo function, by passing x and y as arguments and printing the result. The return type of the maxOfTwo function is Int.
maxOfTwo :: Int -> Int -> Int maxOfTwo a b = max a b main :: IO () main = do putStrLn "Maximum of two numbers:" let x = 5 let y = 10 let maximum = maxOfTwo x y putStrLn (show x ++ " and " ++ show y ++ " has a maximum of " ++ show maximum)
Output
Maximum of two numbers: 5 and 10 has a maximum of 10
Example 4
In this example, the longestString function takes list of strings as arguments and returns the length of the maximum string as its output. The main function demonstrates how to use the longestString function, by passing list of strings as arguments and printing the result. The return type of the longestString function is Int.
longestString :: [String] -> Int longestString xs = maximum (map length xs) main :: IO () main = do putStrLn "Length of the longest string in a list:" let xs = ["hello", "world", "this", "is", "Haskell"] let longest = longestString xs putStrLn (show xs ++ " has a longest string with a length of " ++ show longest)
Output
Length of the longest string in a list: ["hello","world","this","is","Haskell"] has a longest string with a length of 7
Example 5
In this example, the sumEven function takes list of integers as arguments and returns the length of the sum of even integers as its output. The main function demonstrates how to use the sumEven function, by passing list of integers as arguments and printing the result. The return type of the sumEven function is Int.
sumEven :: [Int] -> Int sumEven xs = sum (filter even xs) main :: IO () main = do putStrLn "Sum of all even numbers in a list:" let xs = [1, 2, 3, 4, 5] let evenSum = sumEven xs putStrLn (show xs ++ " has a sum of even numbers of " ++ show evenSum)
Output
Sum of all even numbers in a list: [1,2,3,4,5] has a sum of even numbers of 6
Conclusion
In Haskell, the user-defined functions are functions that are created by the programmer to perform specific operations. The users can define functions as per their need by passing any desired arguments and returning some value in the function definition. A user-defined function with some arguments and with a return value in Haskell is a function that takes some inputs, and returns a value as its corresponding output.