DEV Community

Kajiru
Kajiru

Posted on

Getting Started with Python (Part 10): Using Functions

Using Functions

In this article, you’ll learn about functions.

By using functions, you can reuse specific processes and write cleaner, more maintainable code.


Using Functions

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()
Enter fullscreen mode Exit fullscreen mode

In the following example, we define a function called say_hello and then execute it.

# 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!!
Enter fullscreen mode Exit fullscreen mode

Using Arguments

By using arguments, you can create more flexible functions.

def function_name(argument):
    """ Function description """
    process
Enter fullscreen mode Exit fullscreen mode
# 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!!

# Try greeting in different languages
say_hello("Rust")     # Hello, Rust!!
say_hello("Kotlin")   # Hello, Kotlin!!
say_hello("Swift")    # Hello, Swift!!
Enter fullscreen mode Exit fullscreen mode

Using Multiple Arguments

You can define as many arguments as you need.

def function_name(arg1, arg2, arg3, ...):
    """ Function description """
    process
Enter fullscreen mode Exit fullscreen mode
# 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!!

# Try different greetings and languages
say_hello("Ciao", "C/C++")        # Ciao, C/C++!!
say_hello("Bonjour", "Java")      # Bonjour, Java!!
say_hello("Namaste", "JavaScript")# Namaste, JavaScript!!
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Here is an example of a function that doubles a given number and returns it.

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

# Execute
print(calc_double(10))  # 20
print(calc_double(20))  # 40
print(calc_double(30))  # 60
Enter fullscreen mode Exit fullscreen mode

Here is another example of a function that returns the sum of two numbers.

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

# Execute
print(calc_total(10, 20))  # 30
print(calc_total(30, 40))  # 70
print(calc_total(50, 60))  # 110
Enter fullscreen mode Exit fullscreen mode

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.

The next title will be:

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

Stay tuned! 🚀

Top comments (0)