Finding a function that returns the larger of two values is a challenging task. It requires you to know how much the second value is longer than the first, or you would not call the function.
Using a function can be tedious at times as you have to remember its name, and how much more of something the second value is. This article will talk about some useful functions that return more of something than the first value.
When using a function-as-a-resource type of article, it is important to always have a backup plan in case something goes wrong. Having a second piece of paper or phone screen frozen at the exact same moment when things go down the pan is not going to happen, because both pieces of material will be used up.
Calculate the length of the second string
The length of a string is measured by its length-of-the-first-string parameter.
Using the example of our phone number, the number can be described as follows:
Its length is two parameters, one for the area code and one for the phone number.
The area code is 786, so the second string must be set to an equal of that. The phone number must be set to a 7, so an equal of that would be 6 characters.
If the first string is longer, return that value
If the second string is longer, return the smaller of the two. These functions are called function max and function min, respectively.
function max takes two parameters and returns the larger of those two. For example, to find the largest value in a list of values, you would use function max(value1, value2).
function min takes one parameter and returns the smaller of those two. For example, to find the smallest value in a list of values, you would use function min(value1, value2).
These functions can be used in conjunction with array operations to find the less or remainder when two values are divided into pieces. For example, if we wanted to find how many times an item in our list is greater than or equal to another item, we could use these functions with log(merit) as our measure.
If the second string is longer, return that value
If the second string is longer, return that value as the larger of the two strings. This example returns a string with a
When searching for phone numbers, you may want to limit your results to those with a similar name, which is why this function returns the shorter of the two names.
Compare each character from each string
Whence the character came from and where it went to, these details make your function more powerful.
For example, let’s say we wanted to find the length of all names. We would create a function called max that returns the bigger of the two characters.
The shorter of the two characters would go into an variables as c1 and c2, while the longer one would go into an variables as c3 and c4.
This is a good example to show how parameters and return values can balance out functions.
Return the larger string
When there are two string parameters, or two strings to return, we can return the larger of the two. This is useful when we need to update a large value in one of the strings, but have to make sure to update the other one as well.
The larger of two strings is called an alias. When we want to refer to one string with an alias, we can use an ellipsis (…) after it.
The trick is making sure our function gets its input and output parameters correct.
Return the larger character value
8) Keep track of which character you are comparing
9) Compare all but the last character
10) Check if both strings are equal
11) Check if both strings are empty
12) Return 0
13) Return an error message
14) Test your function with different sets of arguments
- Give it short strings
- Give it long strings
- Give it short and long strings
The function max shown in bullet point 14) returns an integer, not a character. But the function does check for characters in its parameters, so it can still return a value of greater than or equal to 0.
There are plenty of applications for this function, such as finding the largest billfold you can purchase.
Max Length Inputs?
|Max Length Inputs?|
Write a function maximum that has two integer parameters and returns the larger of the two.
If the input parameter is less than or equal to the output parameter, return Infinity. If not, return the smaller of the two values.
If both parameters are integers, return the corresponding integer value for either parameter.