Passing variable to function failing

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am getting weird results when i try:-

void MyFunction(int a, int b, int c){ }
int y;
void RunTest(){ for(y = 0; y < 8; y++){ MyFunction(0,y,1);  }

 

instead of getting expected results 0-7, i get 20-24,30-32

i cannot figure out why!

When I pass a set value being the 0 and 1, no problem, but any other variable doesn't work!

thanks

Last Edited: Sat. May 6, 2017 - 03:53 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello,

 

What do you mean by "results"? Are you looking at the value of y? If so, are you looking in the debugger or the simulator?  In either case, how are you observing?

 

Oh, yes, the important question: what tool chain? avr-gcc? or something else?

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sat. May 6, 2017 - 04:35 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Theone6000 wrote:
expected results 0-7,

How do you expect this to be the "result"?

 

Your function does not return any value - so how can it have a "result" ?

 

i get 20-24,30-32

How & where, exactly, to you "get" that?

 

i cannot figure out why!

You haven't shown the code of your function - so how do you expect anyone else to be able to figure anything??

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I pass the three variables to a serial out file, the first and third print as expected a 0 and a 1, but as i said in my first post, instead of the loop producing 0,1,2,3,4,5,6,7, I get 20,21,22,23,24,30,31,32, I am using atmel studio 7, for the atmega 328. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

There is still a lot you are not telling us. How does 3, for example, get converted to something printable. Why not just show us all of MyFunction?

 

Also really important - how are you viewing the serial output? Did you know that, for example, 32 is the hex equivalent of the character '2'? You may just not be viewing the characters with the appropriate terminal settings!

 

Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA http://www.orelectronics.net

Last Edited: Sun. May 7, 2017 - 05:35 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I found out that I did not have the right base for Itoa(thought the 2nd int was string length when actually a radix(being the base of the number(10 for standard decimal, 2 for binary))), and have narrowed the issue to a switch statement, which the critical code is as follows:-

uint8_t TestInt = 0;

switch(TestInt){

    case 0:{ break; }

    case 1:{ break: }

    default:{ break; }

    }(and no, i do not thing "flush" close brace is neat (don't get me started!))

 

 

 

Edit:-

Fixed it, added breaks to the code, why is it a switch case has to have a break, other program languages do not, with exception of D(I think), is to make the switch system more "dynamic"?

thanks

 

Last Edited: Sun. May 7, 2017 - 05:37 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Why? Because that's the way it is specified! Sometimes it works for you, sometimes against.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Theone6000 wrote:
a switch case has to have a break, other program languages do not

Really? The only languages I can think of right away that has no break are Perl and Pascal.

 

Languages that have break include: C (as you've just discovered), C++, Java, C# , Python..

 

What other programming languages where you thinking of?

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Sun. May 7, 2017 - 06:19 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Theone6000 wrote:
Fixed it, added breaks to the code, why is it a switch case has to have a break, other program languages do not, with exception of D(I think), is to make the switch system more "dynamic"?

Did you consider that if you don't have the "break", then you fall through to the next code -- as you have found.

 

That option can be considered a feature, unavailable in your [unnamed] other languages.

 

Examine, for example, "Duff's Device"... https://en.wikipedia.org/wiki/Du...

 

 

 

 

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.