3

After this question i starteted going deep with the theory, and started studyng step-by-step with the "Beginning C, for Arduino " Jack Purdum.

I finished to study the functions chapter, but i still have some doubts on the code, i hope this will be not a dumb question.


Why there is a Method "Print" not a Data Type inside the Function Argument List?


static void printImuData(Print &printer, int16_t Time0,
        int16_t AcX, int16_t AcY, int16_t AcZ,
        int16_t GyX, int16_t GyY, int16_t GyZ)
{
    printer.print(Time0); printer.print(",");
    printer.print(AcX);   printer.print(",");
    [...]
}

[...]

printImuData(dataFile, Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);
if (Serial_plus_SD)
    printImuData(Serial, Time0, AcX, AcY, AcZ, GyX, GyY, GyZ);

I though the answer was "because you can pass data to a function by-value or by-reference" in fact the first input in the argument list is the left value (memory address) of a Method, but i am very confused.

Function arguments are used to pass data to the function that it may need to perform its task.

I tried to answer to the following questions to understand the code.

1. What task does this function perform?

The function will print some data on a Serial Monitor or SD Card

2. What data do i need to send to the function?

I need to send one Method through the left value of a pointer and 7 intergers. I can't understand why a method.

3. What data do I get back from it?

Nothing because is a void, it will perform some task inside the function (print inside the object thas is pointed?) but nothing will return to the caller.

In the book basic example is a function that returns the value 0 or 1 if is a Leap Year.

The function argument is an int and returns an int.

void loop()
{
    if (Serial.available() > 0) {
        int bufferCount;
        int year;
        char myData[MAXCHARS + 1]; // Save room for null
        bufferCount = ReadLine(myData);
        year = atoi(myData); // Convert to int
        Serial.print("Year: ");
        Serial.print(year);
        Serial.print(" is ");
        if (IsLeapYear(year) == 0) {
            Serial.print("not ");
        }
        Serial.println("a leap year");
}
}
/*****
Purpose: Determine if a given year is a leap year
Parameters:
int yr The year to test
Return value:
int 1 if the year is a leap year, 0 otherwise
*****/
    int IsLeapYear(int yr)
    {
    if (yr % 4 == 0 && yr % 100 != 0 || yr % 400 == 0) {
        return 1; // It is a leap year
    } else {
        return 0; // not a leap year
    }
}
/*****
Purpose: Read data from serial port until a newline character is read ('\n')
Parameters:
char str[] character array that will be treated as a null-terminated string
Return value:
int the number of characters read for the string
CAUTION: This method will sit here forever if no input is read from the serial port
and no newline character is entered.
****/
int ReadLine(char str[])
    {
        char c;
        int index = 0;
        while (true) {
        if (Serial.available() > 0) {
        index = Serial.readBytesUntil('\n', str, MAXCHARS);
        str[index] = '\0'; // null termination character
        break;
        }
    }
    return index;
}

Like in my the last question, I have a civil engineer background and i am a self-taught, so is my first time with coding :)

Thank you for your patience.

  • 1
    I need to send one Method through the left value of a pointer and 7 intergers. The first arguments is actually a pointer to an object. – Gerben May 25 '17 at 9:48
  • The example seems to be more about inheritance (polymorphism). You can pass any object of a class derived from the Print class to this function. HardwareSerial (the class of the Serial object) derives from the Print class. (Actually it derives from Stream which derives from Print). – Johnny Mopp May 25 '17 at 17:09
2

"There are no stupid questions, just stupid answers"

Why there is a Method "Print" not a Data Type inside the Function Argument List?

There is no "Method" Print in the argument list. There is a reference (not a pointer which would be a * not a &) to an object called printer that is of type Print. The code you have provided doesn't define Print but its a could guess that it is probably something to do with printing. By supplying the printer argument you can print this data to anything that inherits from the Print base class, Serial is a good possibility, but you could write a class that derived from Print and wrote the data to an LED matrix, etc. I can tell its not a Method, because you are calling the print method of printer, so it has to be either a class or structure.

The function will print some data on a Serial Monitor or SD Card

I can see why you thought that but you are being too specific, it prints to anything that derives from Print, Serial and dataFile derive from it, but they might not be the only things.

I need to send one Method through the left value of a pointer and 7 integers. I can't understand why a method.

An example might help:

class CGPrinter : public Print
{
  // Some code....
  virtual void print (const char* input) {// do something}
}

void loop()
{
  CGPrinter cgp;
  printImuData (Serial, 1,2,3,4,5,6,7);
  printImuData (cgp, 1,2,3,4,5,6,7);
}

Serial is an object, a variable, a specific instance of type of class that can print data.

CGPrinter is my new super sexy printing class that will read the characters that you 'print'. To print the 7 integers to CG's printer class is exactly the same call as the one to write it to the Serial Monitor, because both classes derive from Print.

  1. What data do I get back from it?

Yes, correct on both points.

Does it make more sense now? If not, just say and I'll try and clarify it.

  • In the argument list ex int16_t Time0 i have 1st the data type and then the rval of my variable. In the case Print &printer i have something in the 1st place (an object that belong to the Print class) and a reference in the 2nd(something that inform the compiler about the lval of my variable referenced), but is not a Method. A Friend of mine from CS told me that Print was a method, so i belive he was wrong, this is one reason of my confusion. – Andrea Ciufo May 26 '17 at 18:12
  • But is still not clear for me :( Inside the function you can put both a class and an object (istance of a class) that belong to that class, why? Why not only one of the two? – Andrea Ciufo May 26 '17 at 18:13
  • I'm not clear what you mean. If you mean in the case of printImuData() you can't pass a class into the function. A class is a type name, so try doing what you are saying with a char or an int. If you have the function void DoStuff (int& a) can you call DoStuff (n); if int n = 4;? Yes or course you can, but can you pass in int does DoStuff(int); make sense? Its the same with classes if you have void OtherFunc(CHole& a); and then call CHole n; OtherFunc(n); calling OtherFunc(CHole) wouldn't make sense. – Code Gorilla May 30 '17 at 7:06
  • The & turns the function parameter means the variable is "passed by reference". This means it is not copied, the address is passed to the function and it works on that memory address rather than the actual value. This means the parameter becomes an IN/OUT value, any modifications made inside the function are passed back to the caller. For example: void add (int a) {++a;} int a = 0; a = add(a); // a = 0 against void add (int a) {++a;} int a = 0; a = add(a); // a = 1 Note that (const int& a) in not modifiable. – Code Gorilla May 30 '17 at 7:10
  • Thanks for your patience. I made an error studying your answer, mea culpa, my question about the class inside the function was nosense :( In the last comment you wrote this void add (int a) {++a;} int a = 0; a = add(a); // a = 0 against void add (int a) {++a;} int a = 0; a = add(a); // a = 1 but is the same. Maybe, if i understood , you will write this void add (int a) {++a;} int a = 0; a = add(a); // a = 0 against void add (int& a) {++a;} int a = 0; a = add(a); // a = 1 ? Thanks a lot @Code Gorilla it is very useful for me and I'm very grateful to you :) – Andrea Ciufo May 30 '17 at 16:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.