0Pricing
Mojo Academy · レッスン

def関数の記述

Pythonスタイルで動的かつ柔軟に記述します

「def関数の記述」はCoddyKit上の無料Mojo Academyレッスンです。 これはレッスン1/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはMojo Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Mojo Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Functions Bundle Logic

A function packages a piece of work under a name so you can run it whenever you like, instead of repeating code. 🧩

Meet the def Keyword

The def keyword defines a function the Python way: relaxed about types and easy to write while you explore an idea.

def greet():
    print("Hello from Mojo")

Calling Your Function

Defining a function does not run it. You call it by writing its name followed by parentheses, and the body executes.

greet()

Passing in Arguments

Put parameters in the parentheses to feed data in. Here name becomes a value you can use inside the body.

def greet(name):
    print("Hi", name)

Types Are Optional in def

In a def you may skip type annotations entirely. Mojo lets the value flow dynamically, just like Python does.

def double(x):
    return x * 2

Returning a Value

The return keyword hands a result back to whoever called the function, ending the function right there.

def add(a, b):
    return a + b

Arguments Are Mutable Copies

Inside a def, arguments act like local copies you can reassign freely without touching the caller value.

def bump(x):
    x = x + 1
    return x

def Feels Like Python

If you know Python, def is instantly familiar: same syntax, same forgiving feel, now running on Mojo.

Great for Quick Experiments

Because it asks for so little, def shines when prototyping or scripting, where speed of writing matters most.

Indentation Defines the Body

Everything indented under the def line is the function body. Mojo, like Python, uses indentation to group code.

def info():
    print("line one")
    print("line two")

Docstrings Document Intent

A string on the first body line is a docstring: a short note describing what the function does for future readers.

def area(r):
    "Return circle area."
    return 3.14 * r * r

Quick Check

Think about how def handles type annotations.

Recap

You met def: the Python-style function in Mojo. Types are optional, arguments are mutable copies, and it is perfect for quick, forgiving code. 🎯

よくある質問

「def関数の記述」レッスンは無料ですか?

はい。「def関数の記述」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Mojo Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Mojo Academyコースには全4レッスンが含まれています。

「def関数の記述」で何を学びますか?

Pythonスタイルで動的かつ柔軟に記述します ブラウザで直接実行するハンズオンコードでMojo Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Mojo Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのMojo Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン1/4です。

「def関数の記述」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このMojo Academyレッスンでコードを書いて実行できますか?

はい。すべてのMojo Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. def関数の記述
  2. fn関数の記述
  3. それぞれを使う場面
  4. 戻り値の型とNone
← Mojo Academyに戻る