|
Post by klewlis on Nov 21, 2021 17:11:03 GMT -5
not a novice, but been away for awhile so some of my questions may be novice level...
Everything worked until I moved my notification to a function.
'''''calling code'''''
if val(CToInput$) < 1 then
Notify()
exit for
end if
'''''function'''''
function Notify()
notice "NOTICE!" + chr$(13) + "All information must be filled in " + chr$(13) + "before calculations can begin!"
BadInfo = 1
end function
The editor highlights the "Notify()" in the calling code and states "Syntax error"
Any help would be appreciated.
|
|
|
Post by klewlis on Nov 21, 2021 17:17:04 GMT -5
So, perhaps I have to call the function like this?
blah = Notify()
then my function has to say
Notify = blah
|
|
|
Post by tenochtitlanuk on Nov 21, 2021 17:28:47 GMT -5
A more normal way would be to use a subroutine as below. You also need the 'end' to avoid 'falling into' the definition of the sub. It also helps if you post LB code within the 'code' tags..
nomainwin
CToInput$ ="-2" if val( CToInput$) < 1 then call Notify
end
sub Notify notice "NOTICE!" + chr$(13) + "All information must be filled in " + chr$(13) + "before calculations can begin!" BadInfo = 1 end sub
|
|
|
Post by Brandon Parker on Nov 21, 2021 17:42:42 GMT -5
Function returns must be set to a value since all functions return a value in Liberty BASIC even if you will do nothing with the return. If you do not explicitly set the return value of a function when the function exits, it will either return 0 or "" depending on whether it is a numeric return function or a string return function
Here are some examples of how to use functions:
'Returning directly to a Print statement Print "Function add(1, 1) returns: ";add(1, 1) 'Or set it to a variable like: result = add(1, 1) Print "Function add(1, 1) returns: ";result
Print
'Returning directly to a Print statement Print "Function noReturnSet(2, 2) returns: ";noReturnSet(2, 2)
Print
'Or set it to a variable like: 'Notice that it returns 0 result = noReturnSet(2, 2) Print "Function noReturnSet(2, 2) returns: ";result
Print
'Returning directly to a Print statement Print "Function add$(1, 1) returns: ";add$(1, 1) 'Or set it to a variable like: result$ = add$(1, 1) Print "Function add$(1, 1) returns: ";result$
Print
'Returning directly to a Print statement Print "Function noReturnSet$(2, 2) returns: ";noReturnSet$(2, 2)
Print
'Or set it to a variable like: 'Notice that it returns en empty string "" result$ = noReturnSet$(2, 2) Print "Function noReturnSet$(2, 2) returns: ";result$ Wait
Function add(varA, varB) add = (varA + varB) End Function
Function add$(varA, varB) add$ = str$(varA + varB) End Function
Function noReturnSet(varA, varB) Print "Internal to function noReturnSet(";varA;", ";varB;"): ";(varA + varB) End Function
Function noReturnSet$(varA, varB) Print "Internal to function noReturnSet$(";varA;", ";varB;"): ";(varA + varB) End Function
Since you do not appear to need to return anything when you initiate the "Notice" dialog, you can simply use a Subroutine like this.
Call notify "NOTICE!", "All information must be filled in " + chr$(13) + "before calculations can begin!" BadInfo = 1 Wait
Sub notify banner$, info$ Notice banner$ + chr$(13) + info$ End Sub
tenochtitlanuk beat me to the punch...
{:0)
Brandon Parker
|
|
|
Post by klewlis on Nov 21, 2021 17:45:17 GMT -5
The function is actually after the "end" statement for the program. I just posted the offending code so I didn't have to post the entire program.
|
|
|
Post by klewlis on Nov 21, 2021 18:12:49 GMT -5
I will probably use a sub for this, but am currently trying to learn more about how functions need to be called.
|
|
|
Post by Brandon Parker on Nov 21, 2021 21:35:06 GMT -5
Have you tried the "Functions and Subroutines" section in the LB Helpfile? It's under: Language Syntax and Usage>Logic and Structure ...
{:0)
Brandon Parker
|
|
|
Post by klewlis on Dec 31, 2021 19:30:59 GMT -5
Actually, if you will note my second post. I do spend a lot of time in the help file. There are days tho when I have to actually ask the question before the answer occurs to me. I knew it all along, but it just didn't register.
|
|
|
Post by Brandon Parker on Dec 31, 2021 20:58:19 GMT -5
So, perhaps I have to call the function like this? blah = Notify() then my function has to say Notify = blah Your second post does not mention that... Either way, it was just a suggestion of where to look in case you had not already noticed the section. {:0) Brandon Parker
|
|