functions not calling correctly

This is a discussion on functions not calling correctly within the C Programming forums, part of the General Programming Boards category; I'm writing a program that takes a color number (1-7) and prints out it's respective RGB. I created a function ...

  1. #1
    Registered User
    Join Date
    Feb 2011
    Posts
    18

    functions not calling correctly

    I'm writing a program that takes a color number (1-7) and prints out it's respective RGB. I created a function that takes the color and sets its RGB values then returns it back to the function. For some reason the r, g, and b ints aren't returning back and printing out correctly. Any ideas???
    Thanks in advance!

    if (InObject(shapeLetter, newX, newY, arg1, arg2, arg3)){
    Rectangle(arg1, arg2, newX, newY);
    printPix(color, r, g, b);
    newR = r;
    newG = g;
    newB = b;
    }
    printf("%d %d %d # %d %d\n", newR, newG, newB, x, y);
    }
    }

    int printPix(int color, int r, int g, int b) {
    if (color == 0){
    r = 0; g = 0; b = 0;
    }
    else if (color == 1){
    r = MAX_COLOR; g = 0; b = 0;
    }
    else if (color == 2){
    r = 0; g = MAX_COLOR; b = 0;
    }
    else if (color == 3){
    r = 0; g = 0; b = MAX_COLOR;
    }
    else if (color == 4){
    r = 0; g = MAX_COLOR; b = MAX_COLOR;
    }
    else if (color == 5){
    r = MAX_COLOR; g = 0; b = MAX_COLOR;
    }
    else if (color == 6){
    r = MAX_COLOR; g =MAX_COLOR ; b = 0;
    }
    else if (color == 7){
    r = MAX_COLOR; g = MAX_COLOR; b = MAX_COLOR;
    }
    return;
    }

  2. #2
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Well... mostly it's because your function isn't returning anything of value. In fact you should be getting compiler warnings about it not returning a value...

    Then there's the little problem that a C program can only return 1 value... not three.

    If you want to return more than one value you need to pass in a pointer to the variables that are to be changed and operate on the pointers...

    Code:
    void PrintPix(int color, int *r, int *g, int *b)
      { if (color == 0){r = 0; g = 0; b = 0;}
        else if (color == 1){*r = MAX_COLOR; *g = 0; *b = 0;}
        else if (color == 2){*r = 0; *g = MAX_COLOR; *b = 0;}
        else if (color == 3){*r = 0; *g = 0; *b = MAX_COLOR;}
        else if (color == 4){*r = 0; *g = MAX_COLOR; *b = MAX_COLOR;}
        else if (color == 5){*r = MAX_COLOR; *g = 0; *b = MAX_COLOR;}
        else if (color == 6){*r = MAX_COLOR; *g =MAX_COLOR ; *b = 0;}
        else if (color == 7){*r = MAX_COLOR; *g = MAX_COLOR; *b = MAX_COLOR;} } 
    
    
    
    // call it as...
    PrintPix(color,&r,&g,&b);

  3. #3
    msh
    msh is offline
    Novice
    Join Date
    Jul 2009
    Posts
    568
    I would just like to point out that it is much more common have color as a structure. E.g.
    Code:
    #include <stdio.h>
    
    
    typedef struct color
    {
      int red, green, blue;
    } ColorRGB;
    
    ColorRGB make_rgb(int R, int G, int B);
    
    
    int main(void)
    {
      ColorRGB orange;
      orange = make_rgb(255, 127, 0);
      
      return 0;
    }
    
    
    ColorRGB make_rgb(int R, int G, int B)
    {
      ColorRGB C;
      
      C.red = R;
      C.green = G;
      C.blue = B;
      
      return C;
    }
    Just an example.
    Disclaimer: This post shows my ignorance at the time of its making. I claim ownership of but not responsibility for all errors in it. Reference at your own peril.

  4. #4
    Registered User
    Join Date
    Aug 2006
    Posts
    12
    I would use a switch statement in teh function instead of a battery of else-if constructs.

    Actually, a common solution to this is not to make a function at all, but a constant map. It's faster, but you have to take care not to go out of bounds on the color index.

    Code:
    /* in your headers, use */
    #define MAX_COLOR 255
    
    typedef struct _rgbcolor {
       int r,g,b;
    } rgbcolor_t;
    
    rgbcolor_t colormap[]= {
       { 0, 0, 0 },
       { MAX_COLOR, 0, 0 },
       { 0, MAX_COLOR, 0 },
       { 0, 0, MAX_COLOR },
       { 0, MAX_COLOR, MAX_COLOR },
       { MAX_COLOR, 0, MAX_COLOR },
       { 0, MAX_COLOR, MAX_COLOR},
       { MAX_COLOR, MAX_COLOR, MAX_COLOR }
    };
    
    
    /* in your code, use: */
    int newR, newG, newB;
    
    newR = colormap[color].r;
    newG = colormap[color].g;
    newB = colormap[color].b;
    
    /* OR */
    rgbcolor_t myRGB;
    
    myRGB = colormap[color];
    my 2 cents,
    LT

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Functions calling other functions.
    By kbro3 in forum C++ Programming
    Replies: 2
    Last Post: 12-26-2009, 11:10 PM
  2. Problems calling functions
    By barryr in forum C Programming
    Replies: 12
    Last Post: 12-03-2009, 07:44 AM
  3. Stack issues when calling a COM library
    By notnot in forum C Programming
    Replies: 3
    Last Post: 06-01-2009, 02:12 AM
  4. Calling functions via variable
    By SoundFX in forum C++ Programming
    Replies: 3
    Last Post: 08-30-2005, 10:26 PM
  5. calling functions in parallel
    By zeus in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 02-01-2002, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21