What is a User-Defined Function?
As your programs grow larger, you will find yourself writing the same code over and over. User-defined functions let you group a block of code under a name, then call it whenever you need it — avoiding repetition and making your code organized and reusable.

Defining a Function
# Basic function with no parameters def greet(): print("Hello! Welcome to Python.") # Call the function greet()
Functions with Parameters
Functions can accept inputs called parameters. This makes them flexible and reusable with different values.
# Function with parameters def greet(name): print("Hello, " + name + "!") # Call with different arguments greet("Tobi") greet("Sara")

Functions with Return Values
Functions can also compute a value and send it back to the caller using the return keyword.
# Function that returns a value def add(a, b): result = a + b return result # Use the returned value sum = add(5, 3) print(sum) # 8
Parts of a Function


.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)
.webp&w=3840&q=75)