diff --git a/docs/functions/intro-example.py b/docs/functions/intro-example.py new file mode 100644 index 0000000..4ec88bc --- /dev/null +++ b/docs/functions/intro-example.py @@ -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. \ No newline at end of file diff --git a/docs/functions/intro-to-functions.md b/docs/functions/intro-to-functions.md index 6f78466..ee84853 100644 --- a/docs/functions/intro-to-functions.md +++ b/docs/functions/intro-to-functions.md @@ -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 + - print \ No newline at end of file diff --git a/docs/functions/user-functions/README.md b/docs/functions/user-functions/README.md index aa84d08..2067388 100644 --- a/docs/functions/user-functions/README.md +++ b/docs/functions/user-functions/README.md @@ -14,27 +14,3 @@ def name_of_function(passed_variable): ``` The contents of the function is always indented to show that it belongs to the function definition above it. -outline user defined - - abstraction and reusability - - modularity - - namespace separation - - how a function is called and how it is defined - - passing arguements - - keyword arguement rules - - mutable default parameteres - - pass by value and pass by reference - - return statement - - side effects - - variable length arguement lists - - tuple packing and unpacking - - dictionary packing and unpacking - - docstrings - - dunders - - function annotations - -outline built-in functions - - generators - - yeild - - lambda - - map - - \ No newline at end of file diff --git a/docs/values-and-types/README.md b/docs/values-and-types/README.md index 2796179..e7fa5ef 100644 --- a/docs/values-and-types/README.md +++ b/docs/values-and-types/README.md @@ -70,7 +70,7 @@ In this example, the data is stored in a tuple and then unpacked for use in the This brings us nicely onto the other way to use tuples, as immutable lists. The reason you use tuples as immutable lists is so that you can use slicing and other list functionality that also works with tuples, but without the fear of someone else in your production team modifying the list by accident. Another way to store data as records is to store it as a dictionary. ### Dictionary -Dictionaries are an interesting data structure, its used to store data in key value pairs so that you can easily access the data by label instead of by index value. This is especially helpful if you want to dynamically create a csv file or know you need to store categorised data throughout a program, but other people are in charge of much of the data input. Dictionaries always have this form: +Dictionaries are an interesting data structure, its used to store data in key value pairs so that you can easily access the data by label instead of by index value. This is especially helpful if you want to dynamically create a csv file or know you need to store categorised data throughout a program but other people are in charge of much of the data input. Dictionaries always have this form: ```python d = { diff --git a/mk_doc_ultra b/mk_doc_ultra index b83db95..9912d0d 160000 --- a/mk_doc_ultra +++ b/mk_doc_ultra @@ -1 +1 @@ -Subproject commit b83db95bfdf8c9c4e9844d5581b979bf0c12de2b +Subproject commit 9912d0d488819854a629a860b29715c53d1995d7