printfn in a nested function call is not printing anything on console

I've below mentioned code written in F# language:

let f () = 
    printfn "This function will print f"
    0

let xs (x) = 
    printfn "This function will print xs"
    f
    ()


[<EntryPoint>]
let main ( argv : string[]) =
    xs 4 |> ignore
    0

When I run this program I get only one print statement (present in sx function) on console:

This function will print xs

The statement This function will print f present inside f function is not getting printed. Interestingly enough, when I pressed F11 to step into function f while debugging the control isn't going there at all even though there is a break point inside function f.

More intesrestingly, if I introduce a parameter in function f then it all starts working. So if I change the function f to:

let f (x) = 
    printfn "This function will print f"
    0

let xs (x) = 
    printfn "This function will print xs"
    f 2
    ()


[<EntryPoint>]
let main ( argv : string[]) =
    xs 4 |> ignore
    0

Then this print statement starts to work:

This function will print f

This function will print xs

Can someone explain this strange behavior? I'm sure I'm missing something very basic related to F# world or its functional aspect may be. I've a C# background.

1 answer

  • answered 2018-05-16 06:57 Nikon the Third

    You are not calling f, you are just placing a function there without giving it the parameters it needs.

    That's why it works when you give it a parameter. You also have to do the same with the empty braces:

    let xs (x) = 
        printfn "This function will print xs"
        f ()
        ()
    

    In case you wanted the final () to act as the parameters to f, you have to add some more indentation:

    let xs (x) = 
        printfn "This function will print xs"
        f
         ()