What is a value-returning method? How is it used?

Microsoft Visual C#
7th Edition
ISBN:9781337102100
Author:Joyce, Farrell.
Publisher:Joyce, Farrell.
Chapter8: Advanced Method Concepts
Section: Chapter Questions
Problem 11RQ
icon
Related questions
Question

What is a value-returning method? How is it used?

Expert Solution
Step 1

Returning a method value:

When a method completes all the statement(s) in the method, a return statement is reached or an exception is made, whichever happens first.

In its method statement you declare the return type of a method. The return statement is used for returning the value in the body of the process.

No interest is returned by any process deemed void. It does not have to contain a declaration of return, but does. In this case, a return statement may be used to branch out from and exit the process from a control flow block:

return;

You will receive a compiler error if you are attempting to return a value from a declared null path.

Any process that is not declared void must contain a return statement, like this:

return return value;

The return value data type must conform to the form declared for the method; from the declared method to return a boolean, you can't return the integer value.

Step 2

Uses of Return Value Method:

1.      You declare the return type of a method in its method declaration. For returning the value in the process, the return statement is used. No value is retrieved and no return statement is included in any declared vacuums system. Any non-declared method should contain a return statement.

public boolean isFree()

{

if (items.size() == 0)

       return true; 

   }

else

{   

     return false;

    }

}

steps

Step by step

Solved in 3 steps

Blurred answer