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.
Parameters:
Section titled “Parameters:”-
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.
def greet(name): print(f'Hello, {name}!')
# Call functionname = 'PyDocs'greet(name)
Output:
Hello, PyDocs
In this example, name is the parameter of the greet function.
Return:
Section titled “Return:”-
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.
def add(a, b): result = a + b return result
# Usage examplesum_result = add(3, 5)print('The sum is:', sum_result)
Output:
The sum is: 8