Thread: just a random question

  1. #1
    Registered User
    Join Date
    Jan 2011
    Posts
    87

    just a random question

    hey, i was just think in and wondered how complex you can make a return command.

    i know that
    Code:
     return 0;
    works
    and
    Code:
     return x;
    works

    but how complex can you make it?
    will these work?
    Code:
    return increment(3);
    Code:
    return a_void_function();
    Code:
    #include "text.txt"
    
    char main()
    {
        return "text.txt"
    }
    what are other weird things you can do?

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    You can return any single value. The only thing you need to worry about is returning a reference to memory that will go out of scope when the function returns. e.g.:
    Code:
    char *my_func()
    {
      char foo[] = "bar";
      return foo;
    }
    It's still legal syntax, but it's a "bad idea".
    If you understand what you're doing, you're not learning anything.

  3. #3
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    You can return anything that matches the return type of a function. You can put a function call in there and it will return what the function returns.
    You can put an assignment there and it will return the assigned variable, eg

    return (x = 10);

    Basically, anything that is valid expression of a proper type can be put in there. You could say that anything you can assign to a variable can be returned.
    A function that returns void can never be returned since you cannot return void. And main must return int. Also note the difference between a character (put in single quotes), char, and a string, std::string (put in double quotes).
    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.

  4. #4
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    thanks

    also will something like this work?
    Code:
    #include <iostream>
    
    int main()
    {
    goto goto_1;
    goto_2:;
    return 0;
    }
    
    goto_1:;
    //some code
    goto goto_2;
    and yes, i know it is a bad idea :P

  5. #5
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    No, labels must be inside functions. Furthermore, goto cannot jump to a label inside a different function.
    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.

  6. #6
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    thanks for all the help
    one more thing, becuse you are on,
    what happens if you do
    Code:
    int function1()
    {
         return function2();
    }
    
    int function2()
    {
        return function1();
    }

  7. #7
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Infinite recursion.
    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.

  8. #8
    Registered User
    Join Date
    Jan 2011
    Posts
    87
    thought so
    thanks for all the help,
    now ill let this die

  9. #9
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Quote Originally Posted by itsme86 View Post
    You can return any single value. The only thing you need to worry about is returning a reference to memory that will go out of scope when the function returns. e.g.:
    Code:
    char *my_func()
    {
      char foo[] = "bar";
      return foo;
    }
    It's still legal syntax, but it's a "bad idea".
    Interestingly enough, you *can* do:

    Code:
    const char* my_func()
    {
      const char* foo = "bar";
      return foo;
    }
    because "bar" is static memory that will be available until your program exits and "const char* foo = "bar" simply gives foo the address of this memory. I'm not 100% sure this is required to work properly by the standard, but I'm pretty sure it is. Does someone else here know?

  10. #10
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Mozza314
    because "bar" is static memory that will be available until your program exits and "const char* foo = "bar" simply gives foo the address of this memory. I'm not 100% sure this is required to work properly by the standard, but I'm pretty sure it is. Does someone else here know?
    Yes, it is guaranteed:
    Quote Originally Posted by C++03 Clause 2.13.4 Paragraph 1 (part)
    An ordinary string literal has type “array of n const char” and static storage duration
    Quote Originally Posted by C++03 Clause 3.7.1 Paragraph 1
    All objects which neither have dynamic storage duration nor are local have static storage duration. The storage for these objects shall last for the duration of the program.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  11. #11
    C++ Junkie Mozza314's Avatar
    Join Date
    Jan 2011
    Location
    Australia
    Posts
    174
    Thanks laserlight, I must read the standard for myself some time.

  12. #12
    Veni Vidi Vice
    Join Date
    Aug 2001
    Posts
    343
    A function that returns void can never be returned since you cannot return void
    Code:
    void test()
    {
    	return ;
    }
    compiles fine...

    Is it possible to "return ;" in a function that return void???
    01000111011011110110111101100100 011101000110100001101001011011100110011101110011 01100100011011110110111001110100 01100011011011110110110101100101 01100101011000010111100101110011 0110100101101110 01101100011010010110011001100101
    Good things donīt come easy in life!!!

  13. #13
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    return; does not return void, though. It returns nothing.
    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.

  14. #14
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Quote Originally Posted by ripper079 View Post
    Code:
    void test()
    {
    	return ;
    }
    compiles fine...

    Is it possible to "return ;" in a function that return void???
    Yes, and it's oftentimes very useful:
    Code:
    void func()
    {
      if(!condition)
        return;
    
      // Several lines of code follow
    }
    If you couldn't return; then the several lines of code would have to be contained in the if(), which IMHO, makes the function harder to read.
    If you understand what you're doing, you're not learning anything.

  15. #15
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by itsme86 View Post
    If you couldn't return; then the several lines of code would have to be contained in the if(), which IMHO, makes the function harder to read.
    Heh. That reminds me of Microsoft:
    Code:
    if (SUCCEEDED(hr)
    {
    	//...
    	if (SUCCEEDED(hr)
    	{
    		//...
    		if (SUCCEEDED(hr)
    		{
    			//...
    			if (SUCCEEDED(hr)
    			{
    				//...
    				// and so on
    			}
    		}
    	}
    }
    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. random question program
    By rogster001 in forum C++ Programming
    Replies: 1
    Last Post: 10-08-2009, 02:29 AM
  2. Another brain block... Random Numbers
    By DanFraser in forum C# Programming
    Replies: 2
    Last Post: 01-23-2005, 05:51 PM
  3. How do I restart a random number sequence.
    By jeffski in forum C Programming
    Replies: 6
    Last Post: 05-29-2003, 02:40 PM
  4. Noob question for random number
    By SKINp in forum C++ Programming
    Replies: 6
    Last Post: 01-03-2003, 12:53 PM
  5. Very simple question, problem in my Code.
    By Vber in forum C Programming
    Replies: 7
    Last Post: 11-16-2002, 03:57 PM

Tags for this Thread