Action! › and› BBS Express! PRO› Tutorial›› by› Thomas M. Johnson ››› Available from ›› Villa Video's Bargain Cellar › (414) 265-5149 › ExpressNet Node X11 ››Action! is copyright of ACS, OSS, ICD.›BBS Express! PRO is copyright Orion›Micro Systems.› ›This tutorial is copyright Thomas M.›Johnson.››This tutorial can be distributed under›the following conditions:› 1) It is free.› 2) All of the above› information is intact.››--------------------------------------››Do you remember what a function is›from high school algebra? It take›an number, performs some operation on›it and return only 1 number back.›Like:››y = f(x)››where f(x) = x^2››So if x=3 then y=9.››In Action!, a function is like a PROC›only it has a type associated with it.›We can write the x^2 function in›Action! like this.›››CARD FUNC squared(BYTE x)›› CARD y›› y = x * x››RETURN(y)››PROC main()›› CARD answer›› answer = squared(3)› PrintCE(answer)››RETURN››And it will output a 9.››Notice that there is a variable type›before the word FUNC. This tells›what is going to be returned. A BYTE,›CARD or INT can be used. As you can›see, the rest is alot like a PROC.›The next different thing is the›RETURN statement.››Whatever is on the left side of the›equals sign in the call, in this›case the variable 'answer' will›get the same value as what is inside›the () after the word RETURN in the›FUNC.››You can see that the input routines›we've been using so far are really ›FUNCs. When you do a: ››value = InputB()››the first line of InputB in reality›looks like this.››BYTE FUNC InputB()››meaning it returns a BYTE to value.››When we first talked about IF FI, ›I said that the conditional after the›IF returned a TRUE or a FALSE. In›reality, the FALSE is the number 0.›And TRUE is actually any other number.›The most common values for TRUE are›1 or 255 but ANY positive value will›due.››So if you wrote a FUNC that returns›a 0 or a 1, you could use it in›place of a conditional. This is a›powerful use of FUNCs.›››BYTE FUNC values_match(BYTE x,y)›› BYTE rval›› rval=0› IF x=10 AND y=20 THEN› rval=1› FI›RETURN(rval)›››PROC main()›› BYTE v1,› v2›› PrintE("Please give me 2 numbers.")› Print("v1 = ")› v1=InputB()› Print("v2 = ")› v2=InputB()›› IF values_match(v1,v2) THEN› PrintE("Your values match!!")› FI››RETURN›››This is almost a trivial sample of ›the power this type of construct.›If you have seen a program I wrote›running on BBS Express! PRO called›"The Wheel", 50% of that program is›BYTE FUNCs that return a 1 or a 0 to›IF FI statements.››Now, what if you need a variable that›will be used by alot of PROCs and ›FUNCs? Do you keep sending it as›a parameter? Well, you could, but›using globals would be better.››We know that using a local variable›doesn't affect any of the other›variable used outside that PROC or›FUNC. What if we want to change›something in the outside program?›Again, global variables are the›answer.››A global variable is a variable that›is not declared between a PROC and›its RETURN or a FUNC and its RETURN.›They are declared at the start of the›program and between PROCs and FUNCs.››The compiler knows a variable is a ›global by use of the work MODULE.›The word MODULE is assumed as›the first line your program. But it›never hurts to put it in. If you›declare globals anywhere else in your›program (like between PROCs etc.) then›you MUST have the MODULE there.›››MODULE› ›› BYTE score›› › PROC you_win()›› PrintE("You won this game!!!")› score = score +100› RETURN›› PROC main()›› you_win()› PutE() › Print("Your score is: ")› PrintCE(score)›› RETURN›››You can see that the value of score›in the main() was increased by the›"score = score +100" in you_win().›You can't do this with locals›because when you change a local (this›includes parameters too) it is only›changed within that PROC or FUNC.››Try to resist the urge to use all›globals in your programs. Globals›make bugs nearly impossible to find.›And there are times when you don't›NEED a variable to change in the›outside program.››When I first introduced PROCs I said›that they were independent blocks›of code. Using globals makes a PROC›dependent on the rest of the program.››