Function Arguments and Return Values
On this page
Passing Information with ArgumentsPassing Multiple ArgumentsDefault ArgumentsReturning ValuesUsing Returned ValuesReturning Multiple ValuesMini Project — Currency ConverterFunctions become much more useful when they can receive information, process it, and send something back.
Arguments allow data to be passed into a function, while the return statement sends data out of it.
Together, they make functions powerful tools for computation and code reuse.
Passing Information with Arguments
Arguments are values you provide inside the parentheses when calling a function.
They allow the same function to behave differently depending on the input.
Let’s start with a simple example:
def greet(name):
print("Hello,", name)Here,name is a parameter or a placeholder for any value you provide when calling the function.
greet("Alice")
greet("Bob")Output
Hello, Alice
Hello, Bob
Each time the function runs, the value passed to name changes the output.
Passing Multiple Arguments
A function can take more than one argument.
In this example, the function adds two numbers and prints the result.
def add_numbers(a, b):
print("The sum is:", a + b)Calling it with different numbers produces different results:
add_numbers(3, 7)
add_numbers(10, 25)Output
The sum is: 10
The sum is: 35
Each argument represents one piece of information the function needs to perform its job.
Default Arguments
Sometimes, you may want a function to work even when no argument is provided. Give a parameter a default value so that callers can omit its corresponding argument.
def greet(name="Guest"):
print("Welcome,", name)Calling it with or without a name gives both possibilities:
greet("Anna")
greet()Output
Welcome, Anna
Welcome, Guest
Default arguments make your functions flexible and prevent errors when certain inputs are missing.
Returning Values
So far, our functions have printed results directly.
However, you can also make a function return a value so that the result can be stored, reused, or combined with other expressions.
Here’s a simple function that returns the sum of two numbers:
def add(a, b):
return a + bThe function gives back the result when it is called:
total = add(5, 8)
print(total)Output
13
print() displays information. return gives a value back to the caller; a function that reaches its end without return returns None.
Using Returned Values
Returned values can be used in further calculations or stored for later use.
def square(x):
return x * x
result = square(4) + square(3)
print(result)Output
25
Here, each call to square() returns a number, and the results are added together.
Returning Multiple Values
A function can also send back more than one result by separating them with commas.
Python automatically groups them into a tuple.
def calculate(a, b):
return a + b, a - bsum_result, difference = calculate(10, 4)
print("Sum:", sum_result)
print("Difference:", difference)Returning multiple values helps when a function needs to provide several related results at once.
Mini Project — Currency Converter
Write a program that converts an amount from one currency to another using the illustrative fixed rate 0.92 EUR per USD; this is exercise data, not a current exchange-rate quote. Create a function that takes the amount and conversion rate as arguments, returns the converted value, and prints a message showing the result.
Example Run:
Enter amount in USD: 100
100.0 USD is equal to 92.00 EUR