Function will not return value.

I have been looking at this and cannot figure out why my function will not return this int. It does make it into the if(... prints "test 32" but will not return the given value(123456789).

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
27
28
#include <iostream>

using namespace std;

int lastZero(int x[]);

int main(){
	int myArray[] = {1,1,1};
	lastZero(myArray);
	cout << endl << "test 2 " << endl;
}

 int lastZero(int x[]){
	//Effects: if x--null throw NullPointerException
	// else return the index of the LAST 0 in x.
	// Return -1 if 0 does not occur in x
	for(int i=0; i<sizeof(x); i++){
		if(x[i] == 0){
			
	cout << endl << "test 32 " << endl;
			return 123456789;
		}
	}
	
	cout << "test 3 " << endl;
	return -1;
}
Last edited on
It does return a value. You're just not storing it anywhere...
Wow I have been at this to long. Need a break. Thanks.
function is a group of statements that is given a name, and which can be called from some point of the program. The most common syntax to define a function is:

type name ( parameter1, parameter2, ...) { statements }

#include <iostream>
using namespace std;

int addition (int a, int b)
{
int r;
r=a+b;
return r;
}

int main ()
{
int z;
z = addition (5,3);
cout << "The result is " << z;
}

O/p: 8
I also doubt that that sizeof() call in your function is really doing what you think. It is not giving you the number of elements in your array, it is giving you the size of the pointer.


Topic archived. No new replies allowed.