0

I am just learning programming, in python.. watching the "programming foundation:fundamental" course from linkedin. although in the beginning it seems easy regarding the function of return statement. But I do not understand the function/duty of return in this code. and the code seems really complex to me. can anyone explain please, what is happening here in the code.

What is the output of the following program?

def isEven(num):
  return num % 2 == 0

if isEven(3):
  print("3 is even")
else: 
  print("3 is not even") 
Dmytro
  • 5,068
  • 4
  • 39
  • 50
  • 2
    Why don't you run it and see? – drum Sep 15 '22 at 02:24
  • i ran it but, i did not get it. so, firstly, we define a function isEven, with a parameter called num. and then this function is supposed to give us/return us a value. what value? i guess, if it was return num%2. I would get it. it is returning the remainder of 3/2. but what it is returning here? return num % 2 == 0 – learner2022 Sep 15 '22 at 02:31
  • as far as i understood, return statement gives you back a value, just like when i give a input to a function. but in this case, is it doing something like that? or is it checking the value that i gave , when divided by 2, returns a 0 or not? another confusing thing is , if I am already defining that the function should have a value of 0? – learner2022 Sep 15 '22 at 02:37
  • Without the outer equality check, the return value is 1 if odd and 0 if even. With the outer equality check, the return value is `True` if even and `False` if odd. Also the code takes order of operations for granted, it is `((num % 2) == 0)`. – Dmytro Sep 15 '22 at 02:39

5 Answers5

0

If the remainder is 0 when dividing a number by 2, it means that it is an even number. The % operator returns the remainder of a division. For example, 2 % 2 would return 0, and 2 % 3 would return 1.

khreiv
  • 1
  • 2
0

First off, when working always pay close attention to indentation, i.e. the number of spaces of each line with respect to the line immediately above. In your case, the script can be divided into two logical parts:

def isEven(num):
  return num % 2 == 0

This function returns the value of the operation num % 2 == 0. The operation num % 2 evaluates the remainder of the division between num and 2. It is easy to see by plugging numbers that it will always give 0 for even num, and 1 for odd num. Then, since the operator == evaluates whether the left hand side is logically equal to the right hand side of it, the statement num % 2 == 0 will simply give True for even num and False for odd num.

if isEven(3):
  print("3 is even")
else: 
  print("3 is not even") 

First, it is important to note that, since isEven returns a value (a bool), then wherever you call that function, you can substitute the value returned of that function in order to "simplify" things. Then, the statement if isEven(3) can be rephrased as "is the value returned by isEven(3) true or false?" As you probably learned, if the statement if is evaluated to False and is accompanied by an else, then the script will valuate what is inside the else statement. In other words, this second piece of code will instruct the computer to print "3 is even" if 3 is even (i.e. if isEven(3) returns True, and "3 is not even" otherwise. Finally, since we know that 3 IS odd, 3%2 = 1, and thus the program will simply print "3 is not even".

Matthias
  • 12,873
  • 6
  • 42
  • 48
JustLearning
  • 1,435
  • 1
  • 1
  • 9
0

The "return" does exactly as its named, it returns a value.

in that specific return, "num % 2 == 0" returns the remainder of dividing the left hand operand by right hand operand.

"3 is not even" would be your output.

if isEven(3): - this line is calling the function and only runs when the "return" is true.

Tyler Bury
  • 16
  • 2
0

The return keyword specifies an output

When you go to an ATM, the return output of the ATM program is money
When you throw a dice, the return output is a number (1-6)
When you dial a number on your phone, the return output is a call to that number

Return is ultimately what you get from the program. Or what you get from the portion of the program, if defined inside of a function. A function (def) is like a micro-program, when linked together they make a program.

dyna-soar
  • 9
  • 2
-1

It should be written like this:

def isEven(num): 
    return num % 2 == 0

if isEven(3): 
    print("3 is even") 
else: 
    print("3 is not even")

num is a variable for the definition isEven

The if statement is calling the definition and passing the number 3 into the variable.

The return statement provides the result that is subsequently evaluated by the if statement.

If the return value is true, then the program will print "3 is even". However, if the return value is false, the program will print "3 is not even"

In this case, since three is not a multiple of 2, the definition will evaluate as false

Picture of printed results

Justin Edwards
  • 310
  • 1
  • 4
  • 7