Python Function

Introduction

A function in Python is a block of code that performs a specific task. Functions are a key part of any programming language, as they allow you to break down complex tasks into smaller, more manageable pieces of code. In this tutorial, we will explore the basics of Python functions, including how to define and call functions.

Defining a Function

To define a function in Python, you use the def keyword followed by the function name and a set of parentheses. Inside the parentheses, you can include any parameters that the function should take. The function body is then indented under the function definition.

Here’s an example of a simple function in Python:

def say_hello():
    print("Hello!")

In this example, we define a function called say_hello that takes no parameters. When this function is called, it will simply print the string “Hello!”.

Calling a Function

To call a function in Python, you simply use the function name followed by a set of parentheses. If the function takes any parameters, you include them inside the parentheses.

Here’s an example of how to call the say_hello function:

say_hello()

When this code is executed, it will call the say_hello function and print the string “Hello!”.

Function Parameters

Functions can take one or more parameters, which are variables that are passed into the function. Parameters can be used to customize the behavior of a function based on the values that are passed in.

Here’s an example of a function that takes a parameter:

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

In this example, we define a function called greet that takes a parameter called name. When this function is called, it will print a personalized greeting that includes the name that was passed in as a parameter.

Here’s an example of how to call the greet function with a parameter:

greet("John")

When this code is executed, it will call the greet function with the parameter “John” and print the string “Hello, John!”.

Default Parameter Values

Functions can also have default parameter values, which are values that are used if no value is passed in for a parameter. Default parameter values can be useful when you want to provide a default value for a parameter, but still allow the parameter to be customized if necessary.

Here’s an example of a function with a default parameter value:

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

In this example, we define a function called greet that takes a parameter called name. If no value is passed in for the name parameter, it will default to the string “World”. When this function is called, it will print a personalized greeting that includes the name that was passed in as a parameter, or the default name if no name is provided.

Here’s an example of how to call the greet function with and without a parameter:

greet()
greet("John")

When this code is executed, it will call the greet function twice. The first time, it will use the default parameter value and print the string “Hello, World!”. The second time, it will use the parameter value “John” and print the string “Hello, John!”.

Returning Values from a Function

In Python, functions can not only perform actions, but they can also return values. This means that a function can take some input, process it, and then return an output to the caller. In this tutorial, we’ll explore how to return values from functions in Python.

Returning a Single Value

To return a single value from a function in Python, you use the return keyword followed by the value that you want to return. Here’s an example:

def square(x):
    return x * x

In this example, we define a function called square that takes a single parameter x. The function then returns the square of x.

To call this function and get the returned value, you simply use the function name followed by the parameter value(s), like so:

result = square(5)
print(result)

When you run this code, it will call the square function with a parameter value of 5. The function will compute the square of 5 and return the value 25. The returned value is then assigned to the variable result and printed to the console.

Returning Multiple Values

In Python, you can also return multiple values from a function using a tuple. Here’s an example:

def add_and_subtract(x, y):
    return x + y, x - y

In this example, we define a function called add_and_subtract that takes two parameters x and y. The function then returns a tuple that contains the sum of x and y, followed by the difference between x and y.

To call this function and get the returned values, you simply use the function name followed by the parameter values, and then assign the returned values to variables, like so:

sum, difference = add_and_subtract(5, 3)
print(sum)
print(difference)

When you run this code, it will call the add_and_subtract function with parameter values of 5 and 3. The function will compute the sum and difference of these values and return a tuple containing the results. The returned values are then assigned to the variables sum and difference, and printed to the console.

Code example of Python function:

def add_numbers(x, y):
    result = x + y
    return result

# Call the function with arguments 5 and 3
sum = add_numbers(5, 3)

# Print the result
print(sum)

In this example, we define a function called add_numbers that takes two parameters x and y. Inside the function, we add the two parameters together and store the result in a variable called result. Then, we use the return keyword to return the value of result to the caller.

To call this function and get the returned value, we simply use the function name followed by the two arguments we want to pass in, separated by commas. The value that is returned by the function is then assigned to the variable sum, which we can print to the console to see the result.

Follow us on social media
Follow Author