maltoshi
maltoshi

Reputation: 87

What is the difference here?

I am learning python and here is a small code that instructions want me to write.

First, def a function called cube that takes an argument called number. Don't forget the parentheses and the colon! Make that function return the cube of that number (i.e. that number multiplied by itself and multiplied by itself once again). Define a second function called by_three that takes an argument called number.

if that number is divisible by 3, by_three should call cube(number) and return its result. Otherwise, by_three should return False.

What I did first was;

def cube(number): 
    return number ** 3

def by_three(number):
    if number % 3 == 0:
        cube(number)
        return number
    else:
        return False

but apparently in this, by_three(number) returns number itself. so to my understandings, the cube(Number) and return number doesnt work by line order?

this works:

def cube(number): 
    return number ** 3

def by_three(number):
    if number % 3 == 0:
        return cube(number)
    else:
        return False

I know why this works, but I dont get why the other one doesnt.

Upvotes: 0

Views: 138

Answers (5)

OLIVER.KOO
OLIVER.KOO

Reputation: 5993

What went wrong:

This is because you didn't store the result of cube(number) in any variable. and when the program proceeds to the next line return number it returns the number.

Example:

def example(number):
  number + 5
  return number

print(example(7))

the result is: 7

since I didn't do anything to store my result of number + 5 when I call example(5) I get the number itself back

but now consider:

def example(number):
  number = number + 5
  return number

print(example(7))

the result is: 12

This is because I updated number with the result of number + 5.

I hope this helps you get a better understanding of what was wrong with your code. Welcome to StackOverflow. If you find this answer of any other helpful and solves your problem please mark it as the solution. That way it will help the community and fellow programmers who run into the same question as you in the future. Cheers

Upvotes: 2

iceblueorbitz
iceblueorbitz

Reputation: 1060

Because you didn't assign the return of the cube() function to a variable. So cube(number) returns a number, then gets forgotten and you just return number which was originally passed into by_three(). If in your first one you did number = cube(number) it would reassign the cubed number to the number variable and it would work.

EDIT for additional info

A simple way to think about it is when passing immutable data types such as integers, floats or strings, after a function evaluates and returns data, that data essentially replaces your function call. So you can imagine:

def cube(number): 
    return number ** 3

def by_three(number):
    if number % 3 == 0:
        9
        return number
    else:
        return False

This helps illustrate why nothing happens since 9 just on it's own has no relationship to the VARIABLE number. Granted this isn't valid python code, but you can just imagine the process of what's happening behind the scenes here. So as I stated by reassigning the number, it would be doing this behind the scenes and reassigning number with the cube() returned value of 9:

def cube(number): 
    return number ** 3

def by_three(number):
    if number % 3 == 0:
        number = 9
        return number
    else:
        return False

Upvotes: 3

Christian
Christian

Reputation: 138

Explanation

The first one doesn't work because variables in functions are not global. What does this mean? Look at the following code:

def add(number):
    return number + 3

add(number)
print(number)

If you try this code, you will get NameError: name 'number' is not defined. This is because the variable number is a LOCAL variable. It stays inside the function, having fun, and then it dies after the function ends. This also means that if you switch by_three(number) with by_three(number2)...

def cube(number):
    return number ** 3

def by_three(number2):
    if number % 3 == 0:
        cube(number2)
        return number
    else:
        return False

It will show the error name 'number' is not defined.. When you use number instead, you are getting the output of the number you put in by_three(number), not cube(number). Thus, return cube(number) works because the number in cube(number) is different from the number in by_three(number).


Possible Approaches

If you still want to do a similar process like this, you can make a variable equal to the function cube(number). Example:

if number2 % 3 == 0:
    cubedNumber = cube(number2)
    return cubedNumber
else
    return False

Now by_three(3) shows 27 instead.


Assigning a Global Variable

A global variable is default when outside a function; as a local variable if otherwise. To make a variable global, simply put global variable_name. However, you can't just do global variable = "something"; you must make it a global variable before assigning it. Anyways, in this scenario, it's not recommended, and I don't even bother putting a couple lines of code just to make the function work, because it will be global for the rest of the code.

Upvotes: 1

bharadhwaj
bharadhwaj

Reputation: 2129

That's because you are not storing the result of cube anywhere and you are just returning the number itself.

I think what confused you is, inside cube() function you are returning number. Now you think, it is the same number variable inside the other function. But it is not. You need to reassign the value to some variable so as to use it.

...
cube_num = cube(number)
return cube_num

Now this would work. Hope this helps! :)

Upvotes: 1

Chris
Chris

Reputation: 22953

Python passes pass variables by assignment. That means when you pass an argument to a function, the parameter that corresponds to the argument is assigned a reference to the argument object.

In your code, when you pass number to the cube() function, the number parameter of cube() is assigned a reference to that object. However, you never do anything with that reference. You simply return a new object. This number is never changed. But even if you did attempt to change number inside of cube(), that would fail as well because Python would treat the assignment as a new declaration of a variable named number inside of cube().

This behavior can be demonstrated with a simple example:

>>> def func(n):
    print(id(n))
    print(id(n ** 3))
    n = 2
    print(id(n))


>>> def func2(n):
    print(id(n))
    func(n)


>>> func2(3)
1612339504
1612339504
1612339888
1612339488
>>>

The id() function returns a unique number for each object created in your program. As you can see above, the 3 passed into func2()is the same object passed into func(). However, when we use n in the expression n ** 3, a new object is created. The same thing applies to the assignment n = 2. When we do that, Python treats that as a variable declaration, and a new variable local to func() is created with a reference to the new object 2.

Upvotes: 1

Related Questions