[Type C] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

3. Write a program to have the following functions:

  1. A function that takes a number as an argument and calculates the cube for it. The function does not return a value. If there is no value passed to the function in the function call, the function should calculate the cube of 2.
  2. A function that takes two char arguments and returns True if both the arguments are equal; otherwise, False. Test both these functions by giving appropriate function call statements.

Short Answer: Two functions will be created. One calculates the cube of a number (defaulting to 2 if no argument is provided) without returning a value, and the other checks if two characters are equal and returns True or False accordingly.

Python
def calculate_cube(num=2):
    print("Cube:", num ** 3)

def check_equal_chars(char1, char2):
    return char1 == char2

# Test Program
calculate_cube(3)
print("Are characters equal?", check_equal_chars('a', 'a'))

Explanation:

  • The function calculate_cube calculates the cube of a number (defaulting to 2 if no argument is provided) and prints the result.
  • The function check_equal_chars checks if two characters are equal and returns True or False accordingly.
  • The test program demonstrates the usage of both functions with appropriate function call statements.
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *