Skip to content

Parameters and Return

In Python, parameters and return are key concepts for defining functions. Parameters are the variables that a function receives, allowing the function to operate with external data, while the return specifies the value that the function returns after its execution.

  • Definition: They are variables that are listed within the parentheses of a function definition.

  • Usage: Allow the function to receive data or information from outside of it.

function.py
def greet(name):
print(f'Hello, {name}!')
# Call function
name = 'PyDocs'
greet(name)

Output:

greet.py
Hello, PyDocs

In this example, name is the parameter of the greet function.

  • Definition: The keyword return allows you to return a value from the function.

  • Usage: The value that follows return is returned to the place where the function was called.

sum.py
def add(a, b):
result = a + b
return result
# Usage example
sum_result = add(3, 5)
print('The sum is:', sum_result)

Output:

sum.py
The sum is: 8