Exercise
Function Double With Mutable Parameter
Objective
Develop a Python function named "double_value" to calculate the double of an integer number and modify the data passed as an argument. Since Python does not use reference parameters in the same way as C#, we will use a mutable type like a list to achieve similar behavior. For example:
def double_value(num):
num[0] *= 2
x = [5]
double_value(x)
print(x[0])
would display 10
Example Python Exercise
Show Python Code
# Define the function double_value which doubles the value of the first element in the list
def double_value(num):
num[0] *= 2 # Double the value of the first element in the list
# Main function to test the double_value function
def main():
x = [5] # Create a list with a single element (5)
double_value(x) # Call the double_value function to double the first element
print(x[0]) # Print the first element of the list, which is now 10
# Call the main function to execute the program
main()
Output
10
Share this Python Exercise