Last active
June 21, 2017 15:23
-
-
Save mhull/1ee338b40e2bc73ba52e77df409912ae to your computer and use it in GitHub Desktop.
Exploring the definition of a function in math and how it can inform my understanding of functions in programming
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
<?php | |
/** | |
* This function has no inputs, so it is not a function from a mathematical | |
* perspective. Furthermore, the function is not predictable because it depends on | |
* the state of the underlying program | |
*/ | |
function say_hello() { | |
$name = 'World'; | |
/** | |
* Note we are using external state to alter our function's behavior. This logic may | |
* be better placed elsewhere in order to keep the function more consistent and predictable | |
*/ | |
if( user_is_logged_in_right_now() ) { | |
$user = get_logged_in_user(); | |
$name = $user->first_name; | |
} | |
return "Hello, {$name}"; | |
} |
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
<?php | |
/** | |
* This function achieves the same result in a more predictable way and does | |
* qualify as a function in a mathematical sense | |
*/ | |
function say_hello_to( $name ) { | |
return "Hello, {$name}"; | |
} |
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
<?php | |
$my_array = [ | |
'foo' => 'bar', | |
'baz' => 'bat', | |
]; |
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
<?php | |
/** | |
* For the input (i.e. key) 'baz', this changes the output (i.e. value) from 'bat' to 'bae' | |
*/ | |
$my_array['baz'] = 'bae'; |
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
<?php | |
/** | |
* Again, we have 'bae' replacing 'bat' as the value associated with the key 'bae' | |
*/ | |
$my_array = [ | |
'foo' => 'bar', | |
'baz' => 'bat', | |
'baz' => 'bae', | |
]; |
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
<?php | |
$my_array = [ | |
'foo' => 'bar', | |
'baz' => [ 'bat', 'bae' ] | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment