Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added more generalisation for the code_from_file() function to suppor…
…t multiple languages and added the C++ examples
  • Loading branch information
Barnsa committed Aug 5, 2020
1 parent cf8c30d commit 011a302
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 10 deletions.
6 changes: 1 addition & 5 deletions .vscode/settings.json
@@ -1,12 +1,8 @@
{
"cSpell.words": [
<<<<<<< Updated upstream
"adzyb",
=======
>>>>>>> Stashed changes
"decrement",
"iterable",
"pythonic"
],
"python.pythonPath": "venv\\Scripts\\python.exe"
"python.pythonPath": "c:\\Program Files\\Python38\\python.exe"
}
10 changes: 7 additions & 3 deletions docs/functions/user-functions/README.md
@@ -1,7 +1,7 @@
# Functions
Functions are self contained objects that are used to create code that we often want to reuse. The most arbitrary use of a function is in this code:

{{ code_from_file("conditionals/example-1.py", 1, 9, execute=True) }}
{{ code_from_file("functions/user-functions/example-1.py", 1, 9, execute=True) }}

As you can see, we have created our first function. To create a function you must first use the def keyword followed by the name for the function. The naming conventions for functions are the same as they are for variables. However, it is good practice to have descriptive names for your functions so that you can recognise what they do at a glance.

Expand All @@ -15,8 +15,12 @@ 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. Python then knows that this is the local scope of that function and looks there first for any variables, functions or data you might be trying to use in the function. The passed_variable placeholder can be one or more variables that are passed to the function so they can be used. These are known as function arguments, and are how we can move data between one function and another.

!!! hint
Understanding how data moves between functions is an important part of programming, as side effects can happen when you don't know what's happening behind the scenes. Make sure you feel solid with the next concepts as they can change depending on the programming language and is the starting point for reducing bugs in code and secure programming.
Understanding how data moves between functions is an important part of programming as side effects can happen when you don't know what's happening behind the scenes. Make sure you feel solid with the next concepts in passing by value and reference as they can change depending on the programming language and is the starting point for reducing bugs in code and understanding secure programming.

## Passing-by-value and Passing-by-reference
These concepts are two different ways we pass variables to functions in programming. When we pass a value by reference
These concepts are two different ways we pass variables to functions in programming. When we pass a value by reference we are essentially passing the actual variable to the function and when we pass by value we are copying the value of the variable and passing the copy to the function. Once the function has done what it needs to do, the pass by value variable is then discarded, this is also called a pure function. The best way to program to ensure less bugs is to only programming using pure functions, but sometimes there is need to program using pass by reference and modifying functions. For a more in-depth look at how passing by reference and value work, take a look at this C++ code:

{{ code_from_file("functions/user-functions/example-pass-by.cpp", flavor="c++") }}

!!! example
In this example you can see that, apart from all the C++ shenanigans, that there isn't a huge difference from python. There are still function definitions and there is still a part where the main program is run. In the function definitions we can see that there is two functions declared, one called pass_by_value and the other pass_by_reference. As the code runs we can see that
2 changes: 1 addition & 1 deletion docs/functions/user-functions/example-pass-by.cpp
@@ -1,7 +1,7 @@
#include <iostream>
using namespace std;

// C++ prototypes
// C++ prototypes, not technically required here but old habits...
void pass_by_reference(int &var_1);
void pass_by_value(int var_1);

Expand Down
2 changes: 1 addition & 1 deletion mkdocs.yml
Expand Up @@ -35,7 +35,7 @@ nav:
markdown_extensions:
- admonition ## see: https://squidfunk.github.io/mkdocs-material/extensions/admonition/ for usage
- codehilite:
linenums: false ## For line numbers in code blocks, true/false toggle
linenums: False ## For line numbers in code blocks, true/false toggle
# - footnotes
# - meta

Expand Down

0 comments on commit 011a302

Please sign in to comment.