-1

Is it possible to return a variable, from a function, without passing it through as an argument? For example:

int example(int x, int y) {
    int z = x + y;
    return z
}

Also, how would I use this when I get back to my main function?

mikey5527
  • 59
  • 7

4 Answers4

2

This is pretty much basic for a language like C++, consider helping yourself out by picking up a good book:
The Definitive C++ Book Guide and List


Is it possible to return a variable, from a function, without passing it through as an argument?

It is perfectly valid. Why do you think it is not?
It is legal to return a variable local to function by value. A copy of the variable is returned to the caller.
What you shouldn't do is returning address of a local variable because a local variable lives only within the scope in which it is defined.

What should be avoided?

int* example(int x, int y) 
{
    int z = x + y;
    return &z;
}

How would I use this when I get back to my main function?

int main()
{
    std::cout<<example(4,5);
    return 0;
}
Community
  • 1
  • 1
Alok Save
  • 202,538
  • 53
  • 430
  • 533
2

It's legal.

the content of the variable z is copied.

int iElsewhere = 0;
iElsewhere = example(1, 2); // now iElsewhere == 3

As stated by Kamil Klimek 's comments, you should start to read a C++ little tutorial or introduction, because this question is really really fundamental basic C/C++.

Stephane Rolland
  • 38,876
  • 35
  • 121
  • 169
0

You can return a non argument variable.. Below given code is fine.

int example(int x, int y) {
    int z = x + y;
    return z;
}

Since in this case, the value you are returning is of primitive type, like int. But there are complex cases, when you return address of a variable, and it is already explained in a genius way.

Now to use that variable in main

int main(){
  int ret = example(4,5);

  //do whatever with ret..
}
Community
  • 1
  • 1
Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
0

Your example is perfectly fine. You're returning a discrete value which is allowed. Under the hood what is happening is you're able to return a value in one of the chip's registers from a function. Just for clarity and not to be patronising a register is typically a 32/64 bit value depending on the OS.

When dealing with core types like int, bools, floats these are stored in registers so you can return this without any worries.

The other type of info you can return is a pointer of an address to some memory. When you do that the return value is again a 32/64bit pointer that is just the address for the data.

What you do need to be careful when doing is ensuring any allocated memory done in a function is done via a malloc or new (when using C or C++ respectively) and not just a declared structure in the method.

E.g. something like this is bad and will cause issues as the stack gets overwritten (unless that's your intention :) ).

void* myfunction() { MYSTRUCT muylocalstructure; void* mypointer = &mylocalstructure; return mypointer; }

sradforth
  • 2,176
  • 2
  • 23
  • 37