PUB declaration does not work
EMHmark7
Posts: 93
Hi,
I am trying my first sub program in Spin and I am not able to declare a PUB above the PUB Main.
My most basic attempt is:
(it sets LCD at 'a' position, wipes a 'c' wide blank and displays the 'b' decimal value)
PUB abc (a,b,c)
LCD.tx(a)
repeat c
LCD.str(string(" "))
LCD.tx(a)
LCD.dec(b)
Even by putting the first line only, the Main program does not run.
If I comment these lines it works.
I have these sections: CON DAT OBJ VAR before it
and PUB Main is following it.
1) Any idea about what could be wrong?
Also, documentation is not precise enough by saying:
Param
must be globally unique, but other methods may also use the same symbol name.
2) Can I reuse the same var name within thePUB declaration (params or local variables)
without conflicting with other params or local variables withing other PUB declarations?
(in other words, are these params and local vars in the PUB are local to that function,
and so, if I do not declare a var in such way (param or local var) it will 'see' the var of that name declared outside)
3) Anybody can clarify the documented statement?
Thanks,
Marc
I am trying my first sub program in Spin and I am not able to declare a PUB above the PUB Main.
My most basic attempt is:
(it sets LCD at 'a' position, wipes a 'c' wide blank and displays the 'b' decimal value)
PUB abc (a,b,c)
LCD.tx(a)
repeat c
LCD.str(string(" "))
LCD.tx(a)
LCD.dec(b)
Even by putting the first line only, the Main program does not run.
If I comment these lines it works.
I have these sections: CON DAT OBJ VAR before it
and PUB Main is following it.
1) Any idea about what could be wrong?
Also, documentation is not precise enough by saying:
Param
must be globally unique, but other methods may also use the same symbol name.
2) Can I reuse the same var name within thePUB declaration (params or local variables)
without conflicting with other params or local variables withing other PUB declarations?
(in other words, are these params and local vars in the PUB are local to that function,
and so, if I do not declare a var in such way (param or local var) it will 'see' the var of that name declared outside)
3) Anybody can clarify the documented statement?
Thanks,
Marc
Comments
Are your parameters globally unique?
From the Propeller Manual, page 182, version 1.2:
...Param is a parameter name (optional). Methods can contain zero or more comma-delimited parameters, enclosed in parentheses. Param must be globally unique, but other methods may also use the same symbol name. Each parameter is essentially a long variable and can be treated as such....
So your "Main" is not main at all if you put something above it.
you should attach your complete code to a posting. Easiest way to do this is to use the
File - archive - Project .... function in the propeller-tool.
This creates a ZIP-file with timestamp that contains all files that are nescessary to compile your code.
best regards
Stefan
PARAM must be globally unique means that you can't have a VAR named myVar and then a parameter named myVar as well, because then there would be no way for the function to access the VAR variable.
2) It is NO problem to have several functions with the same parameter name because these are local anyways.
3) ?
Putting my sub programs below Main makes Main work.
I believed the compiler needed to compile a function before it can compile those using it. (As in Pascal, C, etc.)
About variable scope:
So you mean guys that I can define a parameter or local variable with a name that is already used in higher levels, but these highers level variables will not be accessible while the function is running, even by another subfunction because the local variable is hiding the more global one even for other codes that are running while the actual function is runing?
I am still not shure I catch. If I program a function, I should not need to care about variables named outside of it, unless I want to access an outside variable from inside the function.
So, I am not shure I catch why documentation says "Param must be globally unique, but other methods may also use the same symbol name.
I understand it from point-of-view of the calling statement. But from the PUB definition (the serving side), I am not shure I catch what need to we catched in regards of "variable scope" (please let me know if you do not know this programmer's term).
Clarification is welcome on this.
Thanks
The variable scope of a VAR variable is global inside of the file it has been declared in.
The variable scope of a PARAMETER and of a LOCAL variable is local, so only visible for the function.
Of course, when coming from other programming languages you might have the opinion that this should work. But the designers of the SPIN compiler decided to give an error message in this case, as SPIN does not give you a way of deciding which variable to use then inside of the function (as for example in C++ where you can access a object-variable with this.xxx ). Shall the local variable be used or shall the global variable be used?
This is simply a fact you have to learn and handle by simply adding a prefix to the global variable or to the local variable - whatever you prefere.
So in an application that processes let's say a motor direction with a var named Direction, and passing it to functions that also carry Direction,
EVERY function may get a parameter or local variable named lnDirection (localnumeric) while a global variable may be named gnDirection (globalnumeric).
Please let me know if I am wrong.
I'll set as solved after a while.
Thanks for your help.
1. the function call has to copy the parameter to the stack
2. the function itself has to do runtime calculations to access it on the stack
You should be aware that we talk about microcontroller programming which means that we have very limited resources. So, in oposite to PC programming where you have GB to waste, you'll run into problems very fast if you keep your C, C++, Java .... habits of programming ;o)
So what we have here are properties of objects and the same name can be used for different properties in different objects.
As such it often makes no sense to pass them as parameters into methods within the same object. Might as well just declare them in VAR or DAT and refer to them directly in the methods.
Of course if you have a function (perhaps PRI) that is used more than once in your object then perhaps you will need to use parameters and/or local variables.
MagicIO2,
Who said C incourages the waste of gigabytes? The C language was created and designed to be used on some pretty small machines.
Ok, this statement was misleading from my side! Actually it's not a programming language which leads to a waste of resources. It's the fact that you have a lot of resources on desktop computers these days and the programmers which come from desktop programming (most likely using something like C, C++ or Java and of course many other languages) simply don't have the need to think about how wasteful the programs are these days. Did you have a look at a java stack-trace recently? Then you know what I mean ;o)
Guilty as charged. Around here we are writing JavaScript on the server, not to mention PHP and Python.
Just now I'm seriously considering using JavaScript and node.js on an embedded ARM project.
What? Interpreted, slow, huge, memory eating JS on an embedded system. Shocking waste.
However even there I see I have RAM and horsepower enough to waste. I'd rather knock this thing out quickly in JS than spend weeks carefully crafting it in C. And guess what? The JavaScript/node.js experiments I have been trying have been performing on a par with C++ versions of the same thing and somewhat faster than the Go language which even compiles to native code.