GitHub

@@ -118,7 +118,7 @@ This will become clearer as you see more examples.

118118119119

Let's start by discussing how it's done.

120120121-

### Syntax

121+

### Basic Syntax

122122123123

Here's a very simple Python function, that implements the mathematical function

124124

$f(x) = 2 x + 1$

@@ -171,6 +171,20 @@ print(new_abs_function(3))

171171

print(new_abs_function(-3))

172172

```

173173174+

Note that a function can have arbitrarily many `return` statements (including zero).

175+176+

Execution of the function terminates when the first return is hit, allowing

177+

code like the following example

178+179+

```{code-cell} python3

180+

def f(x):

181+

if x < 0:

182+

return 'negative'

183+

return 'nonnegative'

184+

```

185+186+

Functions without a return statement automatically return the special Python object `None`.

187+174188

### Keyword Arguments

175189176190

```{index} single: Python; keyword arguments

@@ -227,79 +241,8 @@ In particular

227241

* Any object can be passed to a function as an argument, including other functions.

228242

* A function can return any kind of object, including functions.

229243230-

We already {ref}`gave an example <test_program_6>` of how straightforward it is to pass a function to

231-

a function.

232-233-

Note that a function can have arbitrarily many `return` statements (including zero).

234-235-

Execution of the function terminates when the first return is hit, allowing

236-

code like the following example

237-238-

```{code-cell} python3

239-

def f(x):

240-

if x < 0:

241-

return 'negative'

242-

return 'nonnegative'

243-

```

244-245-

Functions without a return statement automatically return the special Python object `None`.

246-247-248-

### Docstrings

249-250-

```{index} single: Python; Docstrings

251-

```

252-253-

Python has a system for adding comments to functions, modules, etc. called *docstrings*.

254-255-

The nice thing about docstrings is that they are available at run-time.

256-257-

Try running this

258-259-

```{code-cell} python3

260-

def f(x):

261-

"""

262-

This function squares its argument

263-

"""

264-

return x**2

265-

```

266-267-

After running this code, the docstring is available

268-269-

```{code-cell} ipython

270-

f?

271-

```

272-273-

```{code-block} ipython

274-

:class: no-execute

275-276-

Type: function

277-

String Form:<function f at 0x2223320>

278-

File: /home/john/temp/temp.py

279-

Definition: f(x)

280-

Docstring: This function squares its argument

281-

```

282-283-

```{code-cell} ipython

284-

f??

285-

```

286-287-

```{code-block} ipython

288-

:class: no-execute

289-290-

Type: function

291-

String Form:<function f at 0x2223320>

292-

File: /home/john/temp/temp.py

293-

Definition: f(x)

294-

Source:

295-

def f(x):

296-

"""

297-

This function squares its argument

298-

"""

299-

return x**2

300-

```

301-302-

With one question mark we bring up the docstring, and with two we get the source code as well.

244+

We will give examples of how straightforward it is to pass a function to

245+

a function in the following sections.

303246304247

### One-Line Functions: `lambda`

305248

Read the original on github.com ↗