Exercise
Value-Returning Function
Objective
Develop a Python program where the main function should look like this:
x = 3
y = 5
print(sum(x, y))
You must define the function `sum`, and it will be called from within the main function. As shown in the example, it must accept two integers as parameters and return an integer (the sum of those two numbers).
Example Python Exercise
Show Python Code
# Define the sum function that accepts two integers as parameters
def sum(x, y):
# Return the sum of the two integers
return x + y # This will return the sum of x and y
# Main function to execute the program
def main():
# Initialize x and y as 3 and 5, respectively
x = 3 # x is set to 3
y = 5 # y is set to 5
# Call the sum function with x and y as arguments, and print the result
print(sum(x, y)) # This will print the result of 3 + 5, which is 8
# Call the main function to run the program
main() # This triggers the execution of the sum function and prints the result
Output
8
Share this Python Exercise