https://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_6_-_Functions&feed=atom&action=historyHPL3/Scripting/AngelScript Fundamentals/Chapter 6 - Functions - Revision history2024-03-28T09:24:14ZRevision history for this page on the wikiMediaWiki 1.34.2https://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_6_-_Functions&diff=4615&oldid=prevTiMan: /* Returning Values */2020-08-16T08:55:01Z<p><span dir="auto"><span class="autocomment">Returning Values</span></span></p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 08:55, 16 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l215" >Line 215:</td>
<td colspan="2" class="diff-lineno">Line 215:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This is because when you pass a variable as a parameter to a function, it does not pass the variable itself. Instead, it takes the value ''inside'' the variable and copies it into the parameter of the function. This is called ''passing by value'', and this happens whever you pass something as a parameter in AngelScript.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This is because when you pass a variable as a parameter to a function, it does not pass the variable itself. Instead, it takes the value ''inside'' the variable and copies it into the parameter of the function. This is called ''passing by value'', and this happens whever you pass something as a parameter in AngelScript.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>{{note|The alternative to passing by value is called ''passing by reference'', in which you do actually pass the variable itself to the function. This concept is a bit more advanced for this tutorial series, but you can read about it in <del class="diffchange diffchange-inline">the </del>[[HPL3/Scripting/AngelScript_Fundamentals/Chapter_9_-_Miscellaneous_AngelScript_Features#Passing_Parameters_By_Reference_2|<del class="diffchange diffchange-inline">appendix section</del>]].)}}</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>{{note|The alternative to passing by value is called ''passing by reference'', in which you do actually pass the variable itself to the function. This concept is a bit more advanced for this tutorial series, but you can read about it in [[HPL3/Scripting/AngelScript_Fundamentals/Chapter_9_-_Miscellaneous_AngelScript_Features#Passing_Parameters_By_Reference_2|<ins class="diffchange diffchange-inline">Chapter 9</ins>]].)}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>==Function Overloading==</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>==Function Overloading==</div></td></tr>
</table>TiManhttps://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_6_-_Functions&diff=4614&oldid=prevTiMan: /* Returning Values */2020-08-16T08:54:09Z<p><span dir="auto"><span class="autocomment">Returning Values</span></span></p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 08:54, 16 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l5" >Line 5:</td>
<td colspan="2" class="diff-lineno">Line 5:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>A function is a construct in programming that takes a section of code and wraps it up into a package that you can then refer to any time you want to call that code. This is immensely useful if like in the hypothetical example you need to run a subsection of your code many times over the course of your program. It also helps you to organize your program, separating your code into easily describable and readable chunks.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>A function is a construct in programming that takes a section of code and wraps it up into a package that you can then refer to any time you want to call that code. This is immensely useful if like in the hypothetical example you need to run a subsection of your code many times over the course of your program. It also helps you to organize your program, separating your code into easily describable and readable chunks.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>== Function Structure ==</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>==Function Structure==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The structure of a function is similar to that of the statements that you learned about in the previous chapter:</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>The structure of a function is similar to that of the statements that you learned about in the previous chapter:</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l50" >Line 50:</td>
<td colspan="2" class="diff-lineno">Line 50:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>There's a function right there! The function is called <code>OnEnter</code>, and as the comment says, it is called by the HPL3 engine whenever the map is entered.}}</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>There's a function right there! The function is called <code>OnEnter</code>, and as the comment says, it is called by the HPL3 engine whenever the map is entered.}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>== Parameters ==</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>==Parameters==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>I promised that I would explain the importance of those parameters in greater detail, and I will do that now. The parentheses are for defining a function's ''parameters''.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>I promised that I would explain the importance of those parameters in greater detail, and I will do that now. The parentheses are for defining a function's ''parameters''.</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l106" >Line 106:</td>
<td colspan="2" class="diff-lineno">Line 106:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>That second parameter is defined as being a string, but <code>10</code> is not a string (not to be confused with <code>"10"</code>, which ''is'' a string). If you tried to do this, then HPL3 would consider it an error, and your code will not run.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>That second parameter is defined as being a string, but <code>10</code> is not a string (not to be confused with <code>"10"</code>, which ''is'' a string). If you tried to do this, then HPL3 would consider it an error, and your code will not run.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>== Returning Values ==</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>==Returning Values==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>In addition to receiving information, a function can also be set up to give information back to the code where it was called:</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>In addition to receiving information, a function can also be set up to give information back to the code where it was called:</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l215" >Line 215:</td>
<td colspan="2" class="diff-lineno">Line 215:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This is because when you pass a variable as a parameter to a function, it does not pass the variable itself. Instead, it takes the value ''inside'' the variable and copies it into the parameter of the function. This is called ''passing by value'', and this happens whever you pass something as a parameter in AngelScript.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>This is because when you pass a variable as a parameter to a function, it does not pass the variable itself. Instead, it takes the value ''inside'' the variable and copies it into the parameter of the function. This is called ''passing by value'', and this happens whever you pass something as a parameter in AngelScript.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>{{note|The alternative to passing by value is called ''passing by reference'', in which you do actually pass the variable itself to the function. This concept is a bit more advanced for this tutorial series, but <del class="diffchange diffchange-inline">I do talk </del>about it in the [[<del class="diffchange diffchange-inline">hpl3</del>/<del class="diffchange diffchange-inline">community</del>/<del class="diffchange diffchange-inline">scripting</del>/<del class="diffchange diffchange-inline">angelscript_tutorial</del>#<del class="diffchange diffchange-inline">appendixuncommon_angelscript_features</del>|appendix section]].)}}</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>{{note|The alternative to passing by value is called ''passing by reference'', in which you do actually pass the variable itself to the function. This concept is a bit more advanced for this tutorial series, but <ins class="diffchange diffchange-inline">you can read </ins>about it in the [[<ins class="diffchange diffchange-inline">HPL3</ins>/<ins class="diffchange diffchange-inline">Scripting</ins>/<ins class="diffchange diffchange-inline">AngelScript_Fundamentals</ins>/<ins class="diffchange diffchange-inline">Chapter_9_-_Miscellaneous_AngelScript_Features</ins>#<ins class="diffchange diffchange-inline">Passing_Parameters_By_Reference_2</ins>|appendix section]].)}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>== Function Overloading ==</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>==Function Overloading==</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Sometimes, we want to create multiple functions that behave similarly but take different types as their parameters. For example, if we wanted to do the following:</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>Sometimes, we want to create multiple functions that behave similarly but take different types as their parameters. For example, if we wanted to do the following:</div></td></tr>
</table>TiManhttps://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_6_-_Functions&diff=4580&oldid=prevTiMan at 20:18, 15 August 20202020-08-15T20:18:58Z<p></p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 20:18, 15 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l274" >Line 274:</td>
<td colspan="2" class="diff-lineno">Line 274:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight>}}</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div></syntaxhighlight>}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>{{NavBar|HPL3/Scripting/AngelScript Fundamentals/Chapter <del class="diffchange diffchange-inline">3 </del>- <del class="diffchange diffchange-inline">Variables </del>and <del class="diffchange diffchange-inline">Types</del>|Chapter <del class="diffchange diffchange-inline">3 </del>- <del class="diffchange diffchange-inline">Variables </del>and <del class="diffchange diffchange-inline">Types</del>|HPL3/Scripting/AngelScript Fundamentals|AngelScript Fundamentals|HPL3/Scripting/AngelScript Fundamentals/Chapter <del class="diffchange diffchange-inline">5 </del>- <del class="diffchange diffchange-inline">Statements and Flow Control</del>|Chapter <del class="diffchange diffchange-inline">5 </del>- <del class="diffchange diffchange-inline">Statements and Flow Control</del>}}</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>{{NavBar|HPL3/Scripting/AngelScript Fundamentals/Chapter <ins class="diffchange diffchange-inline">5 </ins>- <ins class="diffchange diffchange-inline">Statements </ins>and <ins class="diffchange diffchange-inline">Flow Control</ins>|Chapter <ins class="diffchange diffchange-inline">5 </ins>- <ins class="diffchange diffchange-inline">Statements </ins>and <ins class="diffchange diffchange-inline">Flow Control</ins>|HPL3/Scripting/AngelScript Fundamentals|AngelScript Fundamentals|HPL3/Scripting/AngelScript Fundamentals/Chapter <ins class="diffchange diffchange-inline">7 </ins>- <ins class="diffchange diffchange-inline">Calling Other Scripts</ins>|Chapter <ins class="diffchange diffchange-inline">7 </ins>- <ins class="diffchange diffchange-inline">Calling Other Scripts</ins>}}</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:HPL3 Scripting]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:HPL3 Scripting]]</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:English]]</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>[[Category:English]]</div></td></tr>
</table>TiManhttps://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_6_-_Functions&diff=4578&oldid=prevTiMan: /* Function Overloading */2020-08-15T20:17:37Z<p><span dir="auto"><span class="autocomment">Function Overloading</span></span></p>
<table class="diff diff-contentalign-left" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #222; text-align: center;">Revision as of 20:17, 15 August 2020</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l258" >Line 258:</td>
<td colspan="2" class="diff-lineno">Line 258:</td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>HPL3 is smart enough to look at the type of the value you are passing as a parameter and automatically pick the correct function for that type.</div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>HPL3 is smart enough to look at the type of the value you are passing as a parameter and automatically pick the correct function for that type.</div></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>{{note|While you can overload functions by having different parameter types, you cannot overload a function by giving it a different return type.</div></td><td class='diff-marker'>+</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div>{{note|While you can overload functions by having different parameter types, you cannot overload a function by giving it a different return type. Doing so will result in an error when HPL3 tries to run your code. This is because when HPL3 looks at different functions, it only looks at the function name and the parameter types, ''not'' the return type. So while the above overload is perfectly legal, the following overload is not:</div></td></tr>
<tr><td class='diff-marker'>−</td><td style="color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div><del class="diffchange diffchange-inline"> </del>Doing so will result in an error when HPL3 tries to run your code. This is because when HPL3 looks at different functions, it only looks at the function name and the parameter types, ''not'' the return type. So while the above overload is perfectly legal, the following overload is not:</div></td><td colspan="2"> </td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"></td></tr>
<tr><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><syntaxhighlight lang="c++"></div></td><td class='diff-marker'> </td><td style="background-color: #f8f9fa; color: #222; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div><syntaxhighlight lang="c++"></div></td></tr>
</table>TiManhttps://wiki.frictionalgames.com/page?title=HPL3/Scripting/AngelScript_Fundamentals/Chapter_6_-_Functions&diff=4577&oldid=prevTiMan: Created page with "{{shortPageTitle}} {{AngelScriptGuideMenu}} So what happens if, as you're making a program, you have a lot of places where you are copying and pasting the same code over and o..."2020-08-15T20:16:55Z<p>Created page with "{{shortPageTitle}} {{AngelScriptGuideMenu}} So what happens if, as you're making a program, you have a lot of places where you are copying and pasting the same code over and o..."</p>
<p><b>New page</b></p><div>{{shortPageTitle}}<br />
{{AngelScriptGuideMenu}}<br />
So what happens if, as you're making a program, you have a lot of places where you are copying and pasting the same code over and over again? This is repetitive and wordy, not to mention if you find out you have to change that code then you have to change everywhere else that you used that code as well. This is where functions come in.<br />
<br />
A function is a construct in programming that takes a section of code and wraps it up into a package that you can then refer to any time you want to call that code. This is immensely useful if like in the hypothetical example you need to run a subsection of your code many times over the course of your program. It also helps you to organize your program, separating your code into easily describable and readable chunks.<br />
<br />
== Function Structure ==<br />
<br />
The structure of a function is similar to that of the statements that you learned about in the previous chapter:<br />
<br />
<syntaxhighlight lang="c++"><br />
void FunctionName()<br />
{<br />
// some code goes in here<br />
}<br />
</syntaxhighlight><br />
<br />
As you can see, a function consists of several parts.<br />
<br />
First, you have the <code>void</code> keyword. This says that the function does not return a value. (More on returning values in a bit.)<br />
<br />
Next, you have the function name. This name is an identifier, just like a variable name is an identifier, and it follows the same rules for what you can name it. And just like how you use variable names to get the values within variables, you use the function name when you want to run the function's code.<br />
<br />
Then you have a set of empty parentheses. I explain the importance of these parentheses in more detail in the "Parameters" section of this lesson, but know that having opening and closing parentheses is the bare minimum required.<br />
<br />
And finally, you have a code block, shown by the curly braces. Just like the statements in Lesson 4, the code inside the code block "belongs" to the function.<br />
<br />
So now you have a function defined (that's what it's called when you create a function), but how do you run that function's code? See this example:<br />
<br />
<syntaxhighlight lang="c++"><br />
// Elsewhere in your code<br />
FunctionName();<br />
</syntaxhighlight><br />
<br />
It's just that simple. All you need to do is write out the name of the function followed by those parentheses again. This is known as ''calling'' a function. It tells the program to jump to where you defined the function and run its code. Once it finishes that code, it will jump right back to this spot and continue on. <br />
<br />
{{note|Functions are everywhere, not just in AngelScript, but in many other popular languages as well. This is because most languages have a special function (usually called <code>Main</code>) that tells the computer where the program should start running. <br />
<br />
In fact, nearly all the code you write is going to be within a function somewhere. Remember the "Hello World" example from Lesson 1?<br />
<br />
<syntaxhighlight lang="c++"><br />
////////////////////////////<br />
// Run when entering map<br />
void OnEnter()<br />
{<br />
cLux_AddDebugMessage("Hello SOMA");<br />
}<br />
</syntaxhighlight> <br />
<br />
There's a function right there! The function is called <code>OnEnter</code>, and as the comment says, it is called by the HPL3 engine whenever the map is entered.}}<br />
<br />
== Parameters ==<br />
<br />
I promised that I would explain the importance of those parameters in greater detail, and I will do that now. The parentheses are for defining a function's ''parameters''.<br />
<br />
A parameter (also known as an argument) is how you pass information into the function from outside. You do this like so:<br />
<br />
<syntaxhighlight lang="c++"><br />
void FunctionName(int x)<br />
{<br />
int y = x + 5;<br />
}<br />
<br />
// ...<br />
// Elsewhere in your code<br />
<br />
FunctionName(2);<br />
</syntaxhighlight><br />
<br />
Inside the parentheses, we do something that looks a lot like declaring a variable. What this does is it defines a parameter called <code>x</code> which is of type <code>int</code>. When calling the function, we pass an <code>int</code> value inside the parameters, which is what the value of <code>x</code> will be inside the function.<br />
<br />
{{note|If you've dealt with functions in algebra, this might be a bit familiar:<br />
<br />
<syntaxhighlight lang=""><br />
f(x) = x + 5<br />
<br />
f(1) = (1) + 5<br />
f(2) = (2) + 5<br />
f(-12) = (-12) + 5<br />
</syntaxhighlight><br />
<br />
In the algebra function <code>f(x)</code>, when you put a number in for <code>x</code> on the left, you substitute that number everywhere where <code>x</code> appears on the right.}}<br />
<br />
It's also possible to define more than one parameter:<br />
<br />
<syntaxhighlight lang="c++"><br />
void FunctionName(int x, tString y)<br />
{<br />
tString z = y + x;<br />
}<br />
<br />
// ...<br />
// Elsewhere in your code<br />
<br />
FunctionName(5, "abc");<br />
</syntaxhighlight><br />
<br />
The parameters are separated by a comma, both when defining them in the function and when passing the values in calling the function. The order that the parameters appear stays the same - the <code>5</code> goes to <code>x</code>, and the <code>"abc"</code> goes to <code>y</code>.<br />
<br />
Keep in mind that you cannot pass a value of the wrong type to a function. If, for example, you were to do this:<br />
<br />
<syntaxhighlight lang="c++"><br />
FunctionName(5, 10);<br />
</syntaxhighlight><br />
<br />
That second parameter is defined as being a string, but <code>10</code> is not a string (not to be confused with <code>"10"</code>, which ''is'' a string). If you tried to do this, then HPL3 would consider it an error, and your code will not run.<br />
<br />
== Returning Values ==<br />
<br />
In addition to receiving information, a function can also be set up to give information back to the code where it was called:<br />
<br />
<syntaxhighlight lang="c++"><br />
int FunctionName()<br />
{<br />
return 5;<br />
}<br />
</syntaxhighlight><br />
<br />
There are a couple of new things here.<br />
<br />
First, the <code>void</code> from before has been changed to an <code>int</code>. This means that the function will be returning an <code>int</code> value when its code finishes running. You can define this type as anything you want - the function could return a <code>bool</code>, a <code>tString</code>, a <code>float</code>, or anything. (Or nothing, as in the case of our <code>void</code> examples before.)<br />
<br />
Second, we have a new keyword in <code>return</code>. What it does is it signals the function to stop running the code (similar to <code>break</code> from Lesson 4) and take the specified value as the return value. Back where the function was called, the code can take that value and do something with it:<br />
<br />
<syntaxhighlight lang="c++"><br />
int y = FunctionName();<br />
// y is 5<br />
</syntaxhighlight><br />
<br />
Keep in mind, however, that if you define that a function returns a value, you ''must'' return a value with <code>return</code> somewhere in the function code. If you had a function like this, for example:<br />
<br />
<syntaxhighlight lang="c++"><br />
int FunctionName()<br />
{<br />
int x = 5;<br />
}<br />
</syntaxhighlight><br />
<br />
Notice that the function was defined with <code>int</code> as the return type, but the code doesn't use <code>return</code> anywhere. This will be recognized as an error by HPL3.<br />
<br />
{{note|A function must always return a value, so beware of cases where you are putting your <code>return</code> inside conditional statements. Take the following example:<br />
<br />
<syntaxhighlight lang="c++"><br />
int FunctionName()<br />
{<br />
bool b = false;<br />
if (b)<br />
{<br />
return 5;<br />
}<br />
}<br />
</syntaxhighlight><br />
<br />
Even though there is a <code>return</code> in the function, it is inside an <code>if</code> block. The condition for the <code>if</code> statement is <code>b</code>, which happens to be false. In this case, the <code>if</code> code will not run, which means that the program will never reach the <code>return</code>. This leaves a path in the code in which a <code>return</code> doesn't get called, which will still result in an error. To prevent this, you need to make sure that your function has a <code>return</code> no matter which path your code decides to take:<br />
<br />
<syntaxhighlight lang="c++"><br />
int FunctionName()<br />
{<br />
bool b = false;<br />
if (b)<br />
{<br />
return 5;<br />
}<br />
else<br />
{<br />
return 0;<br />
}<br />
}<br />
</syntaxhighlight><br />
<br />
Now there is an <code>else</code> alongside the <code>if</code>, which guarantees that no matter what <code>b</code> is, the function will reach a <code>return</code> at some point.}}<br />
<br />
So now we have both parameters and a return type. Bringing them together, let's create a function that performs a useful action that we might want to do many times over the course of the program.<br />
<br />
<syntaxhighlight lang="c++"><br />
int AddFive(int x)<br />
{<br />
return x + 5;<br />
}<br />
</syntaxhighlight><br />
<br />
Now in our program, whenever we need to add 5 to a value, we can just do this:<br />
<br />
<syntaxhighlight lang="c++"><br />
int y = 5;<br />
int z = AddFive(y);<br />
// z is 10 (5 + 5)<br />
</syntaxhighlight><br />
<br />
In this code, <code>y</code> is passed to the function with a value of 5, so <code>x</code> within the function gets that value. The function then adds 5 to that value (resulting in 10) and returns it. Back outside the function, the return value is assigned to the variable <code>z</code>.<br />
<br />
{{note|If you were to take the above function and compress it down to the following form:<br />
<br />
<syntaxhighlight lang="c++"><br />
int AddFive(int)<br />
</syntaxhighlight><br />
<br />
This is called a function's ''signature''. It takes all the relevant information for identifying a function and discards everything else (return type, function identifier, parameter types). Note that the identifier for the parameter has been removed. This is because the identifier for the parameters are not actually necessary in determining the signature of a function - all it needs to know is how many parameters there are and what types they are.}}<br />
<br />
Note that in this example, <code>y</code> is passed as a parameter. However, nothing that happens inside the function will change the value of <code>y</code>. Even if we did this:<br />
<br />
<syntaxhighlight lang="c++"><br />
void AddFiveToY(int y)<br />
{<br />
y = y + 5;<br />
}<br />
<br />
// ...<br />
// Elsewhere in your code<br />
int y = 5;<br />
AddFiveToY(y);<br />
// y is still 5<br />
</syntaxhighlight><br />
<br />
This is because when you pass a variable as a parameter to a function, it does not pass the variable itself. Instead, it takes the value ''inside'' the variable and copies it into the parameter of the function. This is called ''passing by value'', and this happens whever you pass something as a parameter in AngelScript.<br />
<br />
{{note|The alternative to passing by value is called ''passing by reference'', in which you do actually pass the variable itself to the function. This concept is a bit more advanced for this tutorial series, but I do talk about it in the [[hpl3/community/scripting/angelscript_tutorial#appendixuncommon_angelscript_features|appendix section]].)}}<br />
<br />
== Function Overloading ==<br />
<br />
Sometimes, we want to create multiple functions that behave similarly but take different types as their parameters. For example, if we wanted to do the following:<br />
<br />
<syntaxhighlight lang="c++"><br />
void PrintIntToDebug(int i)<br />
{<br />
cLux_AddDebugMessage("Debug: " + i);<br />
}<br />
<br />
void PrintStringToDebug(tString s)<br />
{<br />
cLux_AddDebugMessage("Debug: " + s);<br />
}<br />
</syntaxhighlight><br />
<br />
As you can see, these two functions behave almost identically, but since they take two different types as parameters, they cannot be made into a single function. However, there is one thing you can do to make your code simpler - function overloading.<br />
<br />
When you overload a function, that means you are defining a function that has the same name as another function. Normally this isn't allowed, but it is when the second function has either a different type for its parameter(s) or a different number of parameters. In this case, we can use function overloading because our two functions have different types as parameters:<br />
<br />
<syntaxhighlight lang="c++"><br />
void PrintToDebug(int i)<br />
{<br />
cLux_AddDebugMessage("Debug: " + i);<br />
}<br />
<br />
void PrintToDebug(tString s)<br />
{<br />
cLux_AddDebugMessage("Debug: " + s);<br />
}<br />
</syntaxhighlight><br />
<br />
Now you can call both functions using the same identifier:<br />
<br />
<syntaxhighlight lang="c++"><br />
PrintToDebug(5);<br />
PrintToDebug("abc");<br />
</syntaxhighlight><br />
<br />
HPL3 is smart enough to look at the type of the value you are passing as a parameter and automatically pick the correct function for that type.<br />
<br />
{{note|While you can overload functions by having different parameter types, you cannot overload a function by giving it a different return type.<br />
Doing so will result in an error when HPL3 tries to run your code. This is because when HPL3 looks at different functions, it only looks at the function name and the parameter types, ''not'' the return type. So while the above overload is perfectly legal, the following overload is not:<br />
<br />
<syntaxhighlight lang="c++"><br />
// First function, minding its own business<br />
int SomeFunction(int x)<br />
{<br />
return 5;<br />
}<br />
<br />
// This function differs from the first only by return type, so it's not a legal overload<br />
tString SomeFunction(int x)<br />
{<br />
return "abc";<br />
}<br />
</syntaxhighlight>}}<br />
<br />
{{NavBar|HPL3/Scripting/AngelScript Fundamentals/Chapter 3 - Variables and Types|Chapter 3 - Variables and Types|HPL3/Scripting/AngelScript Fundamentals|AngelScript Fundamentals|HPL3/Scripting/AngelScript Fundamentals/Chapter 5 - Statements and Flow Control|Chapter 5 - Statements and Flow Control}}<br />
<br />
[[Category:HPL3 Scripting]]<br />
[[Category:English]]</div>TiMan