DEV Community

Susmita Dey
Susmita Dey

Posted on • Updated on

Return Statement in Programming Languages

  1. Return Statement:- It is used to return any value from a function/method when the method is being called in the main() in any programming language.

  2. Return statements are mostly used in recursion programs.

  3. Example to demonstrate Return Statement in Java:-
    Here, after the function call the method returns the sum of the two variables and doesn't execute the last one i.e., the print statement and it remains unreachable and throws an error as the method ends after the return statement.

public class Sum {
    public static void main(String args[]){
        int a = 10;
        int b = 20;
        int ans = sum(a,b);
        System.out.println("Answer = " + ans);
    }

    public static int sum(int a, int b) {
        int sum = a + b;
        return sum; // Function ends

        System.out.println("Unreachable statement"); // This will give an error.
    }
}
Enter fullscreen mode Exit fullscreen mode

Thus after the return statement, if there's any more lines of code then those will not be executed and will give an error and the method will stop it's execution.


Hope this helps you. Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my YouTube Channel and connect on LinkedIn or Twitter.
Also, feel free to support my work.😊

Top comments (0)