Thread: Function pointers with returning values

  1. #1
    Registered User
    Join Date
    Oct 2013
    Posts
    24

    Function pointers with returning values

    I am to make a program that uses function pointers to give an output of adding numbers together or taking the average of these three numbers. I have almost most of it figured out, but am running into a problem. I am supposed to print out also which option they choose, but when I put a print statement into the function, it prints that statement out twice. I'm assuming because the first time it goes to that function and prints it out, then when I call that function in the print statement in main, it prints it out again. I'm not sure. Any advice would be appreciated. Thank you.

    Code:
    #include <stdio.h>
    
    
    int addFunct(int num1, int num2, int num3);
    int averageFunct(int num1, int num2, int num3);
    
    
    int main() 
    {
    int num1 , num2, num3, option;
    int (*f[2])(int num1, int num2, int num3) = {addFunct, averageFunct};
    
    
    printf("Please enter a number greater than 0 to start.\nEnter a negative number to quit.\n");
    printf("Please enter the first number.\n");
    
    
        while (num1 >= 0)
        {
        
    
    
    scanf("%d", &num1);
    printf("Please enter the second number.\n");
    scanf("%d", &num2);
    printf("Please enter the third number.\n");
    scanf("%d", &num3);
    printf("What would you like to do? Please pick the appropriate number.\n");
    scanf("%d", &option);
    
    
        if (option >= 0 && option < 2)
            (*f[option])(num1, num2, num3);
        printf("The answer is %d", f[option](num1, num2, num3));
        printf("Please enter the first number.\n");
        scanf("%d", &num1);
        
        }
        
    
    
        
    return 0;
        
    }
    
    
    int addFunct(int num1, int num2, int num3)
    {
        int add = 0;
        printf("You have chosen to add the two numbers.\n" );
        add = num1 + num2 + num3;
        return add;
         
    }
    
    
    int averageFunct(int num1, int num2, int num3)
    {
        int average = 0;
        
         printf("You have chosen to average the three numbers.\n");
        average = (num1 + num2 + num3) / 3;
        return average;
    }

  2. #2
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    Code:
    ...
        if (option >= 0 && option < 2)
            (*f[option])(num1, num2, num3);
        printf("The answer is %d", f[option](num1, num2, num3));
    ...
    You're running the function twice. You should move the printf() statement into the if() condition, also your bound checking only blocks the one call and then you call it in the printf() regardless if its in bounds or not which is a big bug.

  3. #3
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    My only problem with moving that print statement into the if() condition is that we are not supposed to have nested if statements. If I chose this route, it would need three different if statements, which I am assuming I shouldn't have.

  4. #4
    Ultraviolence Connoisseur
    Join Date
    Mar 2004
    Posts
    555
    No...you can reduce the 2 lines to one, as such:
    Code:
        if (option >= 0 && option < 2)
            printf("The answer is %d", f[option](num1, num2, num3));
    This fixes your problem...because you are only running the function ONCE now. Before you were running it for no reason once, and then running it again in the print statement...get it?

  5. #5
    Registered User
    Join Date
    Oct 2013
    Posts
    24
    I believe I do now! Thank you very much. I see now that I put the function call in before the print statement, I don't know why. I really appreciate it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Returning values from a function
    By stevenson in forum C Programming
    Replies: 14
    Last Post: 07-20-2011, 09:54 PM
  2. returning values from a function...
    By roaan in forum C Programming
    Replies: 1
    Last Post: 08-03-2009, 06:54 PM
  3. Returning Multiple Values from a Function
    By Programmer3922 in forum C Programming
    Replies: 4
    Last Post: 08-02-2008, 09:10 PM
  4. Returning Multiple Values from a function
    By Eddie K in forum C Programming
    Replies: 6
    Last Post: 03-12-2006, 01:18 PM
  5. Returning Multiple Values from a Function
    By Learner007 in forum C++ Programming
    Replies: 11
    Last Post: 07-21-2002, 11:03 AM