Python Functions

Hi all,

Can someone explain to me how to get the output of the code below? I tried print(update(20)) but was returned an error.

def update(new_value = 10):
old_value = new_value

update(20)

Thank you in advance!

Uhm, what output?
You’d need an output, some kind of result, a value

For example, if you wish to add two numbers:

1 + 1

Then that’s got a result, 2, and you can print it.

print(1 + 1)

So if you have some kind of result, then, where did you put it?

1 Like

Hi @ionatan!

Im slightly confused. So this is a function that stores the value of variable (new_value) as old_value.

update(20) will supposedly update new_value to 20 instead of its current value of 10 right?

So to verify that, I keyed in print(update(20)) but an error came up.

I’m very new to python and so I hope you can explain this part to me. Thank you and I appreciate your response a lot!

When I run your code, and print as you’ve described:

def update(new_value = 10):
  old_value = new_value

print(update(20))

I don’t get an error. I get the expected value None printed. Every function returns something. If we don’t tell the function what to return, it returns None.

Not exactly. 10 is a default value that will be assigned to new_value if you call the update function without supplying an argument. Since you supply 20 as the argument, 20 is assigned to new_value.

If we want a function to return something other than None, we have to tell it specifically what to return. How would you do that?