Thread: Scope of a Function Return

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    Scope of a Function Return

    What is the Scope of a funtion return? In this example


    Code:
    int main()
     {
     	int a,b;
    	a=5;
    	a = foo(a);
    	printf("%d", a);
     }
     
     int foo(int x)
     {
     	x = x + 2;
    	return x;
     }

    It appears that if the function foo is passing by value but the purpose is to re value a in main. How is this possible if not by reference?

    Thanks in advance

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    The output there is 7, so I'm not sure what you are confused by.

    However, here's a pass by reference version:
    Code:
    #include <stdio.h>
    
     void foo(int *x)
     {
     	*x = *x + 2;
     }
    
    
    int main()
     {
     	int a,b;
    	a=5;
    	foo(&a);
    	printf("%d", a);
     }
    The output is also 7.

    You might want to read this, it's incomplete but covers what you are interested in here I think:

    http://206.251.36.107/programming/passbyvalue.mhtml
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    I believe he's confused because he thought the variable x, being local, would not actually return anything valid to main().

    Which it did, of course.

    Author Horton in "Beginning C Programming", described returning local variables this way:

    "The golden rule is to NEVER return the address of a local variable".

    As long as the local variable's *value* is caught immediately by another variable, you're OK. (as you have shown with x, being "caught" by a, in main() ).

    If you returned a pointer however, you would inevitably use that pointer (dereference it), later in the program, and then you'd be *toast*, since x would no longer exist.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    13

    I'm reading that very tutorial: Adak does indeed understand

    I was reading the great tutorial that MK27 recommended and got to thinking on the topic of passing variables and realized that I have not been exposed to any thinking in the way of the scope variables being passed from a function as a return. I realize now that the topic includes "returning" and this may make a difference. It appears that when returning a variable the rules are different. The copy/pointer thing is not working the same in this scenario. Would the correct topic be "Scope of Returns from Functions"?

    Thanks for the replies
    Last edited by theKbStockpiler; 06-16-2010 at 10:35 AM.

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The scope is the same. The function is making a copy of the variable and returning it. So basically its data is copied over, but the variable inside the function is destroyed.
    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. why can't my perceptron learn correctly?
    By yann in forum C Programming
    Replies: 25
    Last Post: 10-15-2010, 12:26 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM