Why does it calculate well a variable but not the function that equals that variable?

https://www.codecademy.com/paths/computer-science/tracks/cspath-intro/modules/cspath-python-functions/projects/physics-class

Hello,

in exercise No. 6 it says ‘’ Test get_force (function) by calling it with the variables train_mass and train_acceleration . Save the result to a variable called train_force and print it out.’’

I didn’t know how to do it correctly so I checked for that part in the helping video, where it’s done this way:

train_force = get_force(train_mass, train_acceleration)
print(train_force)

However, if I do:

get_force(train_mass, train_acceleration)
print(get_force)

it gives me some unclear result. I’m aware that I’m mistaken somewhere but I don’t know where, could you please explain me?

Thank you.

1 Like

This will give us a stub that shows where the function is located in memory. That is why we need to assign the return value of the function call to another variable.

1 Like

Thanks for your answer, mtf.

Does this mean that if I want print(get_force) to work, I should’ve done return get_force after I created the function?

2 Likes

It won’t work because get_force is a function.

print (get_force(a,b))

where a and b are your variables. Just printing does not preserve the return value, though, hence the need to assign it.

train_force = get_force(..., ...)
2 Likes

Thank you.

Unfortunately, I’m still confused, I hope that’s not so unusual since I’m almost completely new and have recently started from 0.

I don’t understand why a function sometimes can be printed and sometimes not. Does it have to have parameters when being called? And why can’t it be called itself but have to be assigned to a variable, so that a variable would be called?

1 Like

We don’t print functions, but their call returns.

have to? No. Each function has a specific purpose. If we need to pass in data to the function, then parameters are how we receive that data from the caller. A general rule is to use as few globals as possible.

Again, this depends on the purpose of the function. Ideally our function should not have an effect on states outside of it. We pass in values, compute with those values and return the result. That return is assigned to a variable so we can use it further in the scope from which the call originated.

We are not calling the variable, but assigning the value that comes back from the function.

2 Likes

If you call a function with the parentheses (along with any arguments - if any - that are supposed to go inside of those parentheses)

get_force(train_mass, train_acceleration)

… then the function will run. If it is written to have a return value, it will return that value. But it is up to you to do something to capture that return value. That is frequently done using a variable, as in the video:

train_force = get_force(train_mass, train_acceleration)

… now the variable train_force will be bound to the value returned by your function, and can be printed.

print(train_force)

… or, you could go directly to print() like this:

print(get_force(train_mass, train_acceleration))

When the Python interpreter sees that, it runs the function inside of the print() parentheses, obtains a return value, and sends that return value to print() to print onscreen.

But, If you call a function like this:

print(get_force)

without any parentheses following the function name, then the function will not run, but what is printed out will simply be the function name, and the address where it resides in memory, like this:

<function get_force at 0x03CB56F0>
3 Likes

Thank you very much sir and kudos to you, very well explained and understandable for those who are still beginners, I enjoyed reading it.

1 Like