Skip to content

Commit

Permalink
Episode 8, functions : about reading a global variable inside a funct…
Browse files Browse the repository at this point in the history
…ion (#942)

* First proposal

* Remove the global trap explanation from the section about variable scope, and add a sentence in the dedicated challenge.

* Remove dummy sentence extract.

* Move the debate about the local/global trap in the challenge.

* Second review

* Wrap line to keep under 100 characters

Co-authored-by: Lauren Ko <lauren.ko@unt.edu>
  • Loading branch information
chavid and ldko authored Jul 14, 2021
1 parent e0bdbd3 commit 120de61
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
31 changes: 29 additions & 2 deletions episodes/08-func.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ keypoints:
- "Call a function using `function_name(value)`."
- "Numbers are stored as integers or floating-point numbers."
- "Variables defined within a function can only be seen and used within the body of the function."
- "If a variable is not defined within the function it is used,
Python looks for a definition before the function call"
- "Variables created outside of any function are called global variables."
- "Within a function, we can access global variables."
- "Variables created within a function override global variables if their names match."
- "Use `help(thing)` to view help for something."
- "Put docstrings in functions to provide help for that function."
- "Specify default values for parameters when defining a function using `name=value`
Expand Down Expand Up @@ -177,6 +178,29 @@ temperature in Kelvin was: 373.15
~~~
{: .output}

The variable `temp_kelvin`, being defined outside any function,
is said to be [global]({{ page.root }}/reference.html#global-variable).

Inside a function, one can read the value of such global variables:
~~~
def print_temperatures():
print('temperature in Fahrenheit was:', temp_fahr)
print('temperature in Kelvin was:', temp_kelvin)
temp_fahr = 212.0
temp_kelvin = fahr_to_kelvin(temp_fahr)
print_temperatures()
~~~
{: .language-python}

~~~
temperature in Fahrenheit was: 212.0
temperature in Kelvin was: 373.15
~~~
{: .output}


## Tidying up

Now that we know how to wrap bits of code up in functions,
Expand Down Expand Up @@ -872,6 +896,9 @@ readable code!
> > `k`. The function does not return any values
> > and does not alter `k` outside of its local copy.
> > Therefore the original value of `k` remains unchanged.
> > Beware that a local `k` is created because `f2k` internal statements
> > *affect* a new value to it. If `k` was only `read`, it would simply retreive the
> > global `k` value.
> {: .solution}
{: .challenge}

Expand Down
4 changes: 4 additions & 0 deletions reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,10 @@ function
function call
: A use of a function in another piece of software.

global variable
: A variable defined outside of a function. It can be used in global statements,
and read inside functions.

heat map
: A graphical representation of two-dimensional data in which colors,
ranging on a scale of hue or intensity, represent the data values.
Expand Down

0 comments on commit 120de61

Please sign in to comment.