3. User-defined Methods

Lesson 1

Four Models

Introduction

In this lesson, we can discuss user-defined methods and practise various ways of defining and executing them.

1. Input, Process and Output in Method (IPO)
2. Input and Process in Method (IP)
3. Process in Method (P)
4. Process and Output in Method (PO)

Model 1: Input, Process and Output in Method: Non-parameterized

Learn it

  1. Write a program using a method named area() that input length and breadth of a rectangle, calculate area and print it.

Description & Input and Output


Do Yourself

  1. Write a program using a method average() that inputs two marks, calculates average and prints it.

Practice Problem

  1. W a p using a method toCelsius() that input a degree in Fahrenheit, convert to Celsius and print it. C= 5.0/9*(F-32)

Model 2: Input and Process in Method: Non-parameterized

Learn it

  1. Write a program using a method area() that inputs length and breadth, calculates area and returns it. The main() prints area.

Description & Input and Output


Do Yourself

  1. Write a program using method circumference() that inputs radius of a circle, calculate circumference (2πr) and returns it; the main() prints it.

Practice Problem

  1. W a p using a method toFahrenheit() that input a degree in Celsius, convert to Fahrenheit and return it. The main() prints it. F=1.8*C+32

Model 3: Process in Method: Parameterized

Learn it

  1. Write a program using method area() that receives length and breadth as arguments, calculates area and returns it; main() prints it.

Description & Input and Output


Do Yourself

  1. Write a program using method volume() that receives radius and height of a cylinder as parameter, calculates volume (πr2h) and returns it; main() prints it.

Practice Problem

  1. W a p using method simple() that receives principal amount, rate and time as arguments, calculates and returns simple interest. (si= prt/100). The main prints it.

Model 4: Process and Output in Method: Parameterized

Learn it

  1. Write a program using method area() that receives length and breadth as parameter, calculates area and prints it.

Description & Input and Output


Do Yourself

  1. Write a program using method perimeter() that receives length and breadth of a rectangle as arguments, calculates perimeter and prints it. p=2(l+b).

Practice Problem

  1. W a p using a method compound() that receives principal amount, rate and time as arguments, calculates and prints the compound interest. Formula: ci= p[(1+r/100)t-1]