diff --git a/docs/functions/intro-to-functions.md b/docs/functions/intro-to-functions.md index b7136d4..4918900 100644 --- a/docs/functions/intro-to-functions.md +++ b/docs/functions/intro-to-functions.md @@ -46,10 +46,34 @@ Now we've made the code take in an arbitrary number of arguments and add them al Don't worry if much of this feels a little advanced for you right now, much of this is broken down in the later sections and explained in greater detail. It's only important that you understand that these principles exist and very roughly how they work at this stage. ## Modularity -The idea of modular programming lends itself to the idea that each function contains all of the code required to do a single job, and then every time you need to do that job again the function is called. If you think about how a character in a computer game is controlled, every time the character turns left it must call a turn left function. The same if it needs to jump or run backwards, directional animations and everything that makes a game run, all could be their own independent functions containing all the code to do each specific job. Consider this code: - - - +The term modular programming lends itself to the idea that each function contains all of the code required to do a single job, and then every time you need to do that job again the function is called. If you think about how a character in a computer game is controlled, every time the character turns left it must call a turn left function. The same if it needs to jump or run backwards, directional animations and everything that makes a game run, all could be their own independent functions containing all the code to do each specific job. Consider this pseudo code: + +``` python + # outline of a modular program + def open_file(): + + + + + def process_file_data(): + + + + + def close_file(): + + + + + open_file() + process_file_data() + close_file() +``` + +In this pseudo code, you can see that we have three functions all doing different jobs. Each one of the functions can contain code for the testing and for completing the steps required to do the job for which its named. In this way, you create modular code that can be used later in the program or exported into other projects, because you can call the function into another file. You can find more information on modules in the modules section [here](../modules/README.md). + + +{{ todo("remove this outline, for guide only.") }} ## Outline outline user defined - abstraction and reusability diff --git a/docs/functions/user-functions/README.md b/docs/functions/user-functions/README.md index 2067388..7dd91b8 100644 --- a/docs/functions/user-functions/README.md +++ b/docs/functions/user-functions/README.md @@ -12,5 +12,11 @@ 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. +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. + +## 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 diff --git a/docs/functions/user-functions/example-2.py b/docs/functions/user-functions/example-2.py new file mode 100644 index 0000000..3510889 --- /dev/null +++ b/docs/functions/user-functions/example-2.py @@ -0,0 +1,30 @@ +#!/usr/bin/env python3 +# example-2.py +# showing pass by reference +var = 20 +print("Initial variable value and object number:") +print(var) +print(id(var)) + +def pass_by_reference(): + global var + print("Initial ") + print(id(var)) + var += 20 + print(var) + print(id(var)) + +pass_by_reference() +print(var) +print(id(var)) + +def pass_by_value(var): + total = var + total = total + 20 + print(total) + print(id(total)) + return total + +pass_by_value(var) +print(var) +print(id(var)) \ No newline at end of file diff --git a/docs/functions/user-functions/example-pass-by.cpp b/docs/functions/user-functions/example-pass-by.cpp new file mode 100644 index 0000000..9ec94a2 --- /dev/null +++ b/docs/functions/user-functions/example-pass-by.cpp @@ -0,0 +1,42 @@ +#include +using namespace std; + +// C++ prototypes +void pass_by_reference(int &var_1); +void pass_by_value(int var_1); + +// example-pass-by.cpp +// Illustrating pass by reference and value +// using C++ + +// Function declarations +void pass_by_reference(int &var_1) +{ + var_1 = var_1 + 5; + cout << var_1 << "\n"; +} + +void pass_by_value(int var_1) +{ + var_1 = var_1 + 10; + cout << var_1 << "\n"; +} + +// Program entry point +int main () +{ + int var_1 = 20; + cout << "The initial value of var_1 is: " << var_1 << "\n"; + + // This preserves the memory address and it + // will keep the value. + cout << "This is after it's passed by reference:" << "\n"; + pass_by_reference(var_1); + cout << var_1 << "\n"; + + // This doesn't preserve the memory address + // and passes a copy of the value + cout << "This is after it's passed by Value." << "\n"; + pass_by_value(var_1); + cout << var_1 << "\n"; +} \ No newline at end of file diff --git a/docs/functions/user-functions/example-pass-by.exe b/docs/functions/user-functions/example-pass-by.exe new file mode 100644 index 0000000..479b217 Binary files /dev/null and b/docs/functions/user-functions/example-pass-by.exe differ diff --git a/mk_doc_ultra b/mk_doc_ultra index 5a393ff..9912d0d 160000 --- a/mk_doc_ultra +++ b/mk_doc_ultra @@ -1 +1 @@ -Subproject commit 5a393ffeb3e613fd9434085c9f56c43c51be71ac +Subproject commit 9912d0d488819854a629a860b29715c53d1995d7