Returning From A Function

When a function is called, controls are sent to the function. As function finishes its execution, controls are sent back to the caller code. When a function terminates after accomplishing some work, its controls with that work or some value are returned to the caller code. The return type specifies the type of value returned by the function. The return statement is useful in two ways: for immediate exit from a function and to return a value to the calling code.

A function may contain several return statements. However, only one of them gets executed. The return statement can be used even if the function does not return any value. Constructor is a function with the same name as that of the class name. It is invoked automatically as the object of the class is created. Constructor in Java does not have a return type. A class in Java can have many constructors but with different signatures.

To Access the full content, Please Purchase

  • Q1

    What will be the outcome of the below given function?
    int myFunct()

    {

    return 20;

    }

    Marks:1
    Answer:

    This function returns an integer value that is 20.

    View Answer
  • Q2

    Which keyword is used to return from a function?

    Marks:1
    Answer:

    Keyword “return” is used to return from a function.

    View Answer
  • Q3

    What should be the type of value following the return in return statement?

    Marks:1
    Answer:

    The value following the return should be of same type, i.e., the return type of the function in which it is given.

    View Answer
  • Q4

    From where does the execution of caller code resume after returning from a called function?

    Marks:1
    Answer:

    After returning from a called function, the execution of the caller code resumes from where it was stopped, i.e., from the statement next to the function call statement.

    View Answer
  • Q5

    What are the two purposes of return statement in a function?

    Marks:1
    Answer:

    1. Immediate exit from function
    2. Return some value to caller code

    View Answer