Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
63 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#!/usr/bin/env python3 | ||
# into-example.py | ||
# Displaying scope as a call to a variable | ||
|
||
variable_1 = 70 | ||
|
||
def add_two(number): | ||
result = number + 2 | ||
print(f"the square of {number} is: {result}") | ||
|
||
add_two(variable_1) | ||
|
||
try: | ||
print("testing functions...") | ||
# this isn't accessable from outside the square() function | ||
print(result) | ||
print(base) | ||
except NameError: | ||
print("These functions aren't in scope") | ||
|
||
|
||
# NOTE!! This code is heavily mentioned in the markdown file "intro-to-functions.md" | ||
# if you change it here then you may mess something up there. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,49 @@ | ||
# Functions | ||
Functions are the way in which we encapsulate code so that it can be reused multiple times. Sometimes this is in a single program and other times it's to include inside our own custom library. | ||
Functions are the way in which we encapsulate code so that it can be reused multiple times. Sometimes this is in a single program and other times it's to include inside our own custom library. | ||
|
||
Any time code needs to be executed or evaluated in the same way, it can also be passed into a function to have the same process done to it. By passing functions to functions we can build up any complex arrangement you can think of, and is how all modern programs are constructed. | ||
|
||
Some functions inside python are built in functions that do a specific job or exhibit a specific behaviour. There are also ways to perform small inline functions in this manner. This will be covered in the [built in functions](built-in-functions/README.md) section. | ||
Some functions inside python are built in functions that do a specific job or exhibit a specific behaviour. There are also ways to perform small inline functions in this manner. This will be covered in the [built in functions](built-in-functions/README.md) section. Let's look at some of the reasons we use functions in more depth: | ||
|
||
## Scope | ||
Scope is the technical term for if python can see a variable or function at any point in your program. As scope is one of those things that makes more sense when you see it, consider this example: | ||
|
||
{{ code_from_file("functions/intro-example.py", 2, 20, execute=true) }} | ||
|
||
!!! Example | ||
In this example, you can see that we have created a variable named "variable_1" and we have created a function using the key word "def", for more information on creating variables you can find it [here](functions/user-functions/README.md) in the user functions section. | ||
|
||
## Abstraction and reusability | ||
|
||
## Modularity | ||
|
||
|
||
|
||
## Outline | ||
outline user defined | ||
- abstraction and reusability | ||
- modularity | ||
- namespace separation | ||
- how a function is called and how it is defined | ||
- passing arguments | ||
- keyword argument rules | ||
- mutable default parameters | ||
- pass by value and pass by reference | ||
- return statement | ||
- side effects | ||
- variable length argument lists | ||
- tuple packing and unpacking | ||
- dictionary packing and unpacking | ||
- docstrings | ||
- dunders | ||
- function annotations | ||
|
||
outline built-in functions | ||
- generators | ||
- yield | ||
- lambda | ||
- map | ||
- Special mentions | ||
- any | ||
- exec | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters