Go Down

Topic: A few questions on C++ (from my C++ textbook) (Read 1 time) previous topic - next topic

thomas3120


Code: [Select]
void foo (int a, int b)
  {
  // you can do things with a and b here

  return;   // you cannot return a value
  }  // end of foo

int bar (int a, int b)
  {
  return a + b;  // you must return an int
  }  // end of bar



To call those:

Code: [Select]
int main ()
  {
  foo (1, 2);  // call foo, there is no return value

  bar (4, 5);  // call bar, discard the return value

  int fubar = bar (6, 7);  // call bar, get its return value

  } // end of main




Thanks again for the replies.
Fubar... hehe  sounds like most of the code I write  :)

So here:
Code: [Select]
int main ()
  {
  foo (1, 2);  // call foo, there is no return value

  bar (4, 5);  // call bar, discard the return value

  int fubar = bar (6, 7);  // call bar, get its return value

  } // end of main


In the function "void foo(int a, int b)", you're assigning the number 1 and 2 to "int a" and "int b" respectively.
In the function "int bar(int a, int b)", you're assigning the number 4 and 5 to "int a" and "int b" respectively, resulting in a value of int a + int b (4 + 5) which equals 9, but discarding the return value.
Then using the variable "int fubar", int fubar = bar(6, 7) to get a return of "13" ?

t

Nick Gammon


Nick Gammon


Just to make sure...
Quote
#include <iostream>

using std::cout;
using std::endl;

the stripped down C++ used by Arduino doesn't have "iostream" or "std", right?



To be precise, the C++ isn't stripped down particularly. However if you compile the above you get:

Code: [Select]

sketch_dec21a.cpp:1:20: warning: iostream: No such file or directory


You can use the Streaming library from here to implement something similar:

http://arduiniana.org/libraries/streaming/

It doesn't particularly make sense for the Arduino IDE to support stdin and stdout (or stderr) as they don't have any real meaning in the environment.

However if you like to "stream" output to, say, Serial, then you can use the Streaming library to get similar effects.

PeterH


It doesn't particularly make sense for the Arduino IDE to support stdin and stdout (or stderr) as they don't have any real meaning in the environment.


Just as a curiosity, I saw a bit of code from maniacbug in an example sketch that configured stdout to use Serial. I suppose it would have been possible to hook up the stdin and stderr file descriptors in the same way, but I've never tried it. I also haven't tested to see whether the standard i/o streams followed the same path. Anyway, I thought it was a neat technique.

Code: [Select]

/*
Copyright (C) 2011 James Coliz, Jr. <maniacbug@ymail.com>

This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
version 2 as published by the Free Software Foundation.
*/

/**
* @file printf.h
*
* Setup necessary to direct stdout to the Arduino Serial library, which
* enables 'printf'
*/

#ifndef __PRINTF_H__
#define __PRINTF_H__

int serial_putc( char c, FILE * )
{
  Serial.write( c );

  return c;
}

void printf_begin(void)
{
  fdevopen( &serial_putc, 0 );
}

#endif // __PRINTF_H__
I only provide help via the forum - please do not contact me for private consultancy.

WizenedEE



It doesn't particularly make sense for the Arduino IDE to support stdin and stdout (or stderr) as they don't have any real meaning in the environment.

Just as a curiosity, I saw a bit of code from maniacbug in an example sketch that configured stdout to use Serial. I suppose it would have been possible to hook up the stdin and stderr file descriptors in the same way, but I've never tried it. I also haven't tested to see whether the standard i/o streams followed the same path. Anyway, I thought it was a neat technique.

http://arduino.cc/forum/index.php/topic,120440.0.html
http://www.nongnu.org/avr-libc/user-manual/group__avr__stdio.html

PeterH

I only provide help via the forum - please do not contact me for private consultancy.

Go Up