Thread: functions return value

  1. #1
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214

    functions return value

    hi here is my code
    Code:
    #include<stdio.h>
    
    int calc(int a, int b);
    
    int main ()
    {
         int first, second, answer;
         printf("pick two numbers: \n");
         scanf("%d", &first);
         scanf("%d", &second);
         answer = calc(first, second);
         if (calc(first, second) == 1)
              return 1;
         else
              printf("the result is: %d", answer);
         return 0;
    }
    
    int calc(int a, int b)
    {
         if (b == 0)
         {
              printf("you cannot divide with zero");
              return 1;
         }
         else
              return (a/b);
    }
    when i type in i.e. 10 and 10 theres no output like
    "the result is blabla". why that ?
    and when i try to divide with zero the message
    "you cannot divide with zero" is displayed twice. i dont really know whats wrong with my code.

    i hope that someone can help me

    thanks threadhead

  2. #2
    anonyunregged
    Guest
    You call the function twice, that's what causes your problems. It's better not to combine the error and good return values, do something like this.
    Code:
    #include<stdio.h>
    
    int calc(int a, int b);
    
    int main ()
    {
        int first, second, answer;
        printf("pick two numbers: \n");
        scanf("%d %d", &first, &second);
        if (calc(first, second, &answer))
            return 1;
        else
            printf("the result is: %d", answer);
        return 0;
    }
    
    int calc(int a, int b, int *c)
    {
        if (b == 0)
        {
            printf("you cannot divide with zero");
            return 1;
        }
        *c = a/b;
    
        return 0;
    }

  3. #3
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    A few things:

    Division doesn't always result in an integer -- you might want to use floats or cast to a float and return a float as an answer, otherwise all of your division will result in whole numbers.

    The reason you get the double output is because you did this:

    answer = calc(first, second);
    if (calc(first, second) == 1)


    You called the calc function twice -- once prior to the if statement and once in the condition.

    The other problem is the fact that you return 1 if the user tries to divide by 0. See the problem? What's 10 divided by 10? What's 4 divided by 4? What's any number divided by a number of the same value? See my point. You can't use 1 as an error value because division can result in 1 is many situations. You should either pass the address of a bool value (or return a bool value and pass the address of a float to store the answer) to store whether or not the operation was successful (IE no division by 0), or throw an exception.

    On a side note -- it's not always a good idea to display error information from within the function. You're best off allowing that to be dealt with on a case by case basis.

  4. #4
    template<typename T> threahdead's Avatar
    Join Date
    Sep 2002
    Posts
    214
    with that:
    Code:
    if (b == 0)
         {
              printf("you cannot divide with zero");
              return 1;
         }
    i wanted to exit the function with return 1, what
    means that there was an error during the function.
    i thought 0 is going to tell the program that it can exit
    and with 1 it can exit but knows that there was an error.
    with
    Code:
    if (calc(first, second) == 1)
              return 1;
    i wanted to check if the function calc exited normal or
    with an error value like 1.
    if so, main should exit with returning 1 to itself.

    thanks

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. is it ok like that?
    By ExDHaos in forum C++ Programming
    Replies: 8
    Last Post: 05-23-2009, 09:02 AM
  2. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  3. string class errors
    By CodeMonkey in forum C++ Programming
    Replies: 13
    Last Post: 07-20-2003, 11:20 PM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. oh me oh my hash maps up the wazoo
    By DarkDays in forum C++ Programming
    Replies: 5
    Last Post: 11-30-2001, 12:54 PM