Shop OBEX P1 Docs P2 Docs Learn Events
making a 'C' function — Parallax Forums

making a 'C' function

Hi all, I’m trying to craft together a function for the following code to put in my library. I tried several methods but each and every one failed. I want the counter to return seconds for a LCD stopwatch I’m making. I know the ‘pause(1000)’ will not give me true seconds but I can deal with that later using some spin code, but for now I’m using C.
So to make this question more clear, I want a function I could call from main like ‘int counter()’ or something like that, that would output seconds. When I put the main body in a function if fails, and so I’m at a loss.
Any help would be most appreciated.
#include "simpletools.h"
#include "lcd.h"
int main() 
{
inithd44780(); 							//init LCD hd44780 function from my lib


// Main counter
for(int t = 0; t >= -1; t++) 							//count to ~ 
{ 
seconds = t; 
char str[80] ;
hours = seconds / 3600;
minutes = seconds / 60;
mins_left = minutes % 60;
secs_left = seconds % 60;

sprintf(str, " Time %d:%d:%d ",hours, mins_left, secs_left ); 
//above 20 line LCD must have 20 spaces after %d
LCDline(2); //go to line 2 on the LCD function from my lib
for(int i = 0; i < 20; i++) 							//allow only 20 characters 
{ 
data(); 										//following this command is data function from my lib
set_outputs (lsbpin,msbpin, (str[i]));						//one character at a time from msbit to lsbit 
pause(TW); 									//wait 1 mS
strobe();									 //latch the data function from my lib
}
pause(947); //wait 947mS 
}
}

Comments

  • Can you post the version that fails? C is a little fussier than spin, so my suspicion is that you're trying to call a function without declaring it first. C/C++ compiles in order, so if you're trying to call a function that comes later in the code, you have to tell the compiler it's coming, like this:
    void MyFunction(void);  // declare it, so C knows it exists, what it returns, and what arguments it takes
    
    void main(void)
    {
      MyFunction();  // calling the function we declared
    }
    
    void MyFunction(void)  // now actually implement it
    {
      Print( "We're here!");
    }
    

    If the body of MyFunction came before main(), I wouldn't need the forward declaration (that's what that thing is called).

    If that isn't what's happening, post the code that's not compiling and someone here will surely be able to point in the right direction.
  • Thank you for your reply. The problem is the function must return 3 (hours, mins_left, secs_left) parameters, and I don't know how to accomplish that. I tried several different ways but no good. Any ideas?
  • C/C++ does not allow returning multiple values. In C, your best bet is to create a box which can hold all three things in it, and then your "one thing" that is returned from the function is a box.

    We call that "box" a "struct".
    struct time_t {
        int hours;
        int minutes_left;
        int seconds_left;
    };
    
    time_t do_stuff () {
      time_t now = {5, 36, 47};
    
      return now;
    }
    
  • You can also create the variables in the calling function pass them in by pointer, and fill them out in the function, which is more efficient than returning a structure on the stack. (You can also pass a pointer to the struct, which is more efficient still)
    void MyFunc( int *hours, int *mins, int *secs)
    {
      *hours = 10;  // store the result in what the variable points at
      *mins = 5;
      *secs = 3;
    }
    
    void main(void)
    {
      int hrResult, minResult, secResult;  // local variables to store results
    
      // pass pointers to the function using &
      MyFunc( &hrResult, &minResult, &secResult);
    
      // values are now in the variables
    }
    

    or
    struct MyTime {
        int hours;
        int minutes_left;
        int seconds_left;
    };
    
    void GetTimeRemaining( MyTime * t ) {
      t->hours = 5;
      t->minutes_left = 36;  // fill in struct members through the pointer
      t->seconds_left = 47;
    
      return now;
    }
    
    void main(void)
    {
      MyTime remain;  // local struct to hold result
    
      GetTimeRemaining( &remain );  // pass pointer to local struct to function
    
      Print( remain.hours );
    }
    
  • Great I'll give it a try.Thanks!
Sign In or Register to comment.