LoginSignup
0
0

[コーディング演習3] Functions Part 3 - 103 - iOS & Swift - The Complete iOS App Development Bootcamp

Posted at

[コーディング演習】関数 その3
出力を持つことができる関数について学んだことを使って、isOdd(n: Int)という関数を作成します。

この関数に渡された任意の整数に対して、例えば次のようにします。

isOdd(n: 5)
この関数は、その数が奇数であるかどうかをテストします。奇数であれば、trueを出力し、そうでなければfalseを出力するはずです。これはブール値であり、文字列ではありません。

ヒント:余り演算子を使って、何かがきれいに分割できるかどうかをチェックすることができます。

解答コード

func isOdd(n: Int) -> Bool {
    if n%2 == 1 {
        return true
    } else {
        return false
    }
}
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0