Thread: Calculation-void

  1. #1
    Registered User
    Join Date
    Nov 2010
    Posts
    29

    Question Calculation-void

    Code:
    #include<stdio.h>
    int confusion(int x,int y)
    {
        x=2*x+y;
        return x;
    }
     int main()
     {
         int x=2,y=5;
         y=confusion(y,x);
         x=confusion(y,x);
         printf("%d %d\n",x,y);
         
         system("pause");
         return 0;
     }
    how is the ans calculated actlly?

  2. #2
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    When you call the function you pass in 2 variables. There is a computation and x is returned. You are setting y and x to the same value in main though...

    If you don't understand this, try to Google Functions in C, and also check out the functions tutorial here on this site. Also, it may help to Google about scope and automatic variables.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by \007 View Post
    You are setting y and x to the same value in main though...
    Are you sure?

    Also: note that just because you have x and y in the function and x and y in main, don't think that they have to match.

  4. #4
    Registered User \007's Avatar
    Join Date
    Dec 2010
    Posts
    179
    Oops, I hadn't read it clearly enough. For some reason I had thought they were entering the same x/y and didn't know they were overwriting one with a new value before calling the function again on the next variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My code wont link with gcc but does with Visual Studio
    By moniker in forum C Programming
    Replies: 2
    Last Post: 12-19-2009, 05:08 AM
  2. Replies: 3
    Last Post: 05-13-2007, 08:55 AM
  3. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 05:40 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. Replies: 2
    Last Post: 05-10-2002, 04:16 PM