Value-Returning Function - Python Programming Exercise

In this exercise, you will develop a Python program where the main function should define and call a sum function that accepts two integers as parameters and returns their sum. This exercise is perfect for practicing function definition, parameter passing, and basic arithmetic operations in Python. By implementing this program, you will gain hands-on experience in defining functions, passing parameters, and performing arithmetic operations in Python. This exercise not only reinforces your understanding of function definition but also helps you develop efficient coding practices for managing user interactions.

 Category

Mastering Functions

 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

 Copy 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

 More Python Programming Exercises of Mastering Functions

Explore our set of Python Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of Python. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in Python.

  •  Value-Returning Function V2

    In this exercise, you will develop a Python program where the main function should look like this: def main(): print("\"Hello, how are you\" contains {0} spaces".form...

  •  Centered Text Function

    In this exercise, you will develop a Python function named "get_int" that displays the text received as a parameter, prompts the user for an integer number, repeats i...

  •  Underline Text Function

    In this exercise, you will develop a Python function that can center the text on the screen (assuming a screen width of 80 characters) and then underline it with hyph...

  •  Array Summation Function

    In this exercise, you will develop a Python program to calculate the sum of the elements in an array. The main function should look like this: def main(): example = [...

  •  Double Value Function

    In this exercise, you will develop a Python function named "double" to calculate and return an integer that is twice its input. This exercise is perfect for pr...

  •  Function Double with Mutable Parameter

    In this exercise, you will develop a Python function named "double_value" to calculate the double of an integer number and modify the data passed as an argument. Sinc...