Skip to content

Commit edb4756

Browse files
committed
titles to using-functions
1 parent 332bbcc commit edb4756

File tree

2 files changed

+53
-37
lines changed

2 files changed

+53
-37
lines changed

lists-and-tuples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ None
145145
```
146146

147147
This is the same thing that happened way back when [we assigned
148-
print's return value to a variable](using-functions.md).
148+
print's return value to a variable](using-functions.md#return-values).
149149

150150
## What is what?
151151

using-functions.md

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,31 @@ Hello World!
2929

3030
But what exactly is print?
3131

32+
## What are functions?
33+
34+
Let's see what happens if we type `print` without the `('Hello')` part.
35+
3236
```py
3337
>>> print
3438
<built-in function print>
3539
>>>
3640
```
3741

38-
In Python 3, print is a function. Functions do something when they are
42+
We could also type `print(print)`, it would do the same thing. Python
43+
replied to us with some information about print wrapped in `<>`.
44+
45+
As we can see, print is a function. Functions do something when they are
3946
**called** by typing their name and parentheses. Inside the
4047
parentheses, we can pass some arguments too. In `print("hello")` the
4148
function is `print` and we give it one argument, which is `"hello"`.
4249

43-
Functions are sometimes thoght of as difficult to understand, but they
44-
really are not. They just do something when they are called. But if we
45-
do `x = print('hello')`, what is x?
50+
Functions are easy to understand, They simply **do something when they
51+
are called**. Functions run immediately when we call them, so the
52+
text appears on the screen right away when we run `print(something)`.
53+
54+
## Return values
55+
56+
If we do `x = print('hello')`, what is x?
4657

4758
```py
4859
>>> x = print('hello')
@@ -58,8 +69,7 @@ explained in more detail:
5869
- In `x = print('hello')`, the right side is processed first.
5970
- `print('hello')` calls the print function with the argument
6071
`'hello'`.
61-
- The function runs **immediately** when it's called. It shows the word
62-
hello.
72+
- The function runs. It shows the word hello.
6373
- The print function **returns** None. All functions need to return
6474
something, and print returns None because there's no need to return
6575
anything else.
@@ -68,17 +78,46 @@ explained in more detail:
6878
there, and the assignment now looks like `x = None`.
6979
- x is now None.
7080

81+
Now we understand what a return value is. When we call the function,
82+
Python "replaces" `function(args)` with whatever the function returns.
83+
7184
Calling a function without assigning the return value to anything (e.g.
7285
`print('hello')` instead of `x = print('hello')`) simply throws away
73-
the return value. The interactive `>>>` prompt also echoes the return
74-
value back if it's not None.
86+
the return value. The interactive `>>>` prompt doesn't echo the return
87+
value back because it's None.
7588

7689
Of course, `x = print('hello')` is useless compared to `print('hello')`
7790
because the print function always returns None and we can do `x = None`
7891
without any printing.
7992

93+
Not all functions return None. The input function can be used for
94+
getting a string from the user.
95+
96+
```py
97+
>>> x = input("Enter something:")
98+
Enter something:hello
99+
>>> x
100+
'hello'
101+
>>>
102+
```
103+
104+
`input("Enter something:")` showed the text `Enter something:` on the
105+
screen and waited for me to type something. I typed hello and pressed
106+
Enter. Then input returned the hello I typed as a string and it was
107+
assigned to x.
108+
109+
Usually we want to add a space after the `:`, like this:
110+
111+
```py
112+
>>> x = input("Enter something: ") # now there's space between : and where i type
113+
Enter something: hello
114+
>>>
115+
```
116+
117+
## More about print
118+
80119
We can also print an empty line by calling print without any
81-
arguments:
120+
arguments.
82121

83122
```py
84123
>>> print()
@@ -97,7 +136,7 @@ world
97136
```
98137

99138
If we want to print a backslash, we need to **escape** it by typing
100-
two backslashes:
139+
two backslashes.
101140

102141
[comment]: # (For some reason, GitHub's syntax highlighting doesn't)
103142
[comment]: # (work here.)
@@ -123,37 +162,14 @@ Unlike with `+`, the arguments don't need to be strings.
123162
>>>
124163
```
125164

126-
Not all functions return None. The input function can be used for
127-
getting a string from the user.
128-
129-
```py
130-
>>> x = input("Enter something:")
131-
Enter something:hello
132-
>>> x
133-
'hello'
134-
>>>
135-
```
136-
137-
`input("Enter something:")` showed the text `Enter something:` on the
138-
screen and waited for me to type something. I typed hello and pressed
139-
Enter. Then input returned the hello I typed as a string and it was
140-
assigned to x.
141-
142-
Usually we want to add a space after the `:`, like this:
143-
144-
```py
145-
>>> x = input("Enter something: ") # now there's space between : and where i type
146-
Enter something: hello
147-
>>>
148-
```
149-
150165
## Summary
151166

152167
- `function()` calls a function without any arguments, and
153168
`function(1, 2, 3)` calls a function with 1, 2 and 3 as arguments.
154-
`x = function()` calls a function, and assigns the return value of
155-
the call to x.
156169
- When a function is called, it does something and returns something.
170+
- `function(stuff)` is "replaced" with the return value in the code
171+
that called it. For example, `x = function()` calls a function, and
172+
then does `x = the_return_value` and the return value ends up in x.
157173
- Python comes with `print` and `input`. They are built-in functions.
158174

159175
***

0 commit comments

Comments
 (0)