void?

This is a discussion on void? within the C++ Programming forums, part of the General Programming Boards category; A challenge, just for fun: Make a function that behaves exactly like a variable....

  1. #16
    Registered User
    Join Date
    Feb 2004
    Posts
    35

    Smile

    A challenge, just for fun:
    Make a function that behaves exactly like a variable.

  2. #17
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    Here is the simplest way I know to explain return.

    Code:
    // Function that wont return anything
    void Function ()
    {
    
         // Do stuff
    
    }
    
    // Function that would actually return a value.
    int ReturnMe ()
    {
    
        int number = 5;
     
        return number;
    
    }
    
    // now if I do this
    
    int value;
    value = ReturnMe ();
    
    // value now equals 5
    What is C++?

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    This guy sounds like a troll to me...
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User Elhaz's Avatar
    Join Date
    Sep 2004
    Posts
    32
    Quote Originally Posted by MortalMonkey
    A challenge, just for fun:
    Make a function that behaves exactly like a variable.
    I love a good challenge so here goes (if this is a trick question though, I'll feel really dumb ):

    A variable stores a value so all it seems we need is a function that mimics this, ie. returns an inputed value. My first idea was something very simple like this:

    Code:
    int imaVariable(int x)
    {
        return x;
    }
    But I can already see some problems with this too simple idea. A global variable (for example) would stay in scope for the whole duration of the program's running time. As long as you don't change it the value of the variable stays the same the whole time. This function, however, would have to be filled with a value every time you called it. Kinda defeats the purpose of using a variable to store stuff. We would need a function that would ask for a value once and then retain it so that when you call it again you don't have to pass it arguements.

    My second idea was to make a function that takes no arguments but asks for input and returns that input:

    Code:
    int imaVariable(void)
    {
        int value;
        cout<<"Enter value: "<<endl;
        cin>>value;
        return value;
    }
    But again, the same problem... every time you call the function, you have to input the value.

    My last idea was to simply assign a value to the function:

    Code:
    int imaVariable(void)
    {
        return //value I want to assign the function goes here;
    }
    But this seems more like a constant than a variable to me so... I don't know...


    Can such a function be created? At this point I don't know.

    I've seen some amazing stuff on this board already so I won't be surprised if someone more experienced here can do it, but at this point in my study I don't think I can.

    At any rate, thanks for the brain bender (well, brain bender for a noob like me). It's given me something to think about. Perhaps a function is like a temporary variable that does something.

  5. #20
    i dont know Vicious's Avatar
    Join Date
    May 2002
    Posts
    1,200
    [edit]

    Bah, never mind

    You would almost have to use a global variable.

    I cant think of anyother way.
    Last edited by Vicious; 09-13-2004 at 08:47 PM.
    What is C++?

  6. #21
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    I was originally going to do the following:
    Code:
    namespace {int var;}
    int imavariable()
    {
       return var;
    }
    void imavariable(int x)
    {
       var = x;
    }
    However, that doesn't work since it takes no consideration of scope - and besides which, you cannot take the address of the variable and you cannot use the assignment operator, or other such variable syntaxes.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  7. #22
    Registered User glUser3f's Avatar
    Join Date
    Aug 2003
    Posts
    345
    Code:
    #include <iostream>
    using namespace std;
                                                                                    
    int& Var(void) {
            static int var = 0;
            return var;
    }
                                                                                    
    int main(void) {
            cin >> Var();
            Var()++;
            cout << Var() << endl;
            return 0;
    }

  8. #23
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Doh. I was, uh.. just about to post that.. yeah.
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Another syntax error
    By caldeira in forum C Programming
    Replies: 31
    Last Post: 09-05-2008, 02:01 AM
  2. Replies: 3
    Last Post: 05-13-2007, 09:55 AM
  3. game window rejected painting !
    By black in forum Windows Programming
    Replies: 4
    Last Post: 03-27-2007, 02:10 AM
  4. msvc just ate one of my source files
    By Eber Kain in forum C++ Programming
    Replies: 6
    Last Post: 07-01-2004, 06:40 AM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM

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