swift

findPerimeterIsoscelesTriangle()

Parameters: base: Double, side: Double

base and side are lengths of triangle in any unit of measurement

Returns: Returns the perimeter of isosceles triangle (Double type result)

This Swift function allows for calculation of the perimeter of an isosceles triangle given the length of the base and the length of the equal sides.

functions
arithmetic operations
variables
parameters
Medium dificulty

Creating a Swift Function to Compute the Perimeter of an Isosceles Triangle.

Greetings Programmer! In this blog post, we are going to dive into the realm of geometrical problem solving. Concretely, our aim will be to construct a function, utilizing Swift, that is capable of finding the perimeter of an isosceles triangle. Although it may seem daunting at first, we'll guide you through every step of the way. Stay tuned to follow along with this programming journey!

Step 1: Understanding the Problem

It is crucial to understand the problem in order to solve it. We are tasked to find the perimeter of an isosceles triangle. By definition, an isosceles triangle has two sides of equal length and one side of different length. The perimeter of a triangle is calculated as the sum of its three sides. Hence, in our case, it is twice the length of the equal side plus the length of the base.

var equalSideLength = 5
var base = 6

var result = 0.0 

Step 2: Setting Up the Function

The function, named findPerimeterIsoscelesTriangle, accepts two parameters: equalSideLength and base, and returns a Double.

func findPerimeterIsoscelesTriangle(equalSideLength: Int ,base: Int) -> Double {
var result = 0.0 

// next steps will go here 

return result
} 

Step 3: Implementing the Calculation

We calculate the perimeter according to the formula: 2 * equalSideLength + base.

func findPerimeterIsoscelesTriangle(equalSideLength: Int ,base: Int) -> Double {
 var result = 0.0 
 result = 2.0 * Double(equalSideLength) + Double(base) 
 return result
} 

Step 4: Testing the Function

We need to test our function by running it with some test cases.

findPerimeterIsoscelesTriangle(equalSideLength: 5, base: 6) // 16.0
findPerimeterIsoscelesTriangle(equalSideLength: 7, base: 10) // 24.0 

Step 5: Conclusion

In this code, we've implemented a function to calculate the perimeter of an isosceles triangle. It receives the length of the equal sides and the base as input and returns the perimeter. A good practice is to test your functions with different input cases to ensure they're working as expected.

Here is the full implementation:

func findPerimeterIsoscelesTriangle(equalSideLength: Int ,base: Int) -> Double {
 var result = 0.0 
 result = 2.0 * Double(equalSideLength) + Double(base) 
 return result
} 

Learn function in:

Perimeter of Isosceles Triangle

Find the perimeter of an isosceles triangle with known base and sides.

Learn more

Mathematical principle

This function utilizes the mathematical principle of the perimeter calculation for an isosceles trapezium, i.e., `Perimeter = base + 2*equalSide`. Both `base` and `equalSide` are parameters of the function and assumed to be positive real numbers.

Learn more