Thread: Help with functions!!

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    Help with functions!!

    Hi I need to convert an old program i created into subroutines/functions and for some reason I can't seem to figure it out..

    Code:
    #include <stdio.h> /* use standard I/O functions */
    /* Defines constants */
    #define seconds_per_centuries 3153600000LL
    #define seconds_per_years 31536000LL
    #define seconds_per_days 86400LL
    #define seconds_per_hours 3600LL
    #define seconds_per_minutes 60LL
    #define A_to_B 9223372036854775807LL
    #define B_to_C 8923333136789666910LL
    #define C_to_D 9125393333929333512LL
    #define D_to_A 8999569369147555127LL
    #define A_to_A 36271667876721331356LL
    
    int main ()
    { /*begin main */
    
    long long int seconds;
    long long int centuries;
    long long int years;
    long long int days;
    long long int hours;
    long long int minutes;
    
    
    /* Calculates distance */
    centuries = A_to_B / seconds_per_centuries;
    years = A_to_B % seconds_per_centuries / seconds_per_years;
    days = A_to_B % seconds_per_years / seconds_per_days;
    hours = A_to_B % seconds_per_days / seconds_per_hours;
    minutes = A_to_B % seconds_per_hours / seconds_per_minutes;
    seconds = A_to_B % seconds_per_minutes;
    
    /*Prints it on the terminal*/
    printf("========================================================================$
            printf("              Centuries     Years     Days     Hours     Minutes$
            printf("A to B        ");
            printf("%lld    ",centuries);
            printf("%lld        ",years);
            printf("%lld      ",days);
            printf("%lld        ",hours);
            printf("%lld          ",minutes);
            printf("%lld     \n",seconds);
    
    /* Calculates distance */
    
    centuries = B_to_C / seconds_per_centuries;
    years = B_to_C % seconds_per_centuries / seconds_per_years;
    days = B_to_C % seconds_per_years / seconds_per_days;
    hours = B_to_C % seconds_per_days / seconds_per_hours;
    minutes = B_to_C % seconds_per_hours / seconds_per_minutes;
    seconds = B_to_C % seconds_per_minutes;
    
    /*Prints it on the terminal*/
    
            printf("B to C        ");
            printf("%lld    ",centuries);
            printf("%lld        ",years);
            printf("%lld      ",days);
            printf("%lld         ",hours);
            printf("%lld          ",minutes);
            printf("%lld     \n",seconds);
    
    /* Calculates distance */
    
    centuries = C_to_D / seconds_per_centuries;
    years = C_to_D % seconds_per_centuries / seconds_per_years;
    days = C_to_D % seconds_per_years / seconds_per_days;
    hours = C_to_D % seconds_per_days / seconds_per_hours;
    minutes = C_to_D % seconds_per_hours / seconds_per_minutes;
    seconds = C_to_D % seconds_per_minutes;
    
    /*Prints it on the terminal*/
    
            printf("C to D        ");
            printf("%lld    ",centuries);
            printf("%lld        ",years);
            printf("%lld      ",days);
            printf("%lld        ",hours);
            printf("%lld           ",minutes);
            printf("%lld     \n",seconds);
    
    /* Calculates distance */
    
    centuries = D_to_A / seconds_per_centuries;
    years = D_to_A % seconds_per_centuries / seconds_per_years;
    days = D_to_A % seconds_per_years / seconds_per_days;
    hours = D_to_A % seconds_per_days / seconds_per_hours;
    minutes = D_to_A % seconds_per_hours / seconds_per_minutes;
    seconds = D_to_A % seconds_per_minutes;
    
    /*Prints it on the terminal*/
    
            printf("D to A        ");
            printf("%lld    ",centuries);
            printf("%lld        ",years);
            printf("%lld       ",days);
            printf("%lld        ",hours);
            printf("%lld          ",minutes);
            printf("%lld     \n",seconds);
            printf("================================================================$
    /* Calculates distance */
    
    centuries = A_to_A / seconds_per_centuries;
    years = A_to_A % seconds_per_centuries / seconds_per_years;
    days = A_to_A % seconds_per_years / seconds_per_days;
    hours = A_to_A % seconds_per_days / seconds_per_hours;
    minutes = A_to_A % seconds_per_hours / seconds_per_minutes;
    seconds = A_to_A % seconds_per_minutes;
    
    /*Prints it on the terminal*/
    
            printf("A to A        ");
            printf("%lld   ",centuries);
            printf("%lld        ",years);
            printf("%lld      ",days);
            printf("%lld       ",hours);
            printf("%lld         ",minutes);
            printf("%lld     \n",seconds);
    
    
    }

    NEW CODE WITH FUNCTION

    Code:
    #include <stdio.h> /* use standard I/O functions */
    /* Defines constants */
    #define seconds_per_centuries 3153600000LL
    #define seconds_per_years 31536000LL
    #define seconds_per_days 86400LL
    #define seconds_per_hours 3600LL
    #define seconds_per_minutes 60LL
    #define A_to_B 9223372036854775807LL
    #define B_to_C 8923333136789666910LL
    #define C_to_D 9125393333929333512LL
    #define D_to_A 8999569369147555127LL
    #define A_to_A 36271667876721331356LL
    
    int main ()
    { /*begin main */
    
    long long int seconds;
    long long int centuries;
    long long int years;
    long long int days;
    long long int hours;
    long long int minutes;
    calculations();
    
    /*Prints it on the terminal*/
    /*printf("%lld", AtoB(A_to_B, seconds_per_centuries, seconds_per_years, seconds_per_days, seconds_per_hours, seconds_per_minutes))*/
    printf("Enter path length to calculate:\n");
    scanf("%lld", &calculations);
    
    {
    int calculations(int );
    
    
    /* Calculates distance */
    centuries = pathlength / seconds_per_centuries;
    years = pathlength % seconds_per_centuries / seconds_per_years;
    days = pathlength % seconds_per_years / seconds_per_days;
    hours = pathlength % seconds_per_days / seconds_per_hours;
    minutes = pathlength % seconds_per_hours / seconds_per_minutes;
    seconds = pathlength % seconds_per_minutes;
    
    }


    That's what I have so far and I get multiple errors as it is.. Does anyone know what I am doing wrong or is able to point me in the right direction? (here are my errors so far)

    Code:
    cc     func.c   -o func
    func.c: In function 'main':
    func.c:39: warning: format '%lld' expects type 'long long int *', but argument 2 has type 'int (*)()'
    func.c:47: error: 'pathlength' undeclared (first use in this function)
    func.c:47: error: (Each undeclared identifier is reported only once
    func.c:47: error: for each function it appears in.)
    make: *** [func] Error 1

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    That's what I have so far and I get multiple errors as it is.. Does anyone know what I am doing wrong or is able to point me in the right direction? (here are my errors so far)
    You're defining and calling the function incorrectly. You need to read some function tutorials. We have one here, and Google has plenty more. Get your text book out, read it and work the examples. Then give this another shot. Some issues for starters:

    • Don't declare a function inside another function.
    • Make sure you return an int from main, usually zero for success.
    • Don't read into the function name with scanf. Use a temporary variable, call it pathlength, and read in to that.
    • After you read in pathlength, pass that in to calculations, along with the pointers to all the variables you need to calculate and send back to main, like centuries, years, etc.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    updated code/bugs

    After watching a few tutorials I came up with this, but I'm still getting a few bugs I do not understand.

    Code:
    #include <stdio.h> /* use standard I/O functions */
    /* Defines constants */
    #define seconds_per_centuries 3153600000
    #define seconds_per_years 31536000
    #define seconds_per_days 86400
    #define seconds_per_hours 3600
    #define seconds_per_minutes 60
    #define A_to_B 9223372036854775807
    #define B_to_C 8923333136789666910
    #define C_to_D 9125393333929333512
    #define D_to_A 8999569369147555127
    #define A_to_A 36271667876721331356
    double calculations(double pathlength);
    
    double calculations(double pathlength)
    {
    
    /* Calculates distance */
    double centuries = pathlength / seconds_per_centuries;
    double years = pathlength % seconds_per_centuries / seconds_per_years;
    double days = pathlength % seconds_per_years / seconds_per_days;
    double hours = pathlength % seconds_per_days / seconds_per_hours;
    double minutes = pathlength % seconds_per_hours / seconds_per_minutes;
    double seconds = pathlength % seconds_per_minutes;
    
    return centuries, years, days, hours, minutes, seconds;
    }
    
    int main ()
    { /*begin main */
    
    double seconds;
    double centuries;
    double years;
    double days;
    double hours;
    double minutes;
    
    
    /*Prints it on the terminal*/
    /*printf("%lld", AtoB(A_to_B, seconds_per_centuries, seconds_per_years, seconds_per_days, seconds_per_hours, seconds_per_minutes))*/
    printf("Enter path length to calculate:\n");
    scanf("%df", &pathlength);
    }

    Here are my errors:

    Code:
    cc     func.c   -o func
    func.c: In function 'calculations':
    func.c:29: error: invalid operands to binary % (have 'double' and 'long int')
    func.c:30: error: invalid operands to binary % (have 'double' and 'int')
    func.c:31: error: invalid operands to binary % (have 'double' and 'int')
    func.c:32: error: invalid operands to binary % (have 'double' and 'int')
    func.c:33: error: invalid operands to binary % (have 'double' and 'int')
    func.c: In function 'main':
    func.c:52: error: 'pathlength' undeclared (first use in this function)
    func.c:52: error: (Each undeclared identifier is reported only once
    func.c:52: error: for each function it appears in.)
    func.c:157: error: expected declaration or statement at end of input
    make: *** [func] Error 1
    thanks in advance!

  4. #4
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Looks like you didn't do quite enough reading. You can't return multiple values from a function like that, it doesn't do what you think. Also, re-read this:
    Quote Originally Posted by anduril462 View Post
    • After you read in pathlength, pass that in to calculations, along with the pointers to all the variables you need to calculate and send back to main, like centuries, years, etc.
    When you have to return multiple values in C, you have to pass in a pointer to each one and modify the value through the pointer. E.g.
    Code:
    #include <stdio.h>
    
    
    void foo(int x)
    {
        x = 17;  // only change a local copy of x
    }
    
    
    void bar(int *x)
    {
        *x = 42;  // dereference the pointer, so we change the value back in main
    }
    
    
    int main(void)
    {
        int y = 10;
    
    
        printf("y = %d\n", y);
        foo(y);
        printf("after foo(y) = %d\n", y);
        bar(&y);  // pass the address, so bar() can change this one, not it's local copy
        printf("after bar(&y), y = %d\n", y);
    
    
        return 0;
    }
    Note that the value of y in main isn't changed by calling foo, only by passing a pointer to y to bar().

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by alex33zebras View Post
    After watching a few tutorials I came up with this,
    Turn off the You Tube videos and go old school... yes, get an actual textbook.

    Start on page 1, read and reread as necessary.
    Type up and compile the examples and quizzes.
    Play with the code, break it, fix it, improve it until you understand what it's doing.
    Now you may turn to page 2...

    Repeat this process until you've read every page and tried all the code.

    This will give you a 75% chance of becoming a half way decent programmer.
    Watching videos is --and always will be-- a complete waste of your time.

    The only way to learn programming is to get directly involved in programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Creating Functions & passing information to other functions
    By RyanLeonard in forum C Programming
    Replies: 4
    Last Post: 10-28-2010, 12:17 PM
  2. Calling functions from functions
    By droseman in forum C Programming
    Replies: 4
    Last Post: 01-21-2010, 06:33 AM
  3. Replies: 7
    Last Post: 04-19-2006, 11:17 AM
  4. Replies: 6
    Last Post: 05-06-2003, 03:08 PM
  5. Replies: 1
    Last Post: 01-20-2002, 11:50 AM