Unexpected linefeed?

Please bear with me folks, remember there are no stupid questions. I'm an absolute beginner (did a fair amount of Pascal years ago).


I'm getting a surplus line feed which shouldn't be there.


var plustax:Double = 0

/ Variable plustax declared here, not inside the function as the book has it. Bugger! */


func printTaxed (pretax:Double) {

plustax = pretax * 1.2

print(plustax)

}


printTaxed(pretax:100)

printTaxed(pretax:25.78)

printTaxed(pretax:105.789)


Gives this output -


120.0

30.936

126.9468


Fine so far.



But this


func printTaxed2 (pretax:Double, taxrate:Double) {

plustax = pretax * taxrate

print(plustax)

}

print(printTaxed2(pretax:150, taxrate: 4/3)) //surplus brackets ‘()’ on next line?

print(printTaxed2(pretax:150, taxrate: 1.3333))


Gives me this -


200.0

()

199.995

()



Whence the linefeed plus () ?

Replies

Whence the linefeed plus () ?

Your usage of `print` outputs both.


Do you know that when you write `print(expr)`, `expr` is evaluated and then the result of evaluation is printed with a following newline as terminator.


So, in your line of code:

print(printTaxed2(pretax:150, taxrate: 4/3))

First, `printTaxed2(pretax:150, taxrate: 4/3)` is evaluated. While its evaluation, `200.0` is printed with terminator.

And the result of the evaluation is void value, as you declare the function `printTaxed2(pretax:taxrate:)` as a void function.


When a void value given, the `print` outputs `()` with terminator.


Generally, you should not write a void function call as a parameter for `print`, unless you want to see the void value `()`.

OK, thank you - I don't fully understand what you are saying, but it gives me something to work to.


I'll try


var anotherVariable = pretax*taxrate

print (anotherVariable)


Must admit, I'm groping in the dark rather!

Light dawns!


print(printTaxed2(pretax:150, taxrate: 4/3))

print(printTaxed2(pretax:150, taxrate: 1.3333))


has a surplus print command.


printTaxed2(pretax:150, taxrate: 4/3)

printTaxed2(pretax:150, taxrate: 1.3333)


doesn't, and works as it should.


Dear me, error spotting needs improvement!

A good way to improve your skills in such a case is to use debugger and go step by step (function key F6), after setting a breakpoint at the beginning (here at print(printTaxed2(pretax:150, taxrate: 4/3)))


You will see which statement precisely print the ()

Thanks, I'll try that.