1.5 Functions and Return Values

Loading content...

1.5 Functions and Return Values

Module 5 — Functions & Return Values 🔧 | Python for ML 🐍 Module 5 • Python for Machine Learning 🔧 Functions & Return Values Build reusable blocks of code — the building blocks of every ML pipeline, every app, and every program you'll ever write. Think of a pizza oven 🍕. You put in raw dough + toppings (the inputs). The oven does its thing (the process). Out comes a finished pizza (the output). You don't rebuild the oven every time you want pizza. You reuse it — same oven, different toppings! 🎯 A function in Python works exactly the same way: 📥 Inputs go in (called arguments) ⚙️ Code inside processes them 📤 A result comes back (called return value) ♻️ You can reuse it as many times as you want Function = Reusable Machine Write once → call many times! ♻️ 📖 Definition: A function is a reusable block of code that performs a specific task. You define it once with def, then call it whenever you need it. Functions make your code organized, reusable, and easier to debug. 🧩 🤖 ML Connection: Every ML model is essentially a function! model.predict(features) takes input data and returns predictions. Functions are the backbone of machine learning. 🎯 A function lets you write code once and reuse it multiple times with different inputs. Before diving in, here are the essential terms you'll use throughout this module 📚: A named, reusable block of code Variable in the function definition (placeholder) def greet(name): — name is a parameter Actual value passed when calling the function greet("Alice") — "Alice" is an argument The result the function sends back Using the function by its name + parentheses Keyword to define (create) a function Documentation string describing the function Where a variable can be accessed Local (inside) vs Global (outside) A small anonymous (unnamed) function What keyword is used to create (define) a function in Python? ⚙️ Defining Functions with def Creating a function in Python is like writing a recipe 📋. You write it once, then "cook" it whenever you want. 🧱 The Anatomy of a Function The def keyword — tells Python "I'm creating a function!" Function name — what you'll call it later (use snake_case: calculate_tax) Parentheses () — hold the parameters (inputs) Colon : — marks the start of the function body Indented body — the code that runs when you call the function (4 spaces) return statement — sends a result back to the caller (optional) # 📋 Step 1: DEFINE the function (write the recipe) def greet(name): # 'name' is a parameter message = f"Hello, {name}! 👋" return message # Send result back # 📞 Step 2: CALL the function (cook the recipe) result = greet("Alice") # "Alice" is an argument # ♻️ Reuse with different arguments! 💡 Key pattern: Define ONCE → Call MANY times. The function remembers its code. Each call runs the same code with different inputs. 🔄 🚫 Function Without Parameters Not every function needs inputs! Some just do a fixed task 🎯: def say_hello(): # No parameters! say_hello() # Call with empty parentheses say_hello() # Call again — same output Complete the function definition to create a function named add that takes two parameters a and b: You must always use parentheses () when calling a function, even if there are no arguments. People often confuse these two — but the difference is simple 🎯: 📌 Parameter: The placeholder name in the function definition. It's like a labeled empty slot. Think: "parking spot label." 🅿️ 📌 Argument: The actual value you pass when calling the function. It's what fills the slot. Think: "the car that parks." 🚗 # Different argument, same parameter 🧠 Easy trick to remember: Parameter = Placeholder (in definition). Argument = Actual value (in call). 💡 Functions can take multiple inputs, separated by commas: def introduce(name, age, city): print(f"I'm {name}, {age} years old, from {city}") # Positional arguments — order matters! introduce("Alice", 25, "NYC") # Keyword arguments — order doesn't matter! introduce(city="London", name="Bob", age=30) I'm Alice, 25 years old, from NYC I'm Bob, 30 years old, from London In def calculate_area(length, width):, what are length and width? 📤 Return Values — Getting Results Back The return statement is like a delivery truck 🚚 — it carries the result back to whoever called the function. 🔑 Three important rules about return 1️⃣ return sends a value back to where the function was called 2️⃣ return immediately stops the function — no code after it runs 3️⃣ If there's no return, the function returns None by default ⚠️ Common trap: print() and return are NOT the same! print() shows text on screen. return sends a value back to your code. You can use a returned value; you can't use a printed value. return a + b # Sends value BACK # Capture the returned value print(total) # 10 print(add(3, 7) * 2) # 20

Subject: Python for Machine Learning | Chapter: Chapter 1: Python Essentials

1.4 Loops (for, while) Chapter 1: Python Essentials 1.6 Lists and List Comprehensions
Topics
Chapter 1: Python Essentials
Chapter 2: Python for Data Science
© Copyright © 2020 ALGOPK . All Rights Reserved.