0Pricing
Elixir & Phoenix: Scalable Backend Development · บทเรียน

ฟังก์ชัน โมดูล และการส่งต่อข้อมูล

เรียนรู้การกำหนดฟังก์ชัน จัดระเบียบโค้ดเป็นโมดูล และใช้ตัวดำเนินการไปป์เพื่อแปลงข้อมูลอย่างชัดเจน

ฟังก์ชัน โมดูล และการส่งต่อข้อมูล เป็นบทเรียน Elixir & Phoenix: Scalable Backend Development ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Elixir & Phoenix: Scalable Backend Development และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Introduction to Elixir Functions

In Elixir, functions are the building blocks of your programs. They take inputs, perform operations, and return outputs.

Elixir functions are designed to be pure: they don't change data outside their scope and always produce the same output for the same input. This makes your code predictable and easier to reason about.

Defining Public Functions

You define public functions using the def keyword. These functions can be called from anywhere within your application, provided they are part of a module.

Let's create a simple function that adds two numbers:

defmodule MathOperations do
  def add(a, b) do
    a + b
  end
end

IO.puts "Sum of 5 and 3: #{MathOperations.add(5, 3)}"

Defining Private Functions

Sometimes you need helper functions that should only be used internally within a module. For these, you use the defp keyword.

Private functions cannot be called directly from outside their defining module. This helps encapsulate logic.

defmodule SecretOperations do
  defp secret_add(a, b) do
    a + b + 10 # A 'secret' addition
  end

  def public_operation(x, y) do
    secret_add(x, y)
  end
end

IO.puts "Result of public operation: #{SecretOperations.public_operation(2, 3)}"
# SecretOperations.secret_add(1, 2) would cause an error if called directly outside the module.

Function Arity and Clauses

Arity refers to the number of arguments a function takes. Elixir allows you to define multiple function clauses with the same name but different arities or different patterns for arguments.

This enables powerful pattern matching (covered in a previous lesson) to handle different scenarios cleanly within a single function definition.

defmodule Greeter do
  def greet("Alice"), do: "Hello, Alice!"
  def greet(name), do: "Hi there, #{name}!"
end

IO.puts Greeter.greet("Alice")
IO.puts Greeter.greet("Bob")

Anonymous Functions

Anonymous functions (also called lambdas) are functions without a name. They are often used when you need to pass a function as an argument to another function, or for short, one-off operations.

You define them using the fn keyword.

adder = fn a, b -> a + b end
multiplier = fn x, y -> x * y end

IO.puts "Anonymous add: #{adder.(10, 20)}"
IO.puts "Anonymous multiply: #{multiplier.(4, 5)}"

Organizing Code with Modules

Modules in Elixir are containers for organizing related functions and data. They help prevent naming conflicts and make your code more structured and maintainable.

You define a module using the defmodule keyword. It's common practice for module names to start with an uppercase letter, following Elixir's naming conventions.

Creating Your First Module

Let's put our add function inside a module called Calculator. This makes it clear that the add function is part of a set of calculator-related operations.

defmodule Calculator do
  def add(a, b) do
    a + b
  end

  def subtract(a, b) do
    a - b
  end
end

IO.puts "Module defined!"

Calling Module Functions

To call a function defined within a module, you use the module's name, followed by a dot (.), and then the function name with its arguments.

This explicit naming helps you understand where a function comes from.

defmodule Calculator do
  def add(a, b), do: a + b
  def subtract(a, b), do: a - b
end

IO.puts "10 + 5 = #{Calculator.add(10, 5)}"
IO.puts "10 - 5 = #{Calculator.subtract(10, 5)}"

Introducing the Pipe Operator `|>`

The pipe operator |> is one of Elixir's most beloved features. It makes sequences of function calls incredibly readable, flowing data from one function to the next.

Think of it as taking the result of the expression on the left and passing it as the first argument to the function call on the right.

Pipelining in Action

Instead of nesting function calls like function_c(function_b(function_a(data))), the pipe operator lets you write it as data |> function_a() |> function_b() |> function_c().

This reads like a series of steps, making complex data transformations much clearer.

defmodule DataProcessor do
  def double(x), do: x * 2
  def add_five(x), do: x + 5
  def to_string(x), do: to_string(x)
end

result = 10 
         |> DataProcessor.double() 
         |> DataProcessor.add_five() 
         |> DataProcessor.to_string()

IO.puts "Processed result: #{result}"

Check Your Understanding

Which of the following statements about Elixir functions, modules, and the pipe operator are true?

Recap: Functions, Modules & Pipes

Great job! In this lesson, you learned the essentials of organizing and transforming data in Elixir:

  • How to define public (`def`) and private (`defp`) functions.
  • The importance of modules (`defmodule`) for structuring your code.
  • How the pipe operator (`|>`) dramatically improves readability for sequential data transformations.

These concepts are fundamental to writing clear, maintainable, and functional Elixir applications. Keep practicing!

คำถามที่พบบ่อย

บทเรียน “ฟังก์ชัน โมดูล และการส่งต่อข้อมูล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “ฟังก์ชัน โมดูล และการส่งต่อข้อมูล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Elixir & Phoenix: Scalable Backend Development ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Elixir & Phoenix: Scalable Backend Development มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “ฟังก์ชัน โมดูล และการส่งต่อข้อมูล”

เรียนรู้การกำหนดฟังก์ชัน จัดระเบียบโค้ดเป็นโมดูล และใช้ตัวดำเนินการไปป์เพื่อแปลงข้อมูลอย่างชัดเจน คุณปฏิบัติ Elixir & Phoenix: Scalable Backend Development ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Elixir & Phoenix: Scalable Backend Development หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Elixir & Phoenix: Scalable Backend Development บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน

บทเรียน “ฟังก์ชัน โมดูล และการส่งต่อข้อมูล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Elixir & Phoenix: Scalable Backend Development นี้ได้ไหม

ได้ บทเรียน Elixir & Phoenix: Scalable Backend Development ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. ฟังก์ชัน โมดูล และการส่งต่อข้อมูล
  2. การทำงานกับคอลเลกชันที่แจกแจงได้
  3. การเรียกซ้ำและฟังก์ชันลำดับสูง
  4. การประเมินแบบขี้เกียจด้วยโมดูล Stream
← กลับไปที่ Elixir & Phoenix: Scalable Backend Development