Results 1 to 6 of 6
Thread: what does <return 0> signifies?
- 12-30-2011, 06:15 AM #1
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
what does <return 0> signifies?
Hi Mentors,
I wrote a code to implement a small stack about 10 elements, though i read it from a book. I have some issues in understanding the one.
The code is as follows:-
Java Code:class Stack { int stck[] = new int[10]; int tos; //Constructor Stack() { tos = -1; } //push routine void push(int item) { if(tos >= 9) { System.out.println("Stack overflow"); } else stck[++tos] = item; } //pop rotuine int pop() { if(tos < 0) { System.out.println("Stack underflow"); return 0; // why return 0?? } else return stck[tos--]; } } class Demo { public static void main(String[] args) { Stack mystack1 = new Stack(); Stack mystack2= new Stack(); //push items on to the stack for(int i=0; i<10; i++) { mystack1.push(i); } for(int j=0; j<10; j++) { mystack2.push(j); } // pop items from stack for(int i=0; i < 10; i++) { System.out.println(mystack1.pop()); } System.out.println("new line"); for(int j=0; j < 10; j++) { System.out.println(mystack2.pop()); } } }
Thanks in advance
AnkitLast edited by ankiit; 12-30-2011 at 06:18 AM.
- 12-30-2011, 08:04 AM #2
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Re: what does <return 0> signifies?
You can use any other value. I don't have all the details of why 0 is used, however; I believe it has to do with being used as a flag indicating failure (some languages -- like c++ use 0 for false, and >0 for true). In the case you have used it you will run into problems. If you have 0 items in a stack and someone attempts to pop the value it should cause an error, since an empty stack can't return anything -- it's empty. Currently it will return 0 when it is empty.
Java Code:Stack st - new Stack(); int element = st.pop();
Last edited by sunde887; 12-30-2011 at 08:07 AM.
- 12-30-2011, 08:41 AM #3
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Re: what does <return 0> signifies?
Hi sunde887,
I have made some changes to the code, incremented the loop variable from 10 to 11.
Please find is the attached code and the output.
Java Code:class Stack { int stck[] = new int[10]; int tos; //Constructor Stack() { tos = -1; } //push routine void push(int item) { if(tos >= 9) { System.out.println("Stack overflow,can't push item onto the stack"); } else stck[++tos] = item; } //pop rotuine int pop() { if(tos < 0) { System.out.println("Stack underflow,can't pop items from stack it's empty"); return 3; } else return stck[tos--]; } } class Demo { public static void main(String[] args) { Stack mystack1 = new Stack(); Stack mystack2= new Stack(); //push items on to the stack for(int i=0; i<11; i++) { mystack1.push(i); } /*for(int j=0; j<11; j++) { mystack2.push(j); } */ // pop items from stack //System.out.println("Pop,Stack1"); for(int i=0; i < 11; i++) { System.out.println(mystack1.pop()); } /* //System.out.println("Pop,Stack2"); for(int j=0; j < 11; j++) { System.out.println(mystack2.pop()); }*/ } }
Now we can see the return value as 3 in the last line as the output as we have used return 3, or for any matter it shows the value we are using with return.
Still I am unable to understand the purpose of return.
Thanks in advance.
Ankit
- 12-30-2011, 10:35 AM #4
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,068
- Blog Entries
- 3
- Rep Power
- 15
Re: what does <return 0> signifies?
pop is defied to return an int, so it must return one. Try leaving out the return statement and see if it changes.
- 12-30-2011, 11:12 AM #5
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Re: what does <return 0> signifies?
Hi sunde887,
The code while compiling throws an error, that the return statement is missing.
Thanks
Ankit
- 12-30-2011, 11:57 AM #6
Member
- Join Date
- Dec 2011
- Location
- India
- Posts
- 74
- Rep Power
- 0
Similar Threads
-
Suggestions return all
By mario_tim in forum New To JavaReplies: 7Last Post: 04-01-2011, 06:32 AM -
return subtype
By TheSheepeh in forum New To JavaReplies: 3Last Post: 03-18-2011, 11:48 PM -
Using int/int, 7/5 would return 1
By zoe in forum New To JavaReplies: 2Last Post: 12-02-2008, 12:25 PM -
about 'return'.
By helloworld in forum New To JavaReplies: 9Last Post: 11-28-2008, 05:08 AM -
if..else..return
By mqdias in forum New To JavaReplies: 1Last Post: 08-10-2007, 05:20 PM
Bookmarks