Initialising variable with return value from a function

Hi!
I'm wondering if it's possible to initialise a variable with a return value from a function like this:

int function()
{............. return a;}

int variable = function();

or

int function()
{............. return 5;}

int variable = function();

?
Thanks!
Last edited on
Yes you can do all those above examples you posted.
Yeah, it's fine to do that.

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


int main()
{
	int add = func();
	//the variable 'add' now equals 8

	return 0;
}


int func()
{
	int a = 5;
	int b = 3;

	int x = a + b;

	return x;
}
Not to nitpick but to save the cost of a temporary stack variable you can just do a

 
return a+b;
Yeah, I thought that as soon as I posted it, but left it there, lol.
Topic archived. No new replies allowed.