Instantly share code, notes, and snippets.

Embed
What would you like to do?
Exploring the definition of a function in math and how it can inform my understanding of functions in programming
<?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}";
}
<?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}";
}
<?php
$my_array = [
'foo' => 'bar',
'baz' => 'bat',
];
<?php
/**
* For the input (i.e. key) 'baz', this changes the output (i.e. value) from 'bat' to 'bae'
*/
$my_array['baz'] = 'bae';
<?php
/**
* Again, we have 'bae' replacing 'bat' as the value associated with the key 'bae'
*/
$my_array = [
'foo' => 'bar',
'baz' => 'bat',
'baz' => 'bae',
];
<?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