Compile errors gerated when I paste in code that runs in another sketch

This is my first post here. I'm new to the Arduino world. I'm using a Rev3 UNO board. The project goal is to read analog data, log and time stamp it. I copied example programs and verified the hardware OpenLog and the DS1307 BOB both from sparkfun. So now I'm trying to Frankenpaste tht software together into a single sketch. The current problem the "gotoCommandMode()" which is a library command to put OpenLog into a command mode so that a file can be created and opened to receive the data. I get a "was not created in this scope" error. My feeble understanding of what little I've read says that "gotoCommandMode()" does not need to be in the main loop since it performs no calculation. In the the snippet of code immediately below my errors is the code that needs to be inserted. But whether I insert it in the main loop or not ,I get compile errors. I'm pretty sure it's a program structure thing as opposed to syntax., but I'm getting nowhere on my own. I guess now I have time to read the manual.

My errors
Logging_and_wth_ds1307_oct27c.ino: In function 'void setup()':
Logging_and_wth_ds1307_oct27c:62: error: 'gotoCommandMode' was not declared in this scope
Logging_and_wth_ds1307_oct27c.ino: In function 'void loop()':
Logging_and_wth_ds1307_oct27c:88: error: 'createFile' was not declared in this scope
Logging_and_wth_ds1307_oct27c:143: error: expected `}' at end of input

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This is the code that functions in another sketch but not this one.

randomSeed(analogRead(A0)); //Use the analog pins for a good seed value
  int fileNumber = random(999); //Select a random file #, 0 to 999
  char fileName[12]; //Max file name length is "12345678.123" (12 characters)
  sprintf(fileName, "log%03d.txt", fileNumber);

  gotoCommandMode(); //Puts OpenLog in command mode
  createFile(fileName); //Creates a new file called log###.txt where ### is random

^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Serial.print("Random file created: ");
Serial.println(fileName);

#include <SoftwareSerial.h>
#include <Wire.h>

 #define DS1307_I2C_ADDRESS 0x68  // This is the I2C address
// Arduino version compatibility Pre-Compiler Directives
#if defined(ARDUINO) && ARDUINO >= 100   // Arduino v1.0 and newer
  #define I2C_WRITE Wire.write 
  #define I2C_READ Wire.read
#else                                   // Arduino Prior to v1.0 
  #define I2C_WRITE Wire.send 
  #define I2C_READ Wire.receive
#endif
// Global Variables
int command = 0;       // This is the command char, in ascii form, sent from the serial port     
int i;
long previousMillis = 0;        // will store last time Temp was updated
byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
byte test;
byte zero;
char  *Day[] = {"","Sun","Mon","Tue","Wed","Thu","Fri","Sat"};
char  *Mon[] = {"","Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"};
//*******************************Logger Vars*******************************************
int CH0 = A0;
int CH1 = A1;
int CH2 = A2;
int CH3 = A3;
float dat0 = 0;
float dat1 = 0;
float dat2 = 0;
float dat3 = 0;
int rxPin = 3;
int txPin = 2;
int x=0;
int resetOpenLog = 4; 
int statLED = 13;
SoftwareSerial OpenLog(rxPin, txPin); //Soft RX on 3, Soft TX out on 2
// Convert normal decimal numbers to binary coded decimal
 
byte decToBcd(byte val)
{
  return ( (val/10*16) + (val%10) );
}
 
// Convert binary coded decimal to normal decimal numbers
byte bcdToDec(byte val)
{
  return ( (val/16*10) + (val%16) );
}


void setup() {
  Wire.begin();
  //Serial.begin(57600); 
  zero=0x00;
  pinMode(statLED, OUTPUT);
  SoftwareSerial OpenLog(rxPin, txPin); //Soft RX on 3, Soft TX out on 2
  Serial.begin(9600);
  Serial.println("OpenLog online");
   gotoCommandMode(); //Puts OpenLog in command mode :  [THIS IS THE PROBLEM]
}

void loop(){
 // Reset the register pointer
  Wire.beginTransmission(DS1307_I2C_ADDRESS);
  I2C_WRITE(zero);
  Wire.endTransmission();
 
  Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
 
  // A few of these need masks because certain bits are control bits
  second     = bcdToDec(I2C_READ() & 0x7f);
  minute     = bcdToDec(I2C_READ());
  hour       = bcdToDec(I2C_READ() & 0x3f);  // Need to change this if 12 hour am/pm
  dayOfWeek  = bcdToDec(I2C_READ());
  dayOfMonth = bcdToDec(I2C_READ());
  month      = bcdToDec(I2C_READ());
  year       = bcdToDec(I2C_READ());
  
   randomSeed(analogRead(A0)); //Use the analog pins for a good seed value
  int fileNumber = random(999); //Select a random file #, 0 to 999
  char fileName[12]; //Max file name length is "12345678.123" (12 characters)
  sprintf(fileName, "log%03d.txt", fileNumber);


  createFile(fileName); //Creates a new file called log###.txt where ### is random

  Serial.print("Random file created: ");
  Serial.println(fileName);

  //Write something to OpenLog
  
  for(int x=0; x < 720; x++){ /* this loop reads the the 4 AI points and writes them to the 
                               to the logger at 5 sec intervals*/
  
  dat0 = analogRead(CH0);
  dat1 = analogRead(CH1);
  dat2 = analogRead(CH2);
  dat3 = analogRead(CH3);
//Need to scale the values here prior to writing them to the OpenLogger  
  OpenLog.print("Data Set ");
  OpenLog.println(x);
  OpenLog.print("CH0 = ");
  OpenLog.println(dat0);
  OpenLog.print("CH1 = ");
  OpenLog.println(dat1);
  OpenLog.print("CH2 = ");
  OpenLog.println(dat2);
  OpenLog.print("CH3 = ");
  OpenLog.println(dat3);
  OpenLog.println();
  OpenLog.println();
  
 
  if (hour < 10)
    Serial.print("0");
  Serial.print(hour, DEC);
  Serial.print(":");
  if (minute < 10)
    Serial.print("0");
  Serial.print(minute, DEC);
  Serial.print(":");
  if (second < 10)
    Serial.print("0");
  Serial.print(second, DEC);
  Serial.print("  ");
  Serial.print(Day[dayOfWeek]);
  Serial.print(", ");
  Serial.print(dayOfMonth, DEC);
  Serial.print(" ");
  Serial.print(Mon[month]);
  Serial.print(" 20");
  if (year < 10)
    Serial.print("0");
  Serial.println(year, DEC);
 
 
 

          delay (2000);
}

But whether I insert it in the main loop or not ,I get compile errors. I'm pretty sure it's a program structure thing as opposed to syntax.

Nope.. it's just not declared. You need a function called gotoCommandMode() in your code. Wherever you lifted the code from, is probably where you'll find it.

Wow that was amazingly quick ! My sincere thanks. I guess I need to either brush up on plagiarzing code or learn to write my own. With all the example sketches it's just too easy to jump into the middle. Where should I look in the Aduino reference to study up? Since I'm here I'll ask another question. The declaration occurs much further down in the code, is the declaration a run time operation? Again, many thanks.
Here is the Donor code.

#include <SoftwareSerial.h>
int CH0 = A0;
int CH1 = A1;
int CH2 = A2;
int CH3 = A3;
float dat0 = 0;
float dat1 = 0;
float dat2 = 0;
float dat3 = 0;
int rxPin = 3;
int txPin = 2;
int x=0;




//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
//Connect TXO of OpenLog to pin 3, RXI to pin 2
SoftwareSerial OpenLog(rxPin, txPin); //Soft RX on 3, Soft TX out on 2
//SoftwareSerial(rxPin, txPin)


int resetOpenLog = 4; //This pin resets OpenLog. Connect pin 4 to pin GRN on OpenLog.
//-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

int statLED = 13;
//{
//val = analogRead(analogPin);    // read the input pin
//  Serial.println(val);             // debug value
//}
 
float dummyVoltage = 3.50; //This just shows to to write variables to OpenLog

void setup() {                
  pinMode(statLED, OUTPUT);
  Serial.begin(9600);

  setupOpenLog(); //Resets logger and waits for the '<' I'm alive character
  Serial.println("OpenLog online");
}

void loop() {
  
  
  randomSeed(analogRead(A0)); //Use the analog pins for a good seed value
  int fileNumber = random(999); //Select a random file #, 0 to 999
  char fileName[12]; //Max file name length is "12345678.123" (12 characters)
  sprintf(fileName, "log%03d.txt", fileNumber);

  gotoCommandMode(); //Puts OpenLog in command mode
  createFile(fileName); //Creates a new file called log###.txt where ### is random

  Serial.print("Random file created: ");
  Serial.println(fileName);

  //Write something to OpenLog
  
  for(int x=0; x < 5; x++){ /* this loop reads the the 4 AI points and writes them to the 
                               to the logger at 5 sec intervals*/
  
  dat0 = analogRead(CH0);
  dat1 = analogRead(CH1);
  dat2 = analogRead(CH2);
  dat3 = analogRead(CH3);
//Need to scale the values here prior to writing them to the OpenLogger  
  OpenLog.print("Data Set ");
  OpenLog.println(x);
  OpenLog.print("CH0 = ");
  OpenLog.println(dat0);
  OpenLog.print("CH1 = ");
  OpenLog.println(dat1);
  OpenLog.print("CH2 = ");
  OpenLog.println(dat2);
  OpenLog.print("CH3 = ");
  OpenLog.println(dat3);
  OpenLog.println();
  OpenLog.println();
  delay(5000);
  }
  
  
  
  
  
  OpenLog.println(dummyVoltage);

  Serial.println("Text written to file");
  Serial.println("Reading file contents:");
  Serial.println();

  //Now let's read back
  gotoCommandMode(); //Puts OpenLog in command mode
  readFile(fileName); //This dumps the contents of a given file to the serial terminal

  //Now let's read back
  readDisk(); //Check the size and stats of the SD card

  Serial.println();
  Serial.println("File read complete");

  //Infinite loop
  Serial.println("Yay!");
  while(1) {
    digitalWrite(13, HIGH);
    delay(250);
    digitalWrite(13, LOW);
    delay(250);
  }
}

//Setups up the software serial, resets OpenLog so we know what state it's in, and waits
//for OpenLog to come online and report '<' that it is ready to receive characters to record
void setupOpenLog(void) {
  pinMode(resetOpenLog, OUTPUT);
  OpenLog.begin(9600);

  //Reset OpenLog
  digitalWrite(resetOpenLog, LOW);
  delay(100);
  digitalWrite(resetOpenLog, HIGH);

  //Wait for OpenLog to respond with '<' to indicate it is alive and recording to a file
  while(1) {
    if(OpenLog.available())
      if(OpenLog.read() == '<') break;
  }
}

//This function creates a given file and then opens it in append mode (ready to record characters to the file)
//Then returns to listening mode
void createFile(char *fileName) {

  //Old way
  OpenLog.print("new ");
  OpenLog.print(fileName);
  OpenLog.write(13); //This is \r

  //New way
  //OpenLog.print("new ");
  //OpenLog.println(filename); //regular println works with OpenLog v2.51 and above

  //Wait for OpenLog to return to waiting for a command
  while(1) {
    if(OpenLog.available())
      if(OpenLog.read() == '>') break;
  }

  OpenLog.print("append ");
  OpenLog.print(fileName);
  OpenLog.write(13); //This is \r

  //Wait for OpenLog to indicate file is open and ready for writing
  while(1) {
    if(OpenLog.available())
      if(OpenLog.read() == '<') break;
  }

  //OpenLog is now waiting for characters and will record them to the new file  
}

//Reads the contents of a given file and dumps it to the serial terminal
//This function assumes the OpenLog is in command mode
void readFile(char *fileName) {

  //Old way
  OpenLog.print("read ");
  OpenLog.print(fileName);
  OpenLog.write(13); //This is \r

  //New way
  //OpenLog.print("read ");
  //OpenLog.println(filename); //regular println works with OpenLog v2.51 and above

  //The OpenLog echos the commands we send it by default so we have 'read log823.txt\r' sitting 
  //in the RX buffer. Let's try to not print this.
  while(1) {
    if(OpenLog.available())
      if(OpenLog.read() == '\r') break;
  }  

  //This will listen for characters coming from OpenLog and print them to the terminal
  //This relies heavily on the SoftSerial buffer not overrunning. This will probably not work
  //above 38400bps.
  //This loop will stop listening after 1 second of no characters received
  for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
    while(OpenLog.available()) {
      char tempString[100];
      
      int spot = 0;
      while(OpenLog.available()) {
        tempString[spot++] = OpenLog.read();
        if(spot > 98) break;
      }
      tempString[spot] = '\0';
      Serial.write(tempString); //Take the string from OpenLog and push it to the Arduino terminal
      timeOut = 0;
    }

    delay(1);
  }

  //This is not perfect. The above loop will print the '.'s from the log file. These are the two escape characters
  //recorded before the third escape character is seen.
  //It will also print the '>' character. This is the OpenLog telling us it is done reading the file.  

  //This function leaves OpenLog in command mode
}

//Check the stats of the SD card via 'disk' command
//This function assumes the OpenLog is in command mode
void readDisk() {

  //Old way
  OpenLog.print("disk");
  OpenLog.write(13); //This is \r

  //New way
  //OpenLog.print("read ");
  //OpenLog.println(filename); //regular println works with OpenLog v2.51 and above

  //The OpenLog echos the commands we send it by default so we have 'disk\r' sitting 
  //in the RX buffer. Let's try to not print this.
  while(1) {
    if(OpenLog.available())
      if(OpenLog.read() == '\r') break;
  }  

  //This will listen for characters coming from OpenLog and print them to the terminal
  //This relies heavily on the SoftSerial buffer not overrunning. This will probably not work
  //above 38400bps.
  //This loop will stop listening after 1 second of no characters received
  for(int timeOut = 0 ; timeOut < 1000 ; timeOut++) {
    while(OpenLog.available()) {
      char tempString[100];
      
      int spot = 0;
      while(OpenLog.available()) {
        tempString[spot++] = OpenLog.read();
        if(spot > 98) break;
      }
      tempString[spot] = '\0';
      Serial.write(tempString); //Take the string from OpenLog and push it to the Arduino terminal
      timeOut = 0;
    }

    delay(1);
  }

  //This is not perfect. The above loop will print the '.'s from the log file. These are the two escape characters
  //recorded before the third escape character is seen.
  //It will also print the '>' character. This is the OpenLog telling us it is done reading the file.  

  //This function leaves OpenLog in command mode
}

//This function pushes OpenLog into command mode
void gotoCommandMode(void) {                                       <<<<<<<<<<<<<<<<---Here is the declaration well after the instance
  //Send three control z to enter OpenLog command mode
  //Works with Arduino v1.0
  OpenLog.write(26);
  OpenLog.write(26);
  OpenLog.write(26);

  //Wait for OpenLog to respond with '>' to indicate we are in command mode
  while(1) {
    if(OpenLog.available())
      if(OpenLog.read() == '>') break;
  }
}

gotoCommandMode() is a function, which is indicated by the presence of parentheses. It used to be, in many C compilers, that you had to either place the function itself before any calls to it (ie. before it in the file), or you had to declare the function with a line like:

void gotoCommandMode(void);

Then later in the file, you could have any call to it before or after the actual function.

Compilers have progressed since then, and you can have the function itself act as it's own declaration.

So, the function itself is a runnable piece of code. In this case, the void before the function name means that it does not return a value. The void in the parentheses means that it takes no arguments. When you call it (any gotoCommandMode(); ) statement, control passes to the function itself, and when it finishes running, control is returned to the statement after the call.

Forgot to mention...

Where should I look in the Aduino reference to study up?

In the giant green header at the top of the page, you'll find a navigation entry called "Reference", and on THAT page, you'll find references to all the standard Arduino-specific functions, and quite a number of C/C++ standard functions, "standard (included) libraries, and so on. On that same page, is a link to Libraries, which contains a list of, and docs on "extra" libraries that are not included with the IDE. Also, at the top, is "Learning", a good place to start.

There are a number of C++ tutorials on-line, Google will point you at many of them.

Thanks. I'm familiar with the Arduino reference. I guess just not familiar enough. The plan now is to build up my own sketch from scratch. I'll incrementally add functionality. Again thanks, I'm shocked at the quick and useful responses. Back to the sketch pad.