Results 1 to 6 of 6

Thread: How to return value for void method?

  1. #1
    Join Date
    Dec 2009
    Posts
    10

    How to return value for void method?

    I am beginner level programmer in the programming world. This days i am learning the programming language: C++, C sharp, Core Java, DBMS and PHP. I want to know how to return value of the void method? Actually i want know, because i am making a mini project and in that i have to use this method of returning the value. So, please somebody help me out.

  2. #2
    Join Date
    Apr 2008
    Posts
    1,948

    Re: How to return value for void method?

    According to me, the method you want to return it values by return keyboard should have declared the method by using void. And it has very simple ay to return the value, example:
    public void demochk()
    {
    // code for the method
    return; // return keyword will give the value of method
    }
    Hope you would be able to understand the use of return and void keyword. And your problem get fix.

  3. #3
    Join Date
    May 2008
    Posts
    2,012

    Re: How to return value for void method?

    I am also the new to the programming world and tell my knowledge and studies says that we can't return any value from those methods which are declared as void type. Because at the time of declaration you are showing or saying that this method will not return any value. Then how can you use return statement for void method? Please help me also in this matter. I am totally confused.

  4. #4
    Join Date
    Apr 2008
    Posts
    2,005

    Re: How to return value for void method?

    My answer would be Yes, you can use the return keyword for returning the value of the void method. According to me, we can use return in a void method for returning anything. Some of the programmer will always use the explicitly end of the void methods by using return statement, but it is not mandatory. It can be used to leave a function early also. Example:
    void DemoFunc(int num)
    {
    if (num == 0)
    {
    //Leave because it is not divisible value
    return;
    }
    //else, do something
    }
    Hope your problem will be solved.

  5. #5
    Join Date
    May 2008
    Posts
    2,389

    Re: How to return value for void method?

    I think any new programmers will be confused with this void method declaration. At first, i was also confused same way, but then came to know that there is exception in this case, if you to exit in between of any loop or procedure then you use the return statement for void method. We know that a method declared as void does not have to return a value, but it may do so. In case like this, a return statement can be used to branch out of a control flow block and exit the method.

  6. #6
    Join Date
    Nov 2005
    Posts
    1,323

    Re: How to return value for void method?

    According to me, the void is a data type and usually sued for returning the null value. And it also used if the the function has a return type and you do not what to return anything. If the function is defined to return void then just use "return". A void return type indicates that a method does not return a value. Hope your problem will be solved soon.
    Last edited by absolute55; 21-01-2010 at 11:45 AM.

Similar Threads

  1. Urgent Help with return method toString() Java!!
    By vizu in forum Software Development
    Replies: 3
    Last Post: 01-05-2011, 11:48 PM
  2. what are the possible return types of a main method?
    By \"Dritan\" in forum Software Development
    Replies: 3
    Last Post: 08-01-2011, 03:50 AM
  3. Method with multiple return types
    By ISAIAH in forum Software Development
    Replies: 5
    Last Post: 25-02-2010, 05:10 AM
  4. Method to return number of line
    By Vodka in forum Software Development
    Replies: 5
    Last Post: 13-01-2010, 02:48 PM
  5. Return a strongly typed array from the ToArray method
    By Eleazar in forum Software Development
    Replies: 2
    Last Post: 06-05-2009, 01:32 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,984,695.98100 seconds with 17 queries