Coding error in subroutine

can anyone tell me what is wrong with this coding? getting error message on 'myfun' subroutine line. does not compile.
"expected constructor, destructor or type conversion before ';' token."

int a;
int x;
void setup() {
Serial.begin(115200);
}
void loop() {
a=5;
myfun();             // subroutine call don't have to declare
Serial.println(x);

}
myfun(){
x=(2*a);
return(x);
}

You have made the same mistake as in your previous topic

Your myfun() function has no return type

1 Like

That error message is very unhelpful to beginners.

As mentioned, you must always give any function a return type. If you don't want to return anything, you can put "void" as the return type.

But in your code, you are returning something, which is the value of x, after it has been updated, so the return type should be int.

However, there is no need for your function to return anything, because

  1. it updates x, which is a global variable
  2. when the function is called, it doesn't do anything with the return value, so the return value is discarded.

Here's one way to fix:

int a;
int x;
void setup() {
  Serial.begin(115200);
}

void myfun(){
  x=(2*a);
}

void loop() {
  a=5;
  myfun();
  Serial.println(x);
}

Notice I moved the declaration of myfun() to before it is called. The Arduino IDE does not give an error if you do not do this, but most other languages and IDE will give an error, so it's a good habit to get into.

A better version:

int a;
int x;
void setup() {
  Serial.begin(115200);
}

int myfun(){
  return (2*a);
}

void loop() {
  a=5;
  x = myfun();
  Serial.println(x);
}

This is better because now, x could be a local variable inside loop(). It's better to have fewer global variables because in more complex code, using them can lead to messy, confusing code.

Even better:

int a;
int x;
void setup() {
  Serial.begin(115200);
}

int myfun(int z){
  return (2*z);
}

void loop() {
  a=5;
  x = myfun(a);
  Serial.println(x);
}

Now, a can also be a local variable inside loop(). It's value is passed as a parameter to myfun() so myfun() can be given any variable or value as it's parameter and isn't fixed to using only a.

You can now also shorten the code:

void setup() {
  Serial.begin(115200);
}

int myfun(int z){
  return (2*z);
}

void loop() {
  Serial.println(myfun(5));
}
I have now specified return type. sketch still has errors. expected constructor etc before ';".
in my fun line.
   here is my new code still with errors.
int a;
int x;
void setup() {
Serial.begin(115200);
}
void loop() {
a=5;
myfun();             // subroutine call don't have to declare
Serial.println(x);
delay(2000);         // 2 second delay between print outs
                    // loops back to a=5 then subroutine can now have a=6 etc futher down in void loop

}
myfun(){
x=(2*a);
int return(x);       //type of return now specified could also be float etc if required
}                    // also seen example code where type of return not specified?

Please show us on which line you added the return type?

PS
Please keep text separated from code.

obviously on the line that says 'return'.

Not obvious. See first line in below.

int myfun()
{
  ...
  ...
  return x;
}

Not when you do it. How many times in how many threads have you been corrected, but you never listened? "All of them."

These functions should have return type int...

Whoops!

Corrected.

Thanks @build_1971

I may have no choice but to try a goto Bob. I added a type to the 'return' as suggested and the compiling is still coming up with errors. Instead of keeping coming up with suggestions to fix the code couldn't you provide me with a code to achieve what I want in the code?
I may also have no choice to try to recover my coding on the Nano board for what is a very long and complicated sketch. The sketch gives me the azimuth and elevation of the sun. Unfortunately I did not make a copy of the sketch. The sketch included calls to 2 different subroutines. But now when trying to do the same thing it is not working and the advice I am getting is not fixing the code.

Yes they show me where I am wrong. I fix the code in the way they suggest and the code still doesn't compile. None of the suggestions include a correct code which will compile. This is what I am waiting for. Please just give me the correct code to use a subroutine that will return a value to the main program. Isn't that simple enough.
Don't use some really complicated example that involves LEDS and sensors etc. Keep it simple.

Every time you change your code, you should post the complete new version and the complete log of error messages you get.

Perhaps if you provide complete information, folks here will help you come up with the correct code yourself. There is a forum section where people will write complete code for you ... you'll need to pay them for their services.

In all your forum posts (including your ongoing other one) ... you still haven't learned that you're referring to a function, not a "subroutine"?

1 Like

where were you given this advice?

the return type is specified before the name of the subroutine

int func () {
    return 3;
}

There is no need to use a goto as there is always a better way. Don't do it

As to not being provided with code that works to fix your problems, that simply is not the case. You were given examples in your other topic on calling functions but seem to have chosen to ignore them

It was also pointed out that your code would be stuck in a while loop forever because a variable was not being updated, but seem to have ignored that

It is very frustrating trying to help you if you take no notice of advice and examples provided and now, by starting this topic, you seem to want to go in a different direction

I assume that you still have problems with code that will not compile and/or does not do what you want. If so then I suggest that you continue in your original topic. Post your latest code, full details of any errors and/or what is not working and we will continue to help you but you must listen to the advice being given

I understand your fustration.

Maybe this will help
In C, subroutines are called functions.
There are four types of user-defined functions:
1. Function with no arguments and no return value
2. Function with no arguments and a return value
3. Function with arguments and no return value
4. Function with arguments and with return value

Here is an example of a function of type number 2
(function with no arguments and a return value)

int y;

void setup()
{
  Serial.begin(9600);
}

void loop()
{
  int z;
  
  y = 2;
  z = bob();
  // Print the value of z
  Serial.println(z);

  delay (1000);
}

// This is function 'bob'

int bob() // The function is an integer type
{
  int x; // x is an integer.
  // x = y multiplied by y
  x = y * y; // y is a global variable
  return (x); // return the value of x
}

I always take notice of any advice given on this forum. I received a suggestion as to why my code was not compiling. It was that I did not put a return type in. So I then put in 'int return'. This also didn't compile. It is apparent that you do not test this code out yourself or you would know this.
I also explained the second point you bought up. The variable was being updated all the time from the rest of my code which was not shown. It was not stuck in the while loop forever.
Please suggest a code that will work for a subroutine call. Then test it yourself to make sure it compiles. Make it simple. Just call a subroutine to return a specific value.
Something like this;
sub1;
y=(x*2);
sub1{
x=5;
return (x)}
Do the correct coding for this. You always point out my mistakes but never suggest a sketch that will compile.
The compiling error is always the same. "expected constructor etc before ';' token.

That's because it's nonsense. You don't get to make up your own syntax. Nowhere did anyone tell you to do this:

You were provided the correct syntax in Post #3, Post #7, and Post #9.

2 Likes

Thanks Jim. at last we have a sketch that compiles. The key thing here is that the type of function must be given. the previous error was giving a type of return. this did not compile.
I have changed the coding to be more in line with Arduino practice. I have declared x,y,z as integers at the beginning rather than later in the program. I believe my revised code is easier to understand. "z=bob()" is confusing. Here is my revised code;

int y;
int x;
int z;

void setup()
{
  Serial.begin(115200);
}

void loop()
{
  
  
  y = 2;
   bob();
   z=(y*x);
  // Print the value of z
  Serial.println(z);

  delay (1000);
}

// This is function 'bob'

int bob() // The function is an integer type
{
  
  x = y * y; // y is a global variable
  return (x); // return the value of x
}

You wrote before that you had completed Arduino projects.
It's really clear that you never completed learning basic C.

int function_X ()
{
  int x;
....
  x = some value 
....
  return x; // this is how returning an int works but learn the rest!
}

Can you remember to post code in code tags?

You need to learn the language before you evaluate it.
You need to spend more time on examples and study.

But DO learn Basic! Learn how much it sucks!