Like Tree1Likes
  • 1 Post By Ewiv

How this c++ code work

This is a discussion on How this c++ code work within the C++ Programming forums, part of the General Programming Boards category; First of all merry xmas guys.... i have this c++ code but i cannot understand why this output is coming ...

  1. #1
    Registered User
    Join Date
    Oct 2013
    Location
    India
    Posts
    30

    Question How this c++ code work

    First of all merry xmas guys....
    i have this c++ code but i cannot understand why this output is coming plese help......

    Code:
    #include<iostream>
    using namespace std;
    
    
    int func(int&x,int y=10)
    {
        if(x%y==0)
            return ++x;
        else
            return y--;
    }
    
    
    
    
    int main()
    {
        int p=20,q=23;
        q=func(p,q);
        cout<<p<<" "<<q<<"\n";        //here the value of q would have become 22 but its still 23 why...
        p=func(q);
            cout<<p<<" "<<q<<"\n";
    q=func(p);
        cout<<p<<" "<<q<<"\n";
    return 0;
    
    
    }
    MY OUTPUTS ARE

    20 23
    10 23
    11 11
    Last edited by Rohit Kumar; 12-25-2013 at 02:28 AM. Reason: providing output

  2. #2
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Code:
    return y--;
    This line is why. Can you figure out why it is?
    stahta01 likes this.

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    2,553
    Quote Originally Posted by Ewiv View Post
    Code:
    return y--;
    This line is why. Can you figure out why it is?
    I was thinking this line is why?

    Code:
    int func(int&x,int y=10)
    Edit: But, I think you are correct and its your line posted.
    After re-reading the code, I saw the issues you hinted at.
    "Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to produce bigger and better idiots. So far, the Universe is winning." Rick Cook

  4. #4
    Registered User
    Join Date
    Jun 2013
    Posts
    56
    Can't say for sure if the int func line is not intended to be that way, so I chose to ignore it. The return line however is obviously wrong.

  5. #5
    Registered User
    Join Date
    Mar 2012
    Posts
    110
    It looks like a school example. I bet your teacher would be thrilled if you'd ask. (I used to work as a teacher)

  6. #6
    Registered User cstryx's Avatar
    Join Date
    Jan 2013
    Location
    Canada
    Posts
    52
    This question's answer is as trivial as understanding (?)-- vs --(?). Combined with the fact that it's being passed by value:
    Code:
    int y=10
    If it worked as you expected, you would've had:
    Code:
    return y-1;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Cant seem to get this code to work.....
    By Alex Coven in forum C Programming
    Replies: 2
    Last Post: 09-10-2013, 06:57 PM
  2. How do I work out the AWT and TAT in my code ?
    By spendotw in forum C Programming
    Replies: 0
    Last Post: 01-21-2012, 06:51 AM
  3. Why does this code work?
    By Whitewings in forum C Programming
    Replies: 8
    Last Post: 08-16-2011, 12:58 PM
  4. this code won't work
    By med linux in forum C Programming
    Replies: 7
    Last Post: 03-23-2011, 08:35 AM
  5. cant get code to work
    By duffy in forum C Programming
    Replies: 13
    Last Post: 10-20-2002, 05:23 AM

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