0
0
Swiftprogramming~3 mins

Why Omitting argument labels with _ in Swift? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if you could make your function calls as simple as talking to a friend?

The Scenario

Imagine you have a function that takes many inputs, and every time you call it, you have to write the name of each input. It feels like filling out a long form repeatedly, even when the names are obvious.

The Problem

Writing argument labels every time is slow and makes your code bulky. It's like repeating yourself over and over, which can cause mistakes and make your code harder to read.

The Solution

By using _ to omit argument labels, you can call functions more simply and cleanly. It's like skipping unnecessary words in a conversation to get straight to the point.

Before vs After
Before
func greet(person name: String) { print("Hello, \(name)!") }
greet(person: "Anna")
After
func greet(_ name: String) { print("Hello, \(name)!") }
greet("Anna")
What It Enables

This lets you write shorter, clearer function calls that feel natural and easy to read.

Real Life Example

Think of a calculator app where you just enter numbers and operations quickly without typing extra labels every time.

Key Takeaways

Writing argument labels every time can be repetitive and clutter code.

Using _ lets you skip labels for simpler calls.

This makes your code cleaner and easier to read.