Thread: conditional function

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    110

    conditional function

    Is there anyway to define a function conditionally? For example in psuedo code:

    IF (condition 1) then function(x)=x^2 ELSE function (x) = x.

    I tried defining the function within an if staement, and it didn't work. I also thought about using the if statement within the definition for the function, but then is it possible to use variables/call other functions from 'main'? i.e. in this case I would need to pass 'condition 1' from main to the if statement within 'function'...(or condition 1 uses variables from main).

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The most likely option, it seems to me, would be to define two functions, calling the appropriate one based on your if. Is there some reason why this isn't an option? (Edit: Or, I suppose, if the function call is going to happen later/repeatedly, then you could use a function pointer to set up the appropriate function to use (again, this would still require defining two functions).)

  3. #3
    Hacker MeTh0Dz's Avatar
    Join Date
    Oct 2008
    Posts
    111
    What you could do is just use one function, and as one of your parameters pass an flag that will allow the function to run one if statement or the other.

    Example.

    Code:
    void Ex_Funt(int var1, char * var2, int flag) {
         if (flag == 0) {
              /* ... */
         }
         else if (flag == 1) {
              /* ... */
         }
         else {
              /* ... */
         }
    }
    Another option could be using two functions and then just a function pointer, or just call the function.

    Just a couple possible solutions.

  4. #4
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    You can use preprocessor directives to generate your function however(s) however you wish. #if is what you want to use.
    Mainframe assembler programmer by trade. C coder when I can.

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Well I think I could use two functions to do what I want, and then just use each one as appropriate in the loop. I had a look at function pointers, but didn't really understand it...will have to save that for another day.

    Thanks

  6. #6
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Function pointers may or may not be overkill.
    Code:
    #include <stdio.h>
    
    int foo(int x)
    {
       return x * x;
    }
    
    int bar(int x)
    {
       return x;
    }
    
    int test(int x, int i)
    {
       int (*function)(int) = i ? foo : bar;
       return function(x);
    }
    
    int main(void)
    {
       int i, a = 5;
       for (i = 0; i < 2; ++i)
       {
          printf("test(&#37;d,%d) = %d\n", a, i, test(a,i));
       }
       return 0;
    } 
    
    /* my output
    test(5,0) = 5
    test(5,1) = 25
    */
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  7. #7
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Chappell Hill, Texas
    Posts
    2,332
    Here's a function pointer example - it's really not that difficult a concept:
    Code:
    #include <stdio.h>
    
    int foo(int x) {
    	return x * x;
    }
    
    int bar(int x) {
    	return x;
    }
    
    int main(void) {
    
    	int ( *myfunc ) () ; 
       
    	myfunc = foo ; 
    	printf("myfunc(5) = &#37;d\n", myfunc(5) );
    
    	myfunc = bar ; 
    	printf("myfunc(5) = %d\n", myfunc(5) );
       
    	return 0;
    }
    Output:
    Code:
    myfunc(5) = 25
    myfunc(5) = 5
    Mainframe assembler programmer by trade. C coder when I can.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    110
    Thanks for the examples I'm understading it now, and have my program working (this part at least).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  2. Question..
    By pode in forum Windows Programming
    Replies: 12
    Last Post: 12-19-2004, 07:05 PM
  3. c++ linking problem for x11
    By kron in forum Linux Programming
    Replies: 1
    Last Post: 11-19-2004, 10:18 AM
  4. Replies: 4
    Last Post: 11-23-2003, 07:15 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM