Difference between revisions of "Function"
esse quam videri
(Created page with " =Definition= =Relevance= =Explanation= =Resources= == See also == * Procedural Programming * Method * Operation ==Notes== ==External Links== Category...") |
|||
Line 1: | Line 1: | ||
=Definition= | =Definition= | ||
+ | A funciton is a block of reusable code that performs a single action. | ||
=Relevance= | =Relevance= | ||
+ | Functions are an essential part of programming. The key is to make them reusable to reduce typing the same (or similar) code. Another key is known as the "Single Responsibility Principle" which states each function should only have one responsibility (perform one specified action). | ||
− | = | + | =Example= |
+ | <syntaxhighlight lang ="csharp"> | ||
+ | string[] shuffleStringArray(string[] targetArray) // By making the array that will be shuffled an argument, we can send any array of string we want into this function. | ||
+ | { | ||
+ | for(int i = 0; i < targetArray.Length; i++) | ||
+ | { | ||
+ | // Code for shuffling here | ||
+ | } | ||
+ | return targetArray; // Return the shuffled array to wherever the function was called. | ||
+ | } | ||
+ | |||
+ | </syntaxhighlight> | ||
Revision as of 15:19, 22 August 2019
Definition
A funciton is a block of reusable code that performs a single action.
Relevance
Functions are an essential part of programming. The key is to make them reusable to reduce typing the same (or similar) code. Another key is known as the "Single Responsibility Principle" which states each function should only have one responsibility (perform one specified action).
Example
string[] shuffleStringArray(string[] targetArray) // By making the array that will be shuffled an argument, we can send any array of string we want into this function.
{
for(int i = 0; i < targetArray.Length; i++)
{
// Code for shuffling here
}
return targetArray; // Return the shuffled array to wherever the function was called.
}