Thread: Help understanding result of Function

  1. #1
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48

    Question Help understanding result of Function

    Hi,
    I hope someone can help me understand the result of this program.

    Code:
    int fun3(int x);
    int i = 10, j= -15, b;
    
    main()
    {
        auto int i = -20;
        int y = 7;
    
        y = fun3(i);
    
        getchar();
        return 0;
    
    }
    
    int fun3(int a)
    {
        int b = 1;
        ++j;
        ++i;
        printf("\n (4)In fun3: Value of a=%d b=%d, i=%d and j=%d", a, b, i, j);
        return ++a;
    }
    It prints out "(4)In fun3: Value of a=-20 b=1, i=11 and j=-14".
    I understand that because b was initialized as 1 within the function that it prints as 1 and that i and j are global variables that are incremented within the fuction and therefore have the values 11 and -14 respectively.
    On the other hand I can't seem to figure out how a gets the value of -20. Can someone point me in the right direction?

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you pass -20 as a parameter (using local var i)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Quote Originally Posted by vart View Post
    you pass -20 as a parameter (using local var i)
    I'm sorry I don't know what that means.

    Why is there a y in this program? It seems superfluous.
    In the main function i is -20 and then y = fun3(i) so I guess that the i variable is replaced with the -20 and the int a in the external function somehow becomes -20? And therefore a = -20?
    Last edited by deadhippo; 04-05-2008 at 01:52 AM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    -Wshadow
    Warn whenever a local variable shadows another local variable, parameter or global variable or whenever a built-in function is shadowed.

    You've got the same variable name, in two different scopes, and you're getting confused as to which one applies at any given point in the code.

    If you give everything a unique name, you'll be less confused.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Quote Originally Posted by Salem View Post
    You've got the same variable name, in two different scopes, and you're getting confused as to which one applies at any given point in the code.

    If you give everything a unique name, you'll be less confused.
    It's part of a program from my textbook. I think that it is deliberately confusing to make you understand the concept but I could be wrong.

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    A scope is defined by the brackets ({ and }). Each scope can have its own variables. Those variables are only accessible inside that scope. So variables placed outside any scope (global) are visible to the entire program.
    Variables defined inside a function is only visible inside that function. And variables defined inside a specific scope is only visible inside that scope.
    However, each scope can also define their own variables with the same name as variables in an outer scope. If this is the case, then the variable within the scope will hide the variable in the outer scope (that means the variable in the outer scope exists, but you can't access it because you have another variable with the same name).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Location
    Yokohama
    Posts
    48
    Thanks for your response Elysia. I think I understand about scope and that even though it is recommended to choose different variable names this program doesn't. I think what you are saying is that when variables are declared within a particular scope global variables with the same name are ignored within that scope. Except you put it in a more elegant and easy to understand way.

    I guess I am still missing something though as I really don't know how the -20 value is passed to the a.

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by deadhippo View Post
    Thanks for your response Elysia. I think I understand about scope and that even though it is recommended to choose different variable names this program doesn't. I think what you are saying is that when variables are declared within a particular scope global variables with the same name are ignored within that scope. Except you put it in a more elegant and easy to understand way.
    Yes, but it happens for every scope. You can define variables within if statements with the same name as other variables and it will hide the ones in the outer scope.

    I guess I am still missing something though as I really don't know how the -20 value is passed to the a.
    This one is really easy. "A" is an argument to your function and you pass the variable "i" which contains the value -20, so "a" is also guaranteed to get -20 because you are passing it as an argument.
    I think everyone already echoed this.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. need help understanding this function
    By Lince in forum C Programming
    Replies: 4
    Last Post: 08-04-2007, 10:29 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM