Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define a F# function and avoid the return value

Tags:

f#

define a function that has a single parameter of type string which which displayed on console. invoke the function passing it a message. ensure the function ignores any returned value

open System    
let NoReturnFunction msg =
    Console.WriteLine(string(msg))


NoReturnFunction "Hello World"

I am in a trouble to how to avoid return value of function

like image 299
user1717327 Avatar asked Apr 10 '26 09:04

user1717327


1 Answers

In this case there is no work to do because the WriteLine method doesn't return any values. It's a void returning function. In general though the way to ignore a return value in F# is to use the ignore function.

1 + 2 |> ignore 

Couple of minor nit picks on the your code sample. The first is you can avoid the cast to string by simply typing the parameter. Second in F# it's more idiomatic to use the printfn function instead of Console.WriteLine

let NoReturnFunction (msg : string) = 
    printfn "%s" msg 
like image 161
JaredPar Avatar answered Apr 18 '26 04:04

JaredPar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!