1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python 自分で関数を作ってみた : 階乗

Posted at

目的

Pythonの基本文法を得ること(初心者向け)

内容

関数の概要を学ぶ。自分で関数を作って動かしてみる。

def文で関数を作る

  • 今回は「階乗」を目的とした関数を作成することにします。
def kaijo(n):
    if n == 1:
        return n
    return n * kaijo(n-1)
  • 関数kaijoを作りました。
num = int(input("階乗したい数字 : "))
print(num,"の階乗は",kaijo(num))
  • numに階乗したい数字を入力し、print文の中で関数呼び出しをします。
  • 関数kaijoの引数に入力した数値を渡します。
階乗したい数字 : 5
5 の階乗は 120
  • ちゃんと表示されました。階乗って普通にコード書くときにどうやるんだっけ。。。
1
0
2

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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?