function returning field

Hello, how do I return variables from function, that has arguments?

For example:


1
2
3
4
5
6
word f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];

return b[5]; //is this right?
}





Or should it look more like this?


1
2
3
4
5
6
word *f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];

return *b;
}





When I want to call function f, how can I do that?
Thanks,

ripley

Last edited on
a: ask questions, do not chop half the words out.
b: use the code tag instead of the quote tags.

I recommend read this
http://www.cplusplus.com/doc/tutorial/functions/

Aim for this:

Hello, how do I return variables from function, that has aruments?

For example:
1
2
3
4
5
6
word f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];

return b[5]; //is this right?
}



Or should it look more like this?
1
2
3
4
5
6
word *f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];

return *b;
}



When I want to call function f, how can I do that?
Thanks,
your name
Last edited on
is that better? sorry, i thought you can see my name above. my english is not so well, sory. i havent find in the link u send what i was looking for.

RIPLEY
The first is more correct, but it has a fencepost error (there is no element b[5] -- only b[0], b[1], b[2], b[3], and b[4]), and the use of the array is not necessary. It is the same as saying:
1
2
3
word f(word a[5]) {
  return -a[4];  // did you really want to return the negative of the last item?
}

Hope this helps.
ripley, your name is ripley.

http://www.cplusplus.com/doc/tutorial/functions2/
arrays are passed by reference, not by value. the function can directly change your arrays.

an easy way to get back b[] is to pass b[]:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
void f2(int b[5], int a[5]) {
	for(int i= 0; i<5; i++) b[i] = -a[i];
}

int main() {
	// start with empty arrays
	int x[5] = {0,1,2,3,4};
	int y[5] = {0,0,0,0,0};

	f2(y, x);
	// now
	// x[] = 0  1  2  3  4
	// y[] = 0 -1 -2 -3 -4

	return(0);
}

no no, i sugest i didnt write it correctly. but what i want to do:

i have some global fields, which has to be changed at some point, and thats why i wanted to use that function forexample f(), which is gonna make some operations with field and returns new field. than there is another function f2(), which take as an argument field - for first time field x, and second time changed field

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
word x[5] = {0,1,2,3,4};
word y[5] = {2,4,6,8,10};

word f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];

return // how do i return whole field?
}

word f2(word u, word s[5]) {
//some operations ....
}

int main() {


word f2(0x26ff, b);
word f2(0x26ff, f(b)); // this wasnt working for me

return 0;
}


the operations i used in f() i used just for ilustration. now i know, that first example i used above, where i was returning b[5] was wrong, because i was returning just one element.

i was looking at the link you gaved me, but i am not more clever from that how to return field.
i supose the best way is throug reference, but i have no idea how. thank you for you replies.


ripley, your name is ripley.

part of ripley's code:
1
2
3
4
5
6
word f(word a[5]) {
word b[5];
for(int i= 0; i<5; i++) b[i] = -a[i];

return // how do i return whole field?
}

b[] is a local variable in function f(). when f() returns, variables in f() may disappear! b[] could be gone! we will make b[] stay.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
word x[5] = {0,1,2,3,4};
word y[5] = {2,4,6,8,10};

word * f(word a[5]) { // returns location
	static word b[5]; // static makes variable permanent
	for(int i= 0; i<5; i++) b[i] = -a[i];

	return(b); // return array location
}

void f2(word u, word s[5]) { // nothing is returned
	//some operations ....
}

int main() {

	f2(0x26ff, x);
	f2(0x26ff, f(x)); // static location of b[] is sent to f2()

	// b[] can't be directly accessed outside of f(). use a pointer.
	word *ptr;     // a pointer
	ptr = f(x);    // f(x) returns location of b[]
	y[2] = ptr[2]; // y[2] = b[2];

	return 0;
}


b[] is not a new array. do you want to allocate the space for a new array?
Topic archived. No new replies allowed.