A challenge, just for fun:
Make a function that behaves exactly like a variable.
Printable View
A challenge, just for fun:
Make a function that behaves exactly like a variable.
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
This guy sounds like a troll to me...
I love a good challenge so here goes (if this is a trick question though, I'll feel really dumb :eek: ):Quote:
Originally Posted by MortalMonkey
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:
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.Code:int imaVariable(int x)
{
return x;
}
My second idea was to make a function that takes no arguments but asks for input and returns that input:
But again, the same problem... every time you call the function, you have to input the value.Code:int imaVariable(void)
{
int value;
cout<<"Enter value: "<<endl;
cin>>value;
return value;
}
My last idea was to simply assign a value to the function:
But this seems more like a constant than a variable to me so... I don't know...Code:int imaVariable(void)
{
return //value I want to assign the function goes here;
}
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.
[edit]
Bah, never mind :rolleyes:
You would almost have to use a global variable.
I cant think of anyother way.
I was originally going to do the following:
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.Code:namespace {int var;}
int imavariable()
{
return var;
}
void imavariable(int x)
{
var = x;
}
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;
}
Doh. I was, uh.. just about to post that.. yeah. :rolleyes: