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

AWS GenAI LIVE image

Real challenges. Real solutions. Real talk.

From technical discussions to philosophical debates, AWS and AWS Partners examine the impact and evolution of gen AI.

Learn more

Top comments (0)

Postmark Image

Speedy emails, satisfied customers

Are delayed transactional emails costing you user satisfaction? Postmark delivers your emails almost instantly, keeping your customers happy and connected.

Sign up

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay