
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Create a Function Without Argument but Return a Value in Haskell
We are going to learn how to create a function without argument but return a value using user-defined function. In this article, the user-defined functions are defined that will contain the function definition with some returning value and is being called without passing any arguments to it.
In all the examples, we are going to define user-defined functions to perform the certain tasks that will return some value but are passed without arguments like, factorial, celsiusToFahrenheit, circleArea 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 without passing any arguments to it.
Step 3 ? The result is printed to the console, after the function is being called.
Example 1
In this example, the function factorial takes one integer as an argument, and uses the putStrLn function from the IO monad to print the factorial of the arguments to the console.
factorial :: Int -> Int factorial 0 = 1 factorial n = n * factorial (n - 1) main :: IO () main = do putStrLn "Factorial of a number:" let num = 5 let result = factorial num putStrLn (show result)
Output
Factorial of a number: 120
Example 2
In this example, the function celsiusToFahrenheit takes one integer as an argument, and uses the putStrLn function from the IO monad to print the converted Fahrenheit value of the Celsius Degree of the argument to the console.
celsiusToFahrenheit :: Float -> Float celsiusToFahrenheit c = (9/5) * c + 32 main :: IO () main = do putStrLn "Temperature conversion:" let celsius = 37.0 let fahrenheit = celsiusToFahrenheit celsius putStrLn (show celsius ++ "°C is equivalent to " ++ show fahrenheit ++ "°F")
Output
Temperature conversion: 37.0°C is equivalent to 98.6°F
Example 3
In this example, the function circleArea takes one integer as an radius argument, and uses the putStrLn function from the IO monad to print the area of the circle using radius as the argument to the console.
circleArea :: Float -> Float circleArea r = pi * r * r main :: IO () main = do putStrLn "Area of a circle:" let radius = 5.0 let area = circleArea radius putStrLn (show area)
Output
Area of a circle: 78.53982
Example 4
In this example, the function reverseString takes one string as an argument, and uses the putStrLn function from the IO monad to print the string and its reverse using the string as argument to the console.
reverseString :: String -> String reverseString str = reverse str main :: IO () main = do putStrLn "Reversing a string:" let original = "Hello, world!" let reversed = reverseString original putStrLn (original ++ " reversed is " ++ reversed)
Output
Reversing a string: Hello, world! reversed is !dlrow ,olleH
Example 5
In this example, the function isEven takes one integer as an argument, checks whether it is even or not and uses the putStrLn function from the IO monad to print the result to the console.
isEven :: Int -> Bool isEven n = n `mod` 2 == 0 main :: IO () main = do putStrLn "Checking if a number is even:" let number = 4 let result = isEven number putStrLn (show number ++ " is " ++ (if result then "even" else "odd"))
Output
Checking if a number is even: 4 is even
Example 6
In this example, the function maxOfTwoNumbers takes two integers as arguments, compare them and uses the putStrLn function from the IO monad to print the maximum of two numbers to the console.
maxOfTwoNumbers :: Int -> Int -> Int maxOfTwoNumbers x y = if x > y then x else y main :: IO () main = do putStrLn "Maximum of two numbers:" let x = 3 let y = 7 let max = maxOfTwoNumbers x y putStrLn (show x ++ " and " ++ show y ++ ": " ++ show max)
Output
Maximum of two numbers: 3 and 7: 7
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 even without passing any arguments and returning some value in the function definition. A user-defined function without an argument but with a return value in Haskell is a function that doesn't take any inputs, but instead returns a value as its output.