Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sure! Here's an example of a more complex PowerShell function that takes two inputs and returns one output:
- ```PowerShell
- function Get-LongestWord ($string1, $string2) {
- $words1 = $string1.Split(" ")
- $words2 = $string2.Split(" ")
- $longestWord = ""
- foreach ($word in $words1 + $words2) {
- if ($word.Length -gt $longestWord.Length) {
- $longestWord = $word
- }
- }
- return $longestWord
- }
- ```
- This function takes two input parameters `$string1` and `$string2`, splits them into words using the `Split` method and assigns the resulting arrays to the variables `$words1` and `$words2`. The function then initializes an empty string variable `$longestWord` to store the longest word.
- The `foreach` loop iterates over all words in both arrays by concatenating them with the `+` operator. For each word, the loop checks if its length is greater than the length of the current longest word. If it is, the loop assigns the current word to the `$longestWord` variable.
- After all words have been processed, the `return` statement outputs the value of `$longestWord`.
- You can call this function like this: `Get-LongestWord "The quick brown fox" "jumps over"` and it will return `"jumps"`.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement