1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

C++ questio about functions

Discussion in 'C++' started by dror, Dec 14, 2011.

  1. dror

    dror New Member

    Joined:
    Dec 14, 2011
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    hello i wrote this short program that need just to multyply between 2 numbers ,but doing so only by useing + (thats our assigment from class)
    the program do the work but it also add numbers like 449176 why does it do it ??,i feel that the answer will make me understand function real better ,any one know the answer??
    Code:
     
    #include <iostream.h>
    int kefel(int A,int B);
    int kefel(int A,int B)
    {
    int x,y,t=0;
        for (x=0;x<A;x++)
        {
            for (y=0;y<B;y++)
            {
               t=++t;
                }
                }
       cout<<t;
    }
      int main()          
    {
    int a=0,b=0,k=0;
    cin>>a>>b;
      cout<<kefel (a,b);
            cin>>k;
                return 0;
                      }
     
    Last edited by a moderator: Dec 14, 2011
  2. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    I don't understand the function enough to help you, but I can give you one little advice : instead of using "cin>>k" use "cout<<endl; system("PAUSE");" :)
     
  3. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Undefined behaviour
    Code:
    t=++t;
    
    If you want to increment t, use ONE of the following, and DO NOT try to mix them:
    Code:
    t++;
    ++t;
    t=t+1;
    
    By the way, your program might work, but it doesn't use +. It uses ++, which is not the same.
    You can do multiplication with repeated addition, e.g. 3*6=6+6+6 - that's 6 added to a zero-initialised accumulator 3 times.
     
  4. Chong

    Chong New Member

    Joined:
    May 15, 2011
    Messages:
    29
    Likes Received:
    7
    Trophy Points:
    0
    Hi your keffel() functions is supposed to return int. The program as it is won't compile.
    Bestt regards
    Chong
     
  5. Scripting

    Scripting John Hoder

    Joined:
    Jun 29, 2010
    Messages:
    421
    Likes Received:
    57
    Trophy Points:
    0
    Occupation:
    School for life
    Location:
    /root
    IMHO, it can be compiled without any difficulties ...
     
  6. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    Good point; I hadn't spotted that.

    kefel() doesn't actually return a value to the calling function (main). However it does display the value of t before it returns. So (if t=++t does what the OP thinks, i.e. t=t+1) it will display the correct value.

    What happens after the function returns, on the "cout<<kefel (a,b);" line, depends on the compiler's method of returning values. For Microsoft Visual Studio on Windows, it is the value in the EAX register. Since there is no "return" statement, the value "returned" will effectively be whatever junk was in EAX (however, MSVS would throw an error, preventing compilation, due to the lack of the return value). This could account for the "numbers like 449176" observed by the OP.

    If the OP is using a very old compiler then it is possible it does not enforce the "non-void function must return a value" rule.
     

Share This Page