[SOLVED] Variables and Functions Can't Have the Same Name


#1

Here is a screen shot of the code that I am working with.

I am working with mBlock v5.4.3 and am using the mBot device and I am running the IDE on a Windows 11 PC.

As you can see, I am in live mode and am connected to the mBot.

Also note that I have a variable and block both called SomeName. After clicking on the green flag and then clicking on the variable SomeName you can see that the value of the variable is 40, This indicates that the value of the variable was initially set to 20 but was then changed to 40 after the block was executed. This code works even if the power switch on the mBot is turned off So far so good.

However, if I disconnect the mBot then the code will not run at all. Can I assume that in order to do something like displaying the value of the variable the IDE must have access to the controller even if the controller is turned off?

Now comes the real reason that I’m posting this topic.

If I now switch the mode over to Upload I am working with this code.

When I press the Upload Code button this is what I get.
image

It did not take long to see the source of the problem. However, some other users may not detect the source of the problem so quickly. I wonder why there was not a problem in live mode but there is a problem in Upload mode. Additionally, I think that it would be nice if there was a more user friendly way of letting the user know that they can not have a variable and a block with the same name.

I had a great experience working with Best_codes on a previous issue. I hope that he or she also takes a look at this.

Thanks in advance.


#2

Hey @bjones, thanks for reporting this issue.
To understand why this occurs, we can look at the Arduino C code:

In Arduino C, the code will have issues compiling for two reasons:

  1. Function Name Conflict: The code defines a variable named SomeName and a function with the same name. This causes a conflict and will result in a compilation error. It is recommended to use different names for variables and functions to avoid such conflicts.

  2. Redefining Variable as Function: In the setup function, the code assigns a value of 0 to SomeName using the function name syntax (SomeName();). However, SomeName is initially declared as a float variable. This will cause a compilation error as the code tries to use SomeName as both a variable and a function.

These are both indicated in the error:
image

To resolve this issue, you need to create unique names for your variables and functions.

In live mode, mBlock runs code on the mBot live as you test it. The reason that your code works in Live Mode but not whenever you try to upload it is that in live mode, mBlock doesn’t compile codes, but instead runs them in a virtual machine and sends them directly to the programming board. If you want to test codes without the board being connected, then you can make test codes on a sprite and run them there (sprites are limited to JS, so functions, variable, and some other things, but not a devices sensors.)

Thanks for bringing this up! I will certainly be sure to check out any issues you have, and be sure to mention me as @Best_codes so that I get an email and can respond faster. :smiley:

I hope this answers your question!

Thanks for your time,
Best_codes


#3

@Best_codes.

It is nice to hear from you again and thanks for your prompt reply.

As mentioned in my post I have fixed the problem and I know why the problem happened. I was able to understand the compile time errors. However, other users might like a little simpler way of being notified about the issue.

I hope that I’m not getting to far away from the original thread topic. I have written some code that might be of use to others. Is there a way to make the code, and the supporting documentation, available to other users?

Thanks


#4

Yes, there is. You can share your code on somewhere like GitHub (github.com), or you can send the code to me and I can host it on my website for you. Can you please send me a preview of the code in a private message as well? Thank you!


I have actually made an extension for Arduinos that allows functions with the same name. I’ll see if I can adapt it to mBot Ranger soon.

Thanks for your time,
Best_codes
@bjones


#5

No need to make them work together. It was a silly mistake to begin with and it is much better off with two names.


#6

@bjones Alright then! If it’s alright with you then I will adjust this topic to solved.


#7

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.