Results 1 to 6 of 6
  1. #1
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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());
    					}
    			}
    
    }
    why we have used return 0, can we use any other thing value with return like return 3.


    Thanks in advance
    Ankit
    Last edited by ankiit; 12-30-2011 at 05:18 AM.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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();
    This will print that a stack underflow has occurred, but will still be allowed to continue. Which can naturally be problematic. If you have some program with automates over a stack you can cause a problem. The underflow occurs but the automation won't be aware. Instead of printing the error message, I'd suggest you throw an exception.
    Last edited by sunde887; 12-30-2011 at 07:07 AM.

  3. #3
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default 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());
    					}*/
    			}
    
    }
    And the output is as below:-


    what does &lt;return 0&gt; signifies?-stack.png

    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

  4. #4
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default 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.

  5. #5
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: what does <return 0> signifies?

    Hi sunde887,

    The code while compiling throws an error, that the return statement is missing.

    Thanks
    Ankit

  6. #6
    ankiit is offline Member
    Join Date
    Dec 2011
    Location
    India
    Posts
    74
    Rep Power
    0

    Default Re: what does <return 0> signifies?

    Hi subnde887,

    can we initialize tos to some other value like 10?(10 is 1 above the maximum value set for the stack)?

Similar Threads

  1. Suggestions return all
    By mario_tim in forum New To Java
    Replies: 7
    Last Post: 04-01-2011, 05:32 AM
  2. return subtype
    By TheSheepeh in forum New To Java
    Replies: 3
    Last Post: 03-18-2011, 10:48 PM
  3. Using int/int, 7/5 would return 1
    By zoe in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 11:25 AM
  4. about 'return'.
    By helloworld in forum New To Java
    Replies: 9
    Last Post: 11-28-2008, 04:08 AM
  5. if..else..return
    By mqdias in forum New To Java
    Replies: 1
    Last Post: 08-10-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •