Pages: 1 [2]   Go Down
Author Topic: A few questions on C++ (from my C++ textbook)  (Read 1509 times)
0 Members and 1 Guest are viewing this topic.
Charleston, SC
Offline Offline
Full Member
***
Karma: 1
Posts: 144
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Code:
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:
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  smiley

So here:
Code:
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
Logged


Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

That's correct.
Logged

Global Moderator
Offline Offline
Brattain Member
*****
Karma: 474
Posts: 18696
Lua rocks!
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
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.
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
/*
 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__
Logged

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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
Logged

UK
Offline Offline
Shannon Member
****
Karma: 222
Posts: 12534
-
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset


Bookmarked, thanks.
Logged

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

Pages: 1 [2]   Go Up
Jump to: