News:

Function Finder  Find all the functions within source code files

Main Menu

Stuck on the Show_Text Function example

Started by SpellSword, July 03, 2007, 09:41:33 PM

Previous topic - Next topic

SpellSword

I'm working my way through the PB Help file tutorials and I've run into a snag.
When I run the following code it fails and produces an error message:

Show_Text(10,10,"Hello")
  Show_Text(20,20,"World")
  Sync
  WaitKey
 
Function Show_Text(x, y, mytext$)
  Text x,y,mytext$
EndFunction


The error information is:

Quote from: Message Box ErrorError in <Main.pba>, 7: Syntax Error - Unknown Character

Quote from: Compiling LogPass #1 [Prototyping Declarations]
Pass #2 [Generating Code]
Compile Time Error:
Line Number:7
Show_Text(10,10,"Hello")
Error Number:14
Error String:Syntax Error - Unknown Character

Line 7 of the code is:
Show_Text(10,10,"Hello")


I'm reading the Help File>About>Functions&Psub section, the last example before: 'Passing Arrays' section.
Here's the section of the Help file I'm reading:
Passing Variables



Now that alone would have very limited uses but wouldn't it be good if we could
pass information into our function so that it would display any text we wanted? Well
we can. Remember those brackets we mentioned earlier? Well that's where we put
any variables we want to pass to our function. So let's do a quick modification to
our existing function:


CODE:

 
  Show_Text("Hello")
  WaitKey
 
Function Show_Text(mytext$)
  Text 10,10,mytext$
  Sync
EndFunction
 





Now in our function call we've added a string of text and in our function header we
have added a variable name between the brackets. This variable will be used to
store the string passed to the function by the function call. So taking our first
function call as an example this in effect this means:


FUNCTION Show_Text(mytext$="Hello") do not type this!


So as an exercise let us expand on that a little further. We'll change it so that we
can also specify where we want the text to appear on screen by also passing x and
y co-ordinates into the function:


CODE:
 
  Show_Text(10,10,"Hello")
  Show_Text(20,20,"World")
  Sync
  WaitKey
 
Function Show_Text(x, y, mytext$)
  Text x,y,mytext$
EndFunction
 





Also note that the values we pass into a function can themselves be variables. eg:



x=10
y=20
showme$="Hello"
Show_Text(x,y,showme$)


So far all this really does is to recreate the TEXT command so try adding extra
variables to control things like text height and colour. You'll soon see just how
flexible functions can be.



The following two sections ("Passing Arrays" & "Passing Types") cover some of the
slightly more advanced features within PlayBasic so feel free to skip over them to
the "Returning Values" section and returning when you feel you've grasped the basics



This code did run without a problem:
  Show_Text()
  WaitKey
 
Function Show_Text()
  Text 10,10,"Hello"
  Sync
EndFunction

The only significant change I can see between the two is that Show_Text() is trying to pass variables to the function.

Quote from: PlayBasic Version Info
Compiler V1.63
IDE 1.1.5f
Help V1.63


I can't find what I'm missing here.

As I understand it 'Show_Text(10,10,"Hello")' Should pass it's data to 'Function Show_Text(x, y, mytext$)'

Why is there an error?
When I dream,
I carry a sword in one hand,
a gun in the other...

kevin

#1
 The issue is with the quote character.  Strings are notated between talking marks like this  "This is a string",  not like this "This is not a String".    PB won't have a clue what the second one is.  


PlayBASIC Code: [Select]
  Show_Text(10,10,"Hello")
Show_Text(20,20,"World")
Sync
WaitKey

Function Show_Text(x, y, mytext$)
Text x,y,mytext$
EndFunction





SpellSword

Thanks! That fixed it. The Show_Text function now runs in all it's glory!

I wasn't aware of the "" talk marks existence.  :-[

The code in the help file uses the "" in the Show_Text example for it's ".
I copied/pasted it into the Editor from the Help file. Error Missing Closing Square Bracket
When I dream,
I carry a sword in one hand,
a gun in the other...

kevin

Ahh .. Obviously It shouldn't do that,  i'll have look at it in a minute.

kevin