-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit 1bd1c82
Showing
255 changed files
with
19,937 additions
and
0 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,4 @@ | ||
# Sphinx build info version 1 | ||
# This file hashes the configuration used when building these files. When it is not found, a full rebuild will be done. | ||
config: 300af7b4c3ade1c47b24c602e711fdb4 | ||
tags: 645f666f9bcd5a90fca523b33c5a78b7 |
Empty file.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
Oops, something went wrong.
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,7 @@ | ||
.. _creating-objects: | ||
|
||
================ | ||
Creating Objects | ||
================ | ||
|
||
... |
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,67 @@ | ||
.. _by-value-or-by-reference: | ||
|
||
By Value or By Reference | ||
======================== | ||
|
||
The second method of transferring information to a function is to pass it `By Reference <https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_reference>`_. This means that a pointer or *reference* to where the data is stored in memory is passed to the function and not a copy of the data. Since a pointer to where the data exists has been passed, if you actually change the value of the data in the function, the actual values of the data in the main program where the function was called from will also be changed. This can be very powerful but also **very dangerous**. Be careful passing parameters *By Reference*, you might mistakenly change a value when that is not what you or someone else is expecting. The rule of thumb is that unless there is a **really good** reason to pass something By Reference, you never do and you always pass parameters By Value (even though it takes up more space in memory). | ||
|
||
Each language has its own syntax on how to declare you are going to accept a value by reference when you are declaring a function here is an example: | ||
|
||
Code for Function passing a value By Reference | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
.. tabs:: | ||
|
||
.. group-tab:: C | ||
.. code-block:: C | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/C/main.c | ||
:language: C | ||
:linenos: | ||
:emphasize-lines: 9-12, 23 | ||
.. group-tab:: C++ | ||
.. code-block:: C++ | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/CPP/main.cpp | ||
:language: C++ | ||
:linenos: | ||
:emphasize-lines: 9-12, 23 | ||
|
||
.. group-tab:: C# | ||
.. code-block:: C# | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/CSharp/main.cs | ||
:language: C# | ||
:linenos: | ||
:emphasize-lines: 12-15, 26 | ||
.. group-tab:: Go | ||
.. code-block:: Go | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/Go/main.go | ||
:language: go | ||
:linenos: | ||
:emphasize-lines: 11-14, 25 | ||
.. group-tab:: Java | ||
.. code-block:: Java | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/Java/Main.java | ||
:language: java | ||
:linenos: | ||
:emphasize-lines: 13-19, 33 | ||
.. group-tab:: JavaScript | ||
.. code-block:: JavaScript | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/JavaScript/main.js | ||
:language: javascript | ||
:linenos: | ||
:emphasize-lines: 7-10, 21 | ||
.. group-tab:: Python | ||
.. code-block:: Python | ||
.. literalinclude:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/Python/main.py | ||
:language: python | ||
:linenos: | ||
:emphasize-lines: 8-12, 24 | ||
Example Output | ||
^^^^^^^^^^^^^^ | ||
.. image:: ../../code_examples/4-Functions/6-By_Value_or_By_Reference/vhs.gif | ||
:alt: Code example output | ||
:align: left |
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,92 @@ | ||
.. _default-values: | ||
|
||
Default Values | ||
============== | ||
|
||
All of the functions that we have looked at to this point, you had to ensure that you were sending the exact same number of parameters to the function as it was expecting. To help us do this a good IDE will have, "auto-complete" giving us a little pop out window to show us what should be passed over to the function. | ||
|
||
Some built in functions we have been using can be accessed in multiple different ways though. For example in Python there is a built in function called random.randrange(). It is kind of like random.ranint() that we have used in the past. Here is the definition for random.ranint(): | ||
|
||
.. code-block:: python | ||
random.randint(a, b) | ||
// Return a random integer N such that a <= N <= b. Alias for randrange(a, b+1). | ||
Notice that "a & b" are our starting and ending points. | ||
Here is the definition for random.randrange(): | ||
.. code-block:: python | ||
random.randrange(start, stop[, step]) | ||
// The positional argument pattern matches that of range(). | ||
First off there is actually 2 seperate ways we could call this function: | ||
- random.randrange(start, stop) | ||
- random.randrange(start, stop, step) | ||
It seems that *step* is, "optional" which it is. By default, if you do not provide it, then python assumes the value is just 1. You can choose for example to place in a 2, and then only even numbers will be chosen. Here is how we would define the function random.randrange() to get this optional parameter: | ||
.. code-block:: python | ||
def randrange(start, stop, step = 1): | ||
Notice that right in the declaration of the function the, "default optional parameter" is being set. If it is not provided as a parameter, the default value is just used. Each programming language has its own syntax to make this kind of optional parameter work. Here is an example: | ||
Code for Function with Default Values | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
.. tabs:: | ||
.. group-tab:: C | ||
.. code-block:: C | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/C/main.c | ||
:language: C | ||
:linenos: | ||
.. group-tab:: C++ | ||
.. code-block:: C++ | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/CPP/main.cpp | ||
:language: C++ | ||
:linenos: | ||
:emphasize-lines: 9-22, 49, 51 | ||
.. group-tab:: C# | ||
.. code-block:: C# | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/CSharp/main.cs | ||
:language: C# | ||
:linenos: | ||
:emphasize-lines: 14-24, 49, 51 | ||
.. group-tab:: Go | ||
.. code-block:: Go | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/Go/main.c | ||
:language: go | ||
:linenos: | ||
.. group-tab:: Java | ||
.. code-block:: Java | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/Java/Main.java | ||
:language: java | ||
:linenos: | ||
:emphasize-lines: 12-22, 47 | ||
.. group-tab:: JavaScript | ||
.. code-block:: JavaScript | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/JavaScript/main.js | ||
:language: javascript | ||
:linenos: | ||
:emphasize-lines: 9-19, 37, 39 | ||
.. group-tab:: Python | ||
.. code-block:: Python | ||
.. literalinclude:: ../../code_examples/4-Functions/5-Default_Values/Python/main.py | ||
:language: python | ||
:linenos: | ||
:emphasize-lines: 9-18, 34, 36 | ||
Example Output | ||
^^^^^^^^^^^^^^ | ||
.. image:: ../../code_examples/4-Functions/5-Default_Values/vhs.gif | ||
:alt: Code example output | ||
:align: left |
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,171 @@ | ||
.. _functions-with-a-parameter: | ||
|
||
Functions with a Parameter | ||
========================== | ||
|
||
A function often needs pieces of information to be able to complete its work. In the last section you might have noticed that I had to ask for the length and width twice, once for each of the area and perimeter calculations; clearly not ideal. One method of doing this is to declare the variable as a *global variable* and then any code within that file can access it. This is a very brute force method and is **very bad programming style**. By doing it this way, the variable is created and held in memory for the entire time the file is running, even though the information may only be needed for a small fraction of the time. Also the function is no longer portable, since it relies on external global variables that might not exist in another program. A better, more elegant and more memory friendly way is to pass the information into the function using a `parameter <https://en.wikipedia.org/wiki/Parameter_(computer_programming)>`_. There are three main ways to pass information to a function, by value, by reference and by object reference. For the time being we will just consider by value. (We will even assume python is doing by value, even though it is really doing it by `object reference <https://www.python-course.eu/passing_arguments.php>`_.) | ||
|
||
Passing By Value | ||
---------------- | ||
|
||
The first method of transferring information to a function is to pass it, `“By Value” <https://www.mathwarehouse.com/programming/passing-by-value-vs-by-reference-visual-explanation.php>`_. This means that a copy of the data is made and it is passed over to the function to do with it what it pleases. Since it is a **copy** of the data, any changes to the data are not reflected in the original variable. From the animation below you can see that when the cup is passed "By Value" to the function and then filled up, the original cup is still empty. This is because the variable passed to the function is a copy, not the original variable. If the function changes this variable, nothing happens to the original one. | ||
|
||
.. image:: ./images/pass-by-reference-vs-pass-by-value-animation.gif | ||
:alt: Pass by value or reference | ||
:align: center | ||
|
||
|
||
A variable or value passed along inside a function call is called an `parameter <https://en.wikipedia.org/wiki/Parameter_(computer_programming)>`_. Parameter(s) are usually placed inside a bracket when you invoke the function. For example: | ||
|
||
Code for Function with a Parameter | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
.. tabs:: | ||
|
||
.. group-tab:: C | ||
.. code-block:: C | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/C/main.c | ||
:language: C | ||
:lines: 28 | ||
.. group-tab:: C++ | ||
.. code-block:: C++ | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/CPP/main.cpp | ||
:language: C++ | ||
:lines: 30 | ||
|
||
.. group-tab:: C# | ||
.. code-block:: C# | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/CSharp/main.cs | ||
:language: C# | ||
:lines: 30 | ||
.. group-tab:: Go | ||
.. code-block:: Go | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Go/main.go | ||
:language: go | ||
:lines: 32 | ||
.. group-tab:: Java | ||
.. code-block:: Java | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Java/Main.java | ||
:language: java | ||
:lines: 45 | ||
.. group-tab:: JavaScript | ||
.. code-block:: JavaScript | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/JavaScript/main.js | ||
:language: javascript | ||
:lines: 24 | ||
.. group-tab:: Python | ||
.. code-block:: Python | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Python/main.py | ||
:language: python | ||
:lines: 28 | ||
When you are creating your function, you must also tell the program that the function is expecting these two values. To do this after the function name declaration you place in brackets the two declaration statements declaring that the function must be passed in two variables (just like when a regular variable is being declared). If your programming language requires that you declare what type the variables will be normally, you will most like have to do that too. | ||
|
||
The following is the function declaration line for the examples above: | ||
|
||
.. tabs:: | ||
|
||
.. group-tab:: C | ||
.. code-block:: C | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/C/main.c | ||
:language: C | ||
:lines: 10 | ||
.. group-tab:: C++ | ||
.. code-block:: C++ | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/CPP/main.cpp | ||
:language: C++ | ||
:lines: 11 | ||
|
||
.. group-tab:: C# | ||
.. code-block:: C# | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/CSharp/main.cs | ||
:language: C# | ||
:lines: 13 | ||
.. group-tab:: Go | ||
.. code-block:: Go | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Go/main.go | ||
:language: go | ||
:lines: 14 | ||
.. group-tab:: Java | ||
.. code-block:: Java | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Java/Main.java | ||
:language: java | ||
:lines: 17 | ||
.. group-tab:: JavaScript | ||
.. code-block:: JavaScript | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/JavaScript/main.js | ||
:language: javascript | ||
:lines: 9 | ||
.. group-tab:: Python | ||
.. code-block:: Python | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Python/main.py | ||
:language: python | ||
:lines: 10 | ||
Here is a full example of the previous sections program, but now the main function takes care of getting the radius. This way it only has to ask you the information once and it passes the radius to the function: | ||
|
||
.. tabs:: | ||
|
||
.. group-tab:: C | ||
.. code-block:: C | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/C/main.c | ||
:language: C | ||
:linenos: | ||
:emphasize-lines: 10-18, 28 | ||
.. group-tab:: C++ | ||
.. code-block:: C++ | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/CPP/main.cpp | ||
:language: C++ | ||
:linenos: | ||
:emphasize-lines: 11-19, 30 | ||
|
||
.. group-tab:: C# | ||
.. code-block:: C# | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/CSharp/main.cs | ||
:language: C# | ||
:linenos: | ||
:emphasize-lines: 12-31, 30 | ||
.. group-tab:: Go | ||
.. code-block:: Go | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Go/main.go | ||
:language: go | ||
:linenos: | ||
:emphasize-lines: 14-21, 32 | ||
.. group-tab:: Java | ||
.. code-block:: Java | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Java/Main.java | ||
:language: java | ||
:linenos: | ||
:emphasize-lines: 12-23, 45 | ||
.. group-tab:: JavaScript | ||
.. code-block:: JavaScript | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/JavaScript/main.js | ||
:language: javascript | ||
:linenos: | ||
:emphasize-lines: 9-17, 24 | ||
.. group-tab:: Python | ||
.. code-block:: Python | ||
.. literalinclude:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/Python/main.py | ||
:language: python | ||
:linenos: | ||
:emphasize-lines: 10-17, 28 | ||
Example Output | ||
^^^^^^^^^^^^^^ | ||
.. image:: ../../code_examples/4-Functions/2-Functions_with_a_Parameter/vhs.gif | ||
:alt: Code example output | ||
:align: left |
Oops, something went wrong.