Thread: Functions in C

  1. #1
    Registered User
    Join Date
    Jun 2005
    Posts
    108

    Functions in C

    Hi, just got round to introducing some functions into my C program but am having a problem.

    im trying to introduce two variables into a function which will use the time functions in C to convert the two variables into a time and calendar date.

    different sections of the time and date (i.e. the numer of the day, hour) are then stored into seperate strings and sent back to
    int main
    where they are then used for further uses.

    when i compile, i get the error :
    old style header.....
    im using this code in my header file which is called by the line in the main program:
    Code:
    int GPS_calculate(int GPSweek, int GPSsec);
    header file:

    Code:
    int GPS_calculate(int GPSweek, int GPSsec);
    
    {
    
            double abs_GPS;
    
            time_t current;
            
    
            struct tm* loctime;
    
            abs_GPS = ((604800 * GPSweek) + (GPSsec)); /* convert to absolute GPS time	*/
    
            current = (abs_GPS + 315964800);           /* Add absolute GPS time to the UTC epoch  */
    
            loctime = localtime(&current);			   /* Convert it to local time representation  */
    
            fputs(asctime(loctime), stdout);
            fputs("\n", stdout);					   /* Print out the date and time in the standard format  */
    
           
            strftime(bufftime, SIZE, "%y %m %d %H %M %S", loctime);		/* Print out the date and time in needed format. */
            sscanf(bufftime, "%8d", &time);
    
    															/*THE DAY*/
            strftime(buffday, SIZE, "%j", loctime); sscanf(buffday, "%3d", &day); // printf("Day is: %3d           ", day);
    
    															/*THE YEAR*/
            strftime(buffyear, SIZE, "%Y", loctime); sscanf(buffyear, "%4d", &year); // printf("Year is: %4d           ", year);
    
    															/*THE HOUR*/
            strftime(buffhour1, SIZE, "%H", loctime); sscanf(buffhour1, "%2d", &hour); // printf("Hour is: %2d          ", hour);
    
            strftime(buffminute, SIZE, "%M", loctime); sscanf(buffminute, "%2d", &minute);
    		
    		return (buffyear[12], buffday[12], buffhour[30], buffhour1[12], buffminute[10], bufftime[19]);
    
    	}
    all help would appreciated

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Code:
    return (buffyear[12], buffday[12], buffhour[30], buffhour1[12], buffminute[10], bufftime[19]);
    You can't do this, the only thing this will return is the last value in this list, which is bufftime[19]. If you want to return the other variables, you need to pass variables by reference in the parameter list.

    You have all of these variables like buffhour and buffyear in your function. I don't know if they're in a standard library you are using or if they're declared globally, but if they aren't, you have to pass them to the function or declare them within the function.

    Also you have a trailing semicolon after the declaration in your function definition. That shouldn't be there.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    thanks.

    i was wondering where the problem lay.

    because when i removed the semi-colon, i end up getting many more errors!!

  4. #4
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Well, those are the errors you should be getting, cause that semicolon isn't supposed to be there.

    Feel free to post your new errors.

    Also, even though you're only posting the important parts of your code so you don't take too much space, it's good if you post a list of the libraries you're using and all of your global/local variables.
    Sent from my iPadŽ

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Here's the misplaced semicolon:
    Code:
    int GPS_calculate(int GPSweek, int GPSsec);
    
    {
    Change it to
    Code:
    int GPS_calculate(int GPSweek, int GPSsec)
    
    {
    return (buffyear[12], buffday[12], buffhour[30], buffhour1[12], buffminute[10], bufftime[19]);
    I think shoobsie removed that semicolon.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by SlyMaelstrom
    Code:
    return (buffyear[12], buffday[12], buffhour[30], buffhour1[12], buffminute[10], bufftime[19]);
    You can't do this, the only thing this will return is the last value in this list, which is bufftime[19]. If you want to return the other variables, you need to pass variables by reference in the parameter list.

    You have all of these variables like buffhour and buffyear in your function. I don't know if they're in a standard library you are using or if they're declared globally, but if they aren't, you have to pass them to the function or declare them within the function.

    Also you have a trailing semicolon after the declaration in your function definition. That shouldn't be there.
    hi, how do i pass variables by reference in the parameter list!?

    can you give me a small example?

    here's all the errors!!

    c:\program files\microsoft visual studio\vc98\include\gpstime.h(31) : warning C4244: '=' : conversion from 'double' to 'long', possible loss of data
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(39) : error C2065: 'bufftime' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(39) : error C2275: 'SIZE' : illegal use of this type as an expression
    c:\program files\microsoft visual studio\vc98\include\windef.h(322) : see declaration of 'SIZE'
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(43) : error C2065: 'buffday' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(43) : error C2275: 'SIZE' : illegal use of this type as an expression
    c:\program files\microsoft visual studio\vc98\include\windef.h(322) : see declaration of 'SIZE'
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(43) : error C2065: 'day' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(46) : error C2065: 'buffyear' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(46) : error C2275: 'SIZE' : illegal use of this type as an expression
    c:\program files\microsoft visual studio\vc98\include\windef.h(322) : see declaration of 'SIZE'
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(46) : error C2065: 'year' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(49) : error C2065: 'buffhour1' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(49) : error C2275: 'SIZE' : illegal use of this type as an expression
    c:\program files\microsoft visual studio\vc98\include\windef.h(322) : see declaration of 'SIZE'
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(49) : error C2065: 'hour' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(51) : error C2065: 'buffminute' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(51) : error C2275: 'SIZE' : illegal use of this type as an expression
    c:\program files\microsoft visual studio\vc98\include\windef.h(322) : see declaration of 'SIZE'
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(51) : error C2065: 'minute' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2065: 'bbuffday' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2065: 'buffhour' : undeclared identifier
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2109: subscript requires array or pointer type
    c:\program files\microsoft visual studio\vc98\include\gpstime.h(53) : error C2109: subscript requires array or pointer type
    c:\documents and settings\shss.grupogmv\my documents\egnos archiving program\c stuff\final program with functions\egnos archiving program.cpp(838) : warning C4508: 'main' : function should return a value; 'void' return type assumed
    Error executing cl.exe.

    Egnos archiving program.obj - 22 error(s), 2 warning(s)

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    How about you start with the first one, and go down the list. In regards to errors, how about actually declaring 'bufftime' some place?


    Quzah.
    Hope is the first step on the road to disappointment.

  8. #8
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by quzah
    In regards to errors, how about actually declaring 'bufftime' some place?


    Quzah.
    i did declare it, in my main program.....should it be passed to the header file too...?

    as in..

    Code:
    int GPS_calculate(int GPSweek, int GPSsec, int SIZE, int buffhour[30],
    int day, char hour, int year, int minute, int buffhour1[12],
    char buffyear[12], char buffday[12], char bufftime[19], char buffminute[10])
    ??

    Quote Originally Posted by quzah
    How about you start with the first one, and go down the list.

    Quzah.
    ..........???

    sarcasmisthelowestformofwit.cpp

  9. #9
    ---
    Join Date
    May 2004
    Posts
    1,379
    You defined a function in a header file?

  10. #10
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by sand_man
    You defined a function in a header file?
    thats my problem!!

    im still learning!

    i saw an example in a book and tried to follow it.

    i want to take out a certain section of reused code from the main program so it doesnt make my main section too cluttered.

    i put it in a file called gpstime.h

    and in the main program, i wrote

    Code:
    #include "gpstime.h"
    i could leave it in the main and use it as a function. but i want to split up the large section of code into smaller files.

    what am i doing wrong?

  11. #11
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Header files generally contain function prototypes and macro definitions. The file declarations themselves typically go in .c files. If you want to break up your code into various files then you should probably keep the actual code for the functions in .c files and then you can put the function prototypes for functions you'll need to call from a different .c file into a header file.
    If you understand what you're doing, you're not learning anything.

  12. #12
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    Quote Originally Posted by itsme86
    Header files generally contain function prototypes and macro definitions. The file declarations themselves typically go in .c files. If you want to break up your code into various files then you should probably keep the actual code for the functions in .c files and then you can put the function prototypes for functions you'll need to call from a different .c file into a header file.
    and how would the C files be called from the program?

    and where should they be saved?

    thanks

  13. #13
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You compile each .c file into an object file and then link all of the necessary object files and library into your executable. Your compiler does most of this stuff automagically. I use gcc mostly so I'll show you an example using that.

    One way:
    Code:
    $ gcc -c file1.c
    $ gcc -c file2.c
    $ gcc file1.o file2.o -o executable_name
    Code:
    $ gcc -c file1.c
    $ gcc file1.o file2.c -o executable_name
    Code:
    $ gcc file1.c file2.c -o executable_name
    All of these give you the same executable, but they work a bit differently. You'd think the last example is only one line so why not just do that? Big programs take time to compile. If you create a .c file and then compile it into an object file, you can use that object file during linking with programs without having to recompile it.

    So let's say you have file1.c and file2.c. You create some functions in file1.c and compile it into an object file (gcc -c file1.c). Now you work with file2.c and you run into a little bug that causes you to keep changing file2.c and then recompile to test. You need to link with file1.c to create the executable, but since you compiled file1.c into an executable it doesn't need to be recompiled every time, just linked into the executable.
    If you understand what you're doing, you're not learning anything.

  14. #14
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    Quote Originally Posted by shoobsie
    i did declare it, in my main program.....should it be passed to the header file too...?

    as in..

    Code:
    int GPS_calculate(int GPSweek, int GPSsec, int SIZE, int buffhour[30],
    int day, char hour, int year, int minute, int buffhour1[12],
    char buffyear[12], char buffday[12], char bufftime[19], char buffminute[10])
    ??
    ..........???
    sarcasmisthelowestformofwit.cpp
    No, everything you declare in main is local to main. You can't use them in the functions unless you pass them in a function call. As for the values you want to pass back to main (the function's output, if you will), you pass variables by reference in this manner:

    Code:
    #include <stdio.h>
    
    void function(int passedbyValue, int passedbyValue2, int *passedbyReference) {
        *passedbyReference = passedbyValue + passedbyValue2;
        }
    
    int main() {
       int referedtoVar = 0;
    
       function(5, 6, &referedtoVar);
    
       printf("%d", referedtoVar);
       return 0;
       }
    Now from what I understand, this is not truly a C equivalent to the C++ Pass by Reference, it's just a way to stimulate it using pointers. This is what you want to do though.
    Sent from my iPadŽ

  15. #15
    Registered User
    Join Date
    Jun 2005
    Posts
    108
    no problem guys, i'll try that tommorrow morning.

    by the way, im using VC++ and windows...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM