DEV Community

Susmita Dey
Susmita Dey

Posted on • Edited on

1

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

Image of Timescale

🚀 pgai Vectorizer: SQLAlchemy and LiteLLM Make Vector Search Simple

We built pgai Vectorizer to simplify embedding management for AI applications—without needing a separate database or complex infrastructure. Since launch, developers have created over 3,000 vectorizers on Timescale Cloud, with many more self-hosted.

Read more

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more

Retry later