Subroutine call will not work

I am trying to understand how to use subroutines. This simple example prints "0" suggesting that either the subroutine multiply is not called or it is not returning the value of z Any suggestions?

int x = 1; int y = 2; void setup(){ Serial.begin (9600); } void loop(){ int z; multiply (x,y); Serial.println (z); x = x+1; y = y+1; }

int multiply (int x, int y){ int z = (x*y); return (z); }

Try: z = multiply (x,y);

You declare z as local to loop ()then another (different) variable called z local to multiply() The Serial.print prints the one local to loop() that has never been updated by the multiply FUNCTION (Note, not subroutine).

Remove the declaration of z from loop() then try Serial.println(multiply(x, y));

two things

  1. the are no

subroutines

in C/C++ there are only functions. Google does not understand if you ask about subroutines that you mean functions so all then results you get back will be from idiots (if they apply to C/C++ or about something else).

2 Read the sticky at the top of the forum. Don't just ignore it (I know you know better) -- this is far from the only place to have rules and to publish them.

Mark

Thanks. Declaring z initally also works int x = 1; int y = 2; int z; void setup(){ Serial.begin (9600); } void loop(){

multiply (x,y); Serial.println (z); x = x+1; y = y+1; }

int multiply (int x, int y){ z = (x*y); return (z); }

Declaring z initally also works

Nobody said it wouldn't. Declaring it like that makes it a global variable which is available throughout the program. You can even ditch thereturn (z);from the function and it will still work.

I am disappointed that you did not follow the advice given on posting code.

int z = multiply (x,y);

You've declared a function that returns an "int", so why not use the "int" that it returns?

Why not use code tags?

return (z);

"return" is not a function - there's no need for parentheses.

in C/C++ there are only functions.

Well, there are methods, too.