Resources | Subject Notes | Computer Science
Library routines are pre-written sets of instructions that programmers can use in their programs. They provide ready-made functionality, saving time and effort. These routines are often referred to as functions or methods and are part of a software library. Using library routines promotes code reusability, reduces errors, and improves program efficiency.
Library routines can be categorized based on their functionality. Common types include:
Let's consider a simple example using a hypothetical library routine called calculate_area
that calculates the area of a rectangle.
Step | Code | Explanation |
---|---|---|
1. Import the Library | import geometry_library |
This line imports the geometry_library , which contains the calculate_area routine. |
2. Call the Routine | area = geometry_library.calculate_area(length=5, width=10) |
This line calls the calculate_area routine, passing the length and width as arguments. The returned area is stored in the area variable. |
3. Display the Result | print("The area is:", area) |
This line displays the calculated area to the user. |
Here's a table showing some common library routines and their purposes:
Library | Routine Name | Purpose |
---|---|---|
Math Library | sqrt() |
Calculates the square root of a number. |
String Library | upper() |
Converts a string to uppercase. |
Input/Output Library | scanf() (C) / input() (Python) |
Reads data from the user. |
Graphics Library | draw_circle() |
Draws a circle on the screen. |
When using library routines, it's important to: