User Defined Functions

A function is a named, reusable block of code that performs a specific task. Instead of repeating the same code, you can define it once as a function and call it whenever needed.

Defining a Function

def greet(name):
    print("Hello,", name)

greet("Meera")

Functions with Multiple Parameters

def add_numbers(a, b):
    print("Sum:", a + b)

add_numbers(5, 10)

Default Parameter Values

def greet(name, greeting="Hello"):
    print(greeting + ",", name)

greet("Aditi")
greet("Rahul", "Good Morning")
Breaking a data science script into small, well-named functions, such as one for loading data and another for cleaning it, makes code far easier to read, test, and reuse.

Coming Up

Functions can also send a value back to the code that called them. The next topic covers exactly how this works, through the concept of the return statement.

Ready to Master Data Science?

Join Uncodemy's Data Science Course and build real, job-ready skills with expert mentors.

Explore Course