Function Parameters and Return Values
How It Works We define a function called say_hello using the syntax as explained above. This function takes no parameters and hence there are no variables declared in the parentheses. Parameters to functions are just input to the function so that we can pass in different values to it and get back corresponding results.
What Parameters Do
A function without parameters always does the same thing every time you call it. A function with parameters lets you pass in different values so the function can work with different inputs and produce different results. Parameters are input to the function — they are variables that receive values when you call the function. Think of a parameter as a placeholder that gets filled in with a real value (called an argument) at call time.
What do you think happens?
If you define a function say_hello() with no parameters, and call it three times, what happens?
Reveal answer
Answer: It prints the same greeting all three times
Without parameters, the function has no way to receive different inputs. It will execute the same code and produce the same output every call. Parameters are what allow a function to behave differently based on what you pass in.
Parameters vs. Arguments
The terminology matters: parameters are the names you write in the function definition (inside the parentheses after the function name). Arguments are the actual values you supply when you call the function. When you call a function with arguments, each argument is assigned to the corresponding parameter in order.
Parameters are specified within the parentheses in the function definition, separated by commas. When you call the function, you supply the values in the same way — in the same order, separated by commas.
How Data Flows Through a Function
When you call a function, three things happen in sequence: first, the arguments you provide are matched to the parameters in order; second, the function body executes using those parameter values; third, if the function has a return statement, the value after return is sent back to the caller. The caller can then use that returned value (assign it to a variable, print it, use it in a calculation, etc.).
A Worked Example: Finding the Maximum
Calling a function with two parameters
Define a function print_max that takes two parameters a and b, compares them, and prints the larger one. Then call it with the numbers 5 and 8.
Define the function: Write the function definition with two parameters a and b. Inside, use an if statement to compare them and print the larger value.
Call with direct values: Call print_max(5, 8). The value 5 is assigned to parameter a, and 8 is assigned to parameter b. The function compares them and prints 8.
Call with variables: Create variables x = 3 and y = 7, then call print_max(x, y). Now the value of x (3) is assigned to a, and the value of y (7) is assigned to b. The function compares them and prints 7.
Observe the pattern: The function works the same way both times because the mechanism is identical: arguments are assigned to parameters in order, the function body executes, and the result is produced. The source of the argument values (literal numbers or variables) does not matter.
The function print_max successfully compares two inputs and prints the maximum, regardless of whether those inputs come from literal values or variables.
Return Values: Sending Data Back
A return statement sends a value back from the function to the code that called it. The caller can then capture that returned value by assigning it to a variable, printing it, or using it in an expression. Without a return statement, a function still executes but sends back nothing (or technically, None in Python).
Parameters are how data gets into a function. Return values are how data gets out. Together, they allow a function to receive input, process it, and send back a result.
8
40Parameters as Local Variables
Parameters are just like variables except that their values are assigned when the function is called, not by an assignment statement inside the function. They exist only during the function's execution. Once the function returns, the parameters cease to exist — they are local to that function call. This is called scope: parameters have local scope, meaning they are only accessible inside the function.
Common Mistakes with Parameters and Return Values
Forgetting to match the number of arguments to the number of parameters
Python cannot assign the second argument to parameter b if you do not provide it. This causes a TypeError.
Fix:
Always provide the same number of arguments as there are parameters: add(5, 3)Confusing the order of arguments
Arguments are assigned to parameters in order. The first argument goes to the first parameter, the second to the second. If you reverse them, you get a different result.
Fix:
Pay attention to the order: subtract(5, 3) returns 2Trying to use a parameter outside the function
Parameters have local scope. They only exist inside the function. Once the function returns, they are gone.
Fix:
Use the returned value instead: print(result)Forgetting to return a value when the function should produce one
Without a return statement, the function computes the value but does not send it back to the caller. The caller receives None instead.
Fix:
Add the return statement: return n * nAssuming a function modifies the original variable passed as an argument
The parameter x inside the function is a local variable. Changing it does not affect the original variable num in the caller's scope.
Fix:
Capture the return value: num = increment(num)
Parameters and Arguments in Practice
Use meaningful parameter names that describe what the parameter represents. For example, use calculate_total(price, tax_rate) instead of calculate_total(a, b). This makes the function easier to understand and use correctly. When you call the function, make sure you understand what each parameter expects and pass arguments in the correct order.
If a function computes a result that the caller needs, always return it. Do not rely on printing or side effects alone. This makes the function reusable in different contexts — the caller can print the result, use it in a calculation, or pass it to another function.
Test your function with different argument values to make sure it works correctly. This helps you catch mistakes early and ensures the function behaves as expected with various inputs.
Practice: Write and Call a Function
Write a function called convert_to_celsius that takes one parameter fahrenheit (a temperature in Fahrenheit) and returns the equivalent temperature in Celsius. The formula is: Celsius = (Fahrenheit - 32) * 5/9. Then call your function with the values 32, 68, and 212 and print the results.
Hints
- Define the function with one parameter named fahrenheit
- Inside the function, apply the formula and return the result
- Call the function three times with different argument values
- Capture each return value and print it
Key Takeaways
- Parameters are input variables in a function definition; arguments are the actual values you supply when calling the function.
- Arguments are assigned to parameters in order: the first argument goes to the first parameter, the second to the second, and so on.
- Parameters are local to the function — they exist only during execution and cannot be accessed outside the function.
- A return statement sends a value back from the function to the caller, allowing the function to produce output.
- Together, parameters and return values allow functions to receive input, process it, and send back results, making them flexible and reusable.