DEV Community

Max
Max

Posted on • Updated on

Python Function Tutorial

Python function is a block of reusable code that performs a specific task. Functions can be called multiple times from within a program, which allows for efficient code reuse and organization. In Python, functions are defined using the "def" keyword, followed by the function name, a set of parentheses, and a colon. The body of the function is indented below the function definition.

Functions can take parameters, which are passed to the function when it is called. Functions can also return values, which can be used by the calling code. Python functions can have default parameter values, making them more flexible and easier to use.

Functions are an essential part of Python programming and are used extensively in all types of applications. They allow developers to organize their code and improve its readability, maintainability, and reusability.

Python Function Example

def greet(name):
    # This function greets the person passed in as a parameter
    print("Hello, " + name + ". How are you today?")

greet("Max")
Enter fullscreen mode Exit fullscreen mode

Output:
Hello, Max. How are you today?

In the above example, we have defined a function named greet which takes one parameter name. The function prints a greeting message to the console, using the print() statement.

To call the function, we simply pass the name of the person we want to greet as an argument to the greet() function.

Python Function with Return Type Example

def add_numbers(x, y):
    # This function takes two numbers as input and returns their sum
    return x + y

result = add_numbers(5, 10)
print("The sum of 5 and 10 is:", result)
Enter fullscreen mode Exit fullscreen mode

Output:
The sum of 5 and 10 is: 15

In this example, we have defined a function named add_numbers which takes two parameters x and y. The function returns the sum of x and y using the return statement.

We call the add_numbers() function and store its result in a variable named result. We then print the result to the console using the print() statement.

Python Function with Default Arguments Example

def print_message(name, message="Hello"):
    # This function prints a message to the console
    print(message + ", " + name + "!")

print_message("Max")
print_message("Bob", "Good morning")
Enter fullscreen mode Exit fullscreen mode

Output:

Hello, Max!
Good morning, Bob!
Enter fullscreen mode Exit fullscreen mode

In this example, we have defined a function named print_message which takes two parameters: name and message. The message parameter has a default value of "Hello".

If we call the function with only one argument (name), it will use the default value for message. If we call the function with two arguments (name and message), it will use the value we passed in for message.

Assigning a Function to a Variable Example

def print_hello():
    print("Hello, world!")

greeting = print_hello
greeting()
Enter fullscreen mode Exit fullscreen mode

Output:
Hello, world!

In this example, we have defined a function named say_hello which prints a greeting message to the console. We then assign the say_hello() function to a variable named greeting.

We can call the greeting() function to execute the say_hello() function and print the greeting message to the console.

Explore Other Related Articles

Python break and continue tutorial
Python While Loop tutorial
Python For Loops, Range, Enumerate Tutorial
Python if-else Statements Tutorial
Python set tutorial

Top comments (0)