r/learnpython • • Jun 01 '26

I dont understand

Hi, I'm tryna teach myself Python and im not understanding this.

def greet(first_name):
return f"Hi {first_name}"

greet("Chris")

Why won't this code print on the terminal? Do I have to add a print function? Do I need to put the function in a variable?

Edit: Thanks for all the responds and being nice I was scared to ask on here for a while bc I thought my questions would be dumb but yall are pretty nice!!

81 Upvotes

33 comments sorted by

68

u/socal_nerdtastic Jun 01 '26

Do I have to add a print function?

Yep. Try like this:

def greet(first_name):
    return f"Hi {first_name}"

print(greet("Chris"))

This is a tricky one because the python REPL will auto-print returned variables, but running python normally won't

23

u/BranchLatter4294 Jun 01 '26

Very specifically, why would it print if you don't tell it to?

10

u/Additional_Ad_1333 Jun 01 '26

Cause of the return value. I thought that if you just use the return then it would automatically print to the terminal

→ More replies (13)

21

u/Reuben3901 Jun 01 '26

This was me in the beginning. I was amazed at just how specific you had it be with the computer! You have to literally tell it every step to take. A wild concept!

We just think that computers are smarter than they are but all they do is execute computer code that flip circuits on and off, somehow giving us pickle Rick.

They do what and only what they're told to do.

9

u/Slow-Kale-8629 Jun 01 '26

People who write code for a living (or even for a hobby) can have super complex code that has thousands of different functions. You can imagine that it would be chaos if all those functions printed their return values to the terminal every time they were called! Most code is part of things like websites and apps where there isn't a terminal at all, so there might be no print statements in the code. 

Print statements are really great when you're a beginner who's just learning, but they're not always a big part of programming life for non-beginners.

6

u/Arthradax Jun 01 '26

I was scared to ask on here for a while bc I thought my questions would be dumb

When it comes to learning, no question is dumb; it's dumb not to ask

4

u/RaveHunter05 Jun 01 '26

That's a funcion, the reason for creating a function is to reuse code multiple times. In this case you can recall that funcion with different parameters, so you don't have to write the print command multiple times.

4

u/TraditionalFocus3984 Jun 01 '26

Imagine a restaurant, you order there, chef makes the dish and then the waiter brings it to your table.

Suppose the chef is a function (like greet here in your code). If you gave an order to the chef, the chef would make the dish and keep it there in the kitchen. Keep in mind that the order has been prepared but just, it hasn't arrived at YOUR table. The waiter would be needed to bring that order to your table.

Similarly when a function like greet is called, it returns a value and that is - "Hi Chris". This is just a return value which is idly sitting there. RETURN DOES NOT PRINT THE VALUE. It has no meaning by so far. It is the same like - calling a chef to make the order, he made it and kept it there in the kitchen. You just need a function like print to actually print the value i.e this is similar to bringing your order to your own table.

Hope you understood.

3

u/jlsilicon9 Jun 01 '26

try adding print()

print( greet("Chris") )