swift
Parameters: sideLength: Double
The length of a side of the Pentagon
Returns: Returns the perimeter of a pentagon (a Double)
The function 'find-perimeter-pentagon' takes a given length of one side of a pentagon as an input and calculates the perimeter by multiplying it by 5.
Hello there, fellow programmer! We're keeping it cool and casual here as we dive into a swift journey of coding a function. This function will be designed to calculate the perimeter of a pentagon. You'll find a simplified, step-by-step programming guide below that we hope will inspire and enlighten your coding experience. Enjoy, and let the coding magic happen!
The first step in solving any problem is to understand it. In this case, we want to create a function that will calculate and return the perimeter of a pentagon. The perimeter of a pentagon can be found using the formula: Perimeter = 5 * side
where 'side' is the length of a side of the pentagon.
Here is the initial setup for our function:
func findPerimeterPentagon(side: Double) -> Double {
// calculation will happen here
}
In Swift, functions are defined using the func
keyword. Our function is taking one parameter 'side' of type Double and will return a value of type Double.
Now, let's implement the formula for the perimeter of a pentagon inside our function:
func findPerimeterPentagon(side: Double) -> Double {
let perimeter = 5 * side
return perimeter
}
In this step, we declared a variable 'perimeter' and assigned it the calculated value of the pentagon's perimeter. The 'return' keyword is then used to specify the result of the function.
Once we have our function ready, we need to test it. Let's see it in action:
var result = findPerimeterPentagon(side: 10)
print(result) // expected output 50.0
This will calculate the perimeter of a pentagon of side 10, and the expected output should be 50.0
Until now, our function is unable to handle any errors. For instance, the side-length cannot be negative. So, we add some error handling to account for that:
func findPerimeterPentagon(side: Double) -> Double {
if (side < 0) { print("Error: side length can't be negative.")
return 0
}
let perimeter = 5 * side
return perimeter
}
Here, we add an error checking statement. If the side is less than zero, the function will print an error message and return 0.
Here is the final code:
func findPerimeterPentagon(side: Double) -> Double {
if (side < 0) { print("Error: side length can't be negative.")
return 0 }
let perimeter = 5 * side
return perimeter
}
We now have a swift function that takes a side length as an input, calculates the perimeter if the side length is positive, and handles the error when the side length is less than zero.
This concept calculates the perimeter of a pentagon using its side length
Learn moreThe math principle behind this function relies on the geometric property of a pentagon - a polygon with 5 equal sides. The perimeter of such a shape is calculated by multiplying the length of one side by the total number of sides - 5 in this case. In Swift, you can use the multiplication operator `*` to do this.
Learn more