← Python for Beginners
0 of 27 lessons done0%

Functions · Lesson 21 of 27

Functions

A function is a named block of code you can run again and again. You create it with def:

def greet():
    print("Hello!")

greet()    # Hello!
greet()    # Hello!

Functions can take inputs, called parameters:

def greet(name):
    print(f"Hello, {name}!")

greet("Riya")    # Hello, Riya!
greet("Aman")    # Hello, Aman!

Defining a function does nothing by itself. The code inside runs only when you call it with ().

Your task

Write a function greet(name) that prints Hello, <name>!.

For example, greet("Riya") should print Hello, Riya!.