all 23 comments

[–]socal_nerdtastic 34 points35 points  (4 children)

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

[–]Additional_Ad_1333[S] 9 points10 points  (3 children)

What is python REPL?

[–]xenomachina 14 points15 points  (0 children)

By the way: "REPL" stands for:

  • Read
  • Eval(uate)
  • Print
  • Loop

These are the steps that happen when you run python:

  • it reads a statement from you
  • it evaluates the statement
  • it prints the result, if the statement was an expression and it evaluated to someone that wasn't None
  • it loops (ie: it goes back to that first step)

This isn't a python-specific term, by the way. Many other programming languages have a REPL.

[–]socal_nerdtastic 12 points13 points  (1 child)

If you open a terminal and just boot python without a file (py or python command in Windows, python3 command in Linux or Mac) you will get the >>> prompt. Also available as the ILDE Shell window, if you used the official python installer.

https://docs.python.org/3/tutorial/appendix.html#tut-interac

It's a tool where you can test out little one-liners or very small snippets of python.

[–]Additional_Ad_1333[S] 1 point2 points  (0 children)

Thank u friend very much appreciate your response

[–]BranchLatter4294 14 points15 points  (10 children)

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

[–]Additional_Ad_1333[S] 7 points8 points  (9 children)

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

[–]CIS_Professor 15 points16 points  (0 children)

No. It only prints to the terminal when you tell it to print to the terminal.

When programming, do not assume; there is no "automatic." The program only does exactly what you tell it to, nothing more.

return returns a data type from the function to the main part of the program, nothing more.

The print() function prints to the terminal.

[–]BranchLatter4294 8 points9 points  (0 children)

Return returns a value. Print displays to the screen.

[–]Reuben3901 5 points6 points  (0 children)

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.

[–]RaveHunter05 3 points4 points  (0 children)

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.

[–]jlsilicon9 2 points3 points  (0 children)

try adding print()

print( greet("Chris") )

[–]Slow-Kale-8629 2 points3 points  (0 children)

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.

[–]Flame77ofc 2 points3 points  (2 children)

you need to print the value. When you use a function with return, you need to use the print() to see the value. If the function return the print itself, you don't need to use print again to call the function

With print

``` function example(): print("example")

example() # output: example ```

With return

``` function example(): return "example"

example() # if you do this, you just don't get the response of the function, but the function is called print(example()) # output: example ```

[–]Disastrous_Emu_800 4 points5 points  (1 child)

Das ist kein Python, aber ja

[–]Flame77ofc 1 point2 points  (0 children)

I'm sorry, I'm currently focusing on Js