Robotics & Artificial Intelligence
Write a Python program to calculate the area of a square and a rectangle by defining a function.
Python Functions
1 Like
Answer
def area(length, breadth=-1):
if breadth == -1:
return length * length
else:
return length * breadth
s = int(input("Enter side of square: "))
result_square = area(s)
print("Area of square:", result_square)
l = int(input("Enter length of rectangle: "))
b = int(input("Enter breadth of rectangle: "))
result_rectangle = area(l, b)
print("Area of rectangle:", result_rectangle)Output
Enter side of square: 4
Area of square: 16
Enter length of rectangle: 5
Enter breadth of rectangle: 6
Area of rectangle: 30
Answered By
1 Like
Related Questions
Which of the following statements is true for functions in Python?
- Function is a reusable code segment of program.
- Function does not provide better modularity for your program.
- You cannot create your own functions in Python.
- All of these
Fill in the blanks:
- Function can accept inputs called ……………. .
- The ……………. arguments allow to specify a default value for a function parameter.
- The ……………. statement is used to return a value from a function.
- A function can have ……………. parameters, which have default values assigned to them.
- In a ……………., a large program is broken down into individual blocks of code that work together to complete a task.
- ……………. functions refer to those functions that are already defined in Python and user can use them directly without referring to any module or library.
- The ……………. keyword is used to define a user-defined function.
- The two parts of a function are ……………. and ……………. .
- ……………. are the names given to variables that are given in the parentheses in the function definition.
- Multiple values are returned by a function in the form of the ……………. data type.
- The function is called ……………. if it does not return any value or has an empty return statement.
- The functions that return some value using the return statement after execution are called ……………. functions.
Write a Python program to print the table of a given number by using a function.
Write a Python program to demonstrate the concept of default arguments in a function.