Getting Started with Python (Part 10): Using Functions

Published: (December 23, 2025 at 08:09 AM EST)
2 min read
Source: Dev.to

Source: Dev.to

Using Functions

In this article, you’ll learn about functions. By using functions, you can reuse specific processes and write cleaner, more maintainable code.

You can define a function by using the def statement. After defining a function, you must call (execute) it to run the code inside.

def function_name():
    """Function description"""
    process

# Call the function
function_name()

Example

# Define a function
def say_hello():
    """A function that prints a greeting"""
    print("Hello, Python!!")

# Call the function
say_hello()  # Hello, Python!!

# A defined function can be called as many times as you like
say_hello()  # Hello, Python!!
say_hello()  # Hello, Python!!
say_hello()  # Hello, Python!!

Using Arguments

By using arguments, you can create more flexible functions.

def function_name(argument):
    """Function description"""
    process

Example

# Define a function with one argument
def say_hello(lang):
    """A function that greets in a chosen language"""
    print("Hello, " + lang + "!!")

# Call the function
say_hello("Python")   # Hello, Python!!
say_hello("Rust")     # Hello, Rust!!
say_hello("Kotlin")   # Hello, Kotlin!!
say_hello("Swift")    # Hello, Swift!!

Using Multiple Arguments

You can define as many arguments as you need.

def function_name(arg1, arg2, arg3, ...):
    """Function description"""
    process

Example

# Define a function with two arguments
def say_hello(greet, lang):
    """A function that combines a greeting with a language"""
    print(greet + ", " + lang + "!!")

# Call the function
say_hello("Hello", "Python")          # Hello, Python!!
say_hello("Ciao", "C/C++")            # Ciao, C/C++!!
say_hello("Bonjour", "Java")          # Bonjour, Java!!
say_hello("Namaste", "JavaScript")    # Namaste, JavaScript!!

Using Return Values

By using return, a function can perform some processing and return the result to the caller. If a function does not use return, it is treated as returning None.

def function_name(arg1, arg2, arg3, ...):
    """Function description"""
    process
    return result

Example – Doubling a Number

def calc_double(num):
    """A function that returns double the value"""
    return num * 2

print(calc_double(10))  # 20
print(calc_double(20))  # 40
print(calc_double(30))  # 60

Example – Summing Two Numbers

def calc_total(num1, num2):
    """A function that returns the sum of two numbers"""
    return num1 + num2

print(calc_total(10, 20))  # 30
print(calc_total(30, 40))  # 70
print(calc_total(50, 60))  # 110

Arguments and return values are often used together, so make sure you understand both concepts well.

What’s Next?

Thank you for reading! In the next article, we’ll learn about modules.

“Getting Started with Python (Part 8): Using Modules”

Stay tuned! 🚀

Back to Blog

Related posts

Read more »