Jump to content

Do the functions have priority over ConsoleWrite?


Recommended Posts

Hello,

I wanted to call a function inside ConsoleWrite:

ConsoleWrite("Hello " & _write15() & " B" & @CRLF)
ConsoleWrite("END" & @CRLF)


Func _write15()
    ;ConsoleWrite("15")
    ConsoleWrite(5*3)
EndFunc

I was expecting this output: 

Hello 15 B
END

But what I got is: 

15Hello 0 B
END

If I change the function like this, of course it will work (without calling function): 

ConsoleWrite("Hello " & 5*3 & " B" & @CRLF)
ConsoleWrite("END" & @CRLF)


Func _write15()
    ;ConsoleWrite("15")
    ConsoleWrite(5*3)
EndFunc

 

As you can see, ConsoleWrite executed the _write15() before writing Hello, even Hello comes before the function. 

In addition to that, there is a "0" in the middle of my output. I have no idea where it came from. 

 

Please note that: I know we can assign the output of _write15() to a variable and call that variable in ConsoleWrite or maybe some easy solutions. But the goal is to call the function within ConsoleWrite. 

TY.

Link to comment
Share on other sites

The ConsoleWrite in the function completes before the one in the main script, so the output you saw makes sense to me. You should return the result from the function if you want it to appear inline with the other text --

ConsoleWrite("Hello " & _write15() & " B" & @CRLF)
ConsoleWrite("END" & @CRLF)


Func _write15()
    Return 5*3
EndFunc

 

Link to comment
Share on other sites

you can call in this order

ConsoleWrite("Hello ")
_write15()
ConsoleWrite(" B" & @CRLF & "END" & @CRLF)


Func _write15()
    ;ConsoleWrite("15")
    ConsoleWrite(5*3)
EndFunc

or

ConsoleWrite("Hello " & _write15() & " B" & @CRLF)
ConsoleWrite("END" & @CRLF)


Func _write15()
    ;ConsoleWrite("15")
    Return 5*3
EndFunc

 

Edited by ioa747

I know that I know nothing

Link to comment
Share on other sites

  • Moderators

Your post confused me, as it is almost an exact copy of Danp2's. Perhaps you guys posted at the exact same time.

"Profanity is the last vestige of the feeble mind. For the man who cannot express himself forcibly through intellect must do so through shock and awe" - Spencer W. Kimball

How to get your question answered on this forum!

Link to comment
Share on other sites

21 hours ago, ioa747 said:

you can call in this order

Yeah, I know that order will work. But if the script is allowing me to use in the way that I use, without any compile errors, then it should be fine. And you can see in the below example that, I'm not separating in different lines. 

 

The thing is, if I want to use any inbuilt function such as String Length: 

ConsoleWrite("Hello " & StringLen("abcdefg") & " B" & @CRLF)
ConsoleWrite("END" & @CRLF)


;I'm not dividing the 1st line into 3 parts as you suggested: 
;ConsoleWrite("Hello ")
;ConsoleWrite(StringLen("abcdefg"))
;ConsoleWrite(" B" & @CRLF)
;ConsoleWrite("END" & @CRLF)

My output will be as below as expected, which is good and correct: 

Hello 7 B
END

Which makes me think that I can use my function inside the console write too. 

 

 

Assume that my function is not doing any arithmetic equation, just another console write: 

ConsoleWrite("Hello " & _write15() & " B" & @CRLF)
ConsoleWrite("END" & @CRLF)


Func _write15()
    ConsoleWrite("15")
    ;Return 5*3
EndFunc


;This gives the below output
;15Hello 0 B
;END

If I change that _write15() function to Return("15"), that will work too. 

 

So, I'm confused where that 0 comes from when I call _write15 which has ConsoleWrite inside. 

Also writing that "15" in the beginning of the output is confusing too. 

TY.

Link to comment
Share on other sites

5 minutes ago, taylansan said:

So, I'm confused where that 0 comes from when I call _write15 which has ConsoleWrite inside. 

From the help file --

Quote

Use the Return keyword to exit the function. Unlike built-in functions, user-defined functions return 0 unless another return value is specified.

So that explains where the 0 comes from. I believe I addressed your other confusion in my prior post, so maybe you could elaborate on why you are still confused.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...