swift

find-power()

Parameters: base: Double, exponent: Double

base is the number being raised, exponent is the power it's raised to

Returns: Returns the base raised to the exponent as a Double

This function takes two integers as input parameters and calculates the power of the first number to the second number.

variables
functions
loops
arithmetic operations
Medium dificulty

Leveraging Swift to Create a Power Function

Hello there, fellow programmer! In this honest and straightforward post, we'll delve deep into how to write a potent function in Swift, namely `find-power`. The steps to come will guide you through this process, avoiding any confusing talk and sticking to the core concepts of functional programming within Swift. This post aims to hand you the knowledge and confidence needed to draft coding solutions in an efficient, Swift-y way! Let's get started.

Step 1: Function Definition

The first thing we want to do is define our function. We will call it findPower and it will take two arguments: base and power. base is going to be our number that we want to raise to a power, and power is the power we want to raise base to. We are starting simply with the structure of the function, so it does not do anything yet.

func findPower(base: Int, power: Int) {

}

Step 2: Conditional for zero power

We will need to handle a case when the power input is zero since any number to the power of zero is equal to one. We insert an if statement at the start of our function for this.

func findPower(base: Int, power: Int) {
    if power == 0 {
        return 1
    }
}

Step 3: Initial variable setup

We define a variable called result and set it equal to base. For multiplication, this will be our initial value which we will build upon.

func findPower(base: Int, power: Int) -> Int {
    if power == 0 {
        return 1
    }
    var result = base
}

Step 4: Loop to compute power

Now we insert a for loop that will iterate over the range from 1 to the power (excluding the power itself since we've already counted it as the initial result). This will consistently multiply the base number by itself.

func findPower(base: Int, power: Int) -> Int {
    if power == 0 {
        return 1
    }
    var result = base
    for _ in 1..<power {
        result *= base
    }
}

Step 5: Return result

Finally, after multiplying the base number by itself for the power number of times, we return result.

func findPower(base: Int, power: Int) -> Int {
    if power == 0 {
        return 1
    }
    var result = base
    for _ in 1..<power {
        result *= base
    }
    return result
}

This final function successfully calculates a base number raised to the power of an exponent.

Learn function in:

Power Calculation

Calculating the power of a number raised to another number.

Learn more

Mathematical principle

The function raises a base number `b` to the power of an exponent `n`, performed by `b * b * ... * b` (`n` times). If `n` is zero, the result is `1` (since any number to the power of zero is `1`). If `n` is negative, the result is `1 / b^n`.

Learn more