9. Libraries And Frameworks

Hi again. Let’s continue on our journey…

Time for another key programming concept; Reusability. As programmers, we should always aim for our code to be reusable. Say you’ve developed a set of functions that, when run together, result in your program sending an email to your users. If you start working on a new app that also sends emails, you wouldn’t want to have to recreate these functions from scratch, would you? Instead, you’d reuse the functions you’d coded previously. Let’s take a look at how libraries and frameworks allow us to achieve reusability.


A library is a set of related functions (and/or, as we’ll see later, classes) grouped together for future reuse. Let’s say that we create two functions to say hello and goodbye to a user. We would save these two functions in a source code file named GreetingLibrary:


    func SayHi() {       Display "Hello"     }      func SayBye() {       Display "Goodbye"     } 

Note that there is no Main function in our source code. Executing this file would result in nothing.

Now, let’s create a program that would use this library. To inform the computer that we want our program to use the GreetingLibrary, we import it. We save the source code for the program in another file named MySuperProgram:


    import GreetingLibrary      func Main() {        isPersonLeaving = true        if isPersonLeaving == true {         SayBye()       }        if isPersonLeaving != true {         SayHi()       }      } 

This program sets a boolean variable named isPersonLeaving to true. Then we use a conditional check to see if the person is leaving. If true, we call SayBye. If false, we call SayHi.

We can use both SayHi and SayBye in our program because we imported the GreetingLibrary containing the source code of these two functions at the top of the program with import GreetingLibrary. The technical term for containing the source code of a function is implements. Thus, we would say that GreetingLibrary implements SayHi and SayBye while MySuperProgram calls SayHi and SayBye. Terminology is important, as it helps us communicate with fellow programmers!

Now, if we want to use these two functions in yet another program, all we have to do is import GreetingLibrary and we can call them at will. Useful!

Built-In Libraries

Often, programming languages come with built-in libraries implementing a whole bunch of functions you can use out of the box without worrying about the need to write them. When you learn a new programming language, you not only learn its syntax and idioms, you also learn about the built-in libraries, or at least how to navigate their documentation to find out if a function you might need has already been written for you. These built-in libraries are sometimes called standard libraries.

In many of the previous examples, you’ve seen the statement Display on the screen (and in later examples, just Display). Where does this Display function come from? You didn’t have to write it yourself to instruct the computer to display some text on the screen. This is because in our pseudocode language, Display is part of the built-in library.

Some common functions that are implemented in built-in libraries are: Display (to output things on the screen), Input (to get a user’s input), and Sort (to sort items in an array).

In our pseudocode, we use Display. In the C++ language, we’d use cout << (C output). In Ruby, it’d be puts (put screen). The equivalent in JavaScript is Console.Log. In Swift, we’d use print and in C#, we’d call Console.WriteLine.


A framework is a collection of libraries. It’s unlikely that you’ll create one any time soon, but once you start doing some serious development, you’ll frequently use common existing frameworks. For example, the framework containing everything you need to store data on a Mac or on iOS is called the Core Data framework. Another popular framework among web developers is called Ruby on Rails (commonly known as just Rails). It allows for the rapid development of web applications using the Ruby programming language.


An API is also a grouping of libraries and sometimes also a grouping of frameworks. The acronym API stands for Application Programming Interface. The API you use to program an app for a Mac is called Cocoa. It consists of several frameworks such as Core Data, Foundation Kit, and Application Kit. A framework like Cocoa is huge and will likely provide you with everything (and more!) that you need to build awesome Mac applications quickly. Because such an API is massive, it can take a substantial amount of time to learn its intricacies and to fully harness it’s capabilities. Worry not, though: we have a ton of lessons for teaching you Cocoa!


Cool. You now know that good code is reusable code. We achieve reusability by creating libraries. Most languages offer a built-in library of functions and classes (we’ll learn about classes soon!) that you can use to simplify your work. A set of related libraries can be grouped and found in what is known as a framework. At an even higher level, we have the API, which can consist of libraries and/or several frameworks.

Important Words

  • Reusability
  • Library
  • import
  • Implements
  • Framework
  • API

Remember Lesson 2‘s exercise, in which you had to create functions for common, routine tasks? Use that list and pick related functions that you would include in a library. An example might be a MorningActions library containing all the functions you execute between waking up and leaving for work. Share your library with other students in the Forums!

Share to friends