Why does a function have to end the moment a return keyword is reached? Especially if you have a counter variable, don't you need to use the return to change the counter variable? Why don't you need a return keyword to constantly let each portion of your function return what you want it to to the main defined function. In the above example, don't each if and elif and else statement have to return a boolean to the defined is_prime function which the is_prime function that evaluates and returns to you main code body?

What am I doing wrong here?

because by default a function returns None at the end of a function, if we want to return something different we can use the return keyword to return something different at the end of the function

No, we can just update the counter variable (like any other variable), why would return needed for this?

this sentence is not correct, what do you mean? Can you clarifty your question with code?


Because I am an elementary coder I don't think I can clarify my question with code. However, I will try to reword it in English. It was my understanding that "return" returns a value to whatever it is nested in. In this case the if, elif, and else statements are nested within the is_prime function. Therefore, shouldn't the if elif and else statements have to return what they evaluate to to the is_prime function? And then, doesn't the is_prime function evaluate the booleans it received from each and every one of the if and elif and else statments, and then, using a series of logical comparators such as "or" return its evaluation to the main body of code, resulting in only one value when the function is called.


As to the counter variable concept, I not entirely sure what I was thinking, but of course we can update counter variables in functions. Sorry about that.


yes, so far so good

unclear what it is, a return keyword is always nested inside a function.

no? We can return whatever we like, we are designing the is_prime function, so we can return what we like. In this case True if x is a prime number, else we want to return False (x is not a prime number)

what does that mean? There are two possible boolean values: True or False

we can just call the function with any number we like so our function can tell us of the number is a prime number or not


Alright! Take 3. We got off in communication after "to whatever it is nested in". I was referring to the fact that the if/elif/else statements are nested inside the function is_prime, and the function is_prime is nested inside of the main body. What I was asking is: Doesn't the first if statement return either True or False to the is_prime function, as does the other if statement, the elif statement, the else statement? Therefore is_prime looks at False, True, False, True (or some variation thereof) and says, because not all of the if statements, elif statements, and else statements return True to me, I need to return False to the main body of code? If I'm still not communicating well, I can just take your word for it that a function stops after a return keyword is met and call it a day. I had a much different approach to this assignment, and it went fine, so I guess this whole back-and-forth is immaterial.


no, the if condition is evaluated, for x=3 we get:

if 3 <=0:

the condition is evaluated to false, so the code inside the if clause doesn't get executed, so the code moves on to the next if condition

code in if/elif/else clause only runs when condition evaluates to true, so if there is a return keyword in a clause that doesn't get executed, there is no problem, the code just move on to the next condition

i said: a function ends the moment a return keyword is reached, reached is very important here


This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.