0 votes
in C Plus Plus by
Where you would use multiple return statements in a function.

1 Answer

0 votes
by

In a function that checks for certain conditions, multiple return statements can be used. For instance, consider a login validation function in Python. The function takes two parameters: username and password. It first checks if the username exists in the database. If it doesn’t, it returns an error message “Username not found”. This is the first return statement. If the username does exist, it then checks if the provided password matches the one stored in the database. If they don’t match, it returns another error message “Incorrect password”. This is the second return statement. If both conditions are met, it returns “Login successful”, which is the third return statement. Here’s a simplified code example:

def validate_login(username, password):
if not user_exists(username):
return "Username not found"
elif not correct_password(username, password):
return "Incorrect password"
else:
return "Login successful"
...