7. Program Flow

And we’re back! We’ve learned many programming constructs in a short space of time. What we haven’t yet tackled, is how you can put them together to write a program. Which statements get executed and when, is commonly known as the program flow, and it’s what breathes life into your programs. Let’s explore!

The Main Function

Check this out:

func SayBye() {   Display "Goodbye" on the screen }  func SayHi() {   Display "Hello" on the screen } 

What would happen upon executing this program? Which function would execute first? Well, each programming language takes a different approach. There’s no clear answer in pseudocode. The family of languages based on the C programming language (C++, Objective-C, Java, Swift, C#) all implement the concept of the Main function, and it’s what we’ll use, going forward.

The Main function is just like any other function, except that when you run your program, it’s the function that is always called first. We also refer to this function as the entry point to the program. We could re-write the example to explicitly say what we want the computer to run and in which order:

func SayBye() {   Display "Goodbye" on the screen }  func SayHi() {   Display "Hello" on the screen }  func Main() {   SayHi()   SayBye() } 

Now, if we run this program, the computer will look for, and execute the Main function first. Within the Main function, we’ll call SayHi first and then SayBye. This way, we know the order in which our code will execute.

Functions Need To Be Called Explicitly

Let’s extend our previous example with a new function:

func SayBye() {   Display "Goodbye" on the screen }  func SayHi() {   Display "Hello" on the screen }  func IntroduceSelf {   Display "My name is Computer" on the screen }  func Main() {   SayHi()   SayBye() } 

We’ve added this IntroduceSelf function. When will it be executed? Never. Notice how the function wasn’t called. The code for the function is in the source code, but because it’s never called, it’ll never be used within the program.

Flow Diagrams

Programmers often draw a flow diagram before they start coding. It helps to test the logic on paper and write better code. Flow diagrams rely on a convention for the meaning of symbols. Let’s start by looking at the most common ones.

Process

A process can be a single statement, an algorithm, or a function.

Entry Point / Terminator

An entry point shows where the flow starts. Usually, it’s the Main function. The same symbol is used to indicate where a program ends.

Decision

A decision symbol is used to express conditional logic, e.g. if this is true, then go there and if this is false, go here.

Display

Sometimes, we want our program to display something to our users. Using a web interface, a textbox in an app, a photo. This is when the Display symbol comes in handy.

Data

The data that is currently being manipulated is represented by a Data symbol.

Stored Data

Our programs often need to store data for later use. Take your smartphone and its photo library, for example. The photos themselves are stored in your phone’s memory. To represent stored data, we use the Stored Data symbol.

Manual Input

When we want our users to enter data into our program (say typing their username in an app, or uploading a profile photo), we represent this action with the Manual Input symbol.

Flow Line

The connection of all of these symbols is what we call the Flow Line. The direction of the arrow shows the direction of the flow.

Other Symbols

There are several other symbols, but we won’t use them for now. We’ll introduce new symbols as we need them.

Fun fact: Some symbols are also remnants of the old days of computing: one exists to represent punched cards!

Making A Drink: The Illustrated Story

Remember our very first example? When you had to make drinks for all of those thirsty work mates of yours?

Let’s use a flow diagram to tell the story once again:

Now that we have a diagram, we can visually test our logic. We can even use it to add features to our program. For example, say the machine runs out of paper cups? We could redesign our diagram by adding these symbols:

The Importance Of Flow Diagrams

As you become an experienced and seasoned programmer, trivial tasks won’t call for drawing a flow diagram. Still, in the beginning of your coding journey, you should take time to make as many as you can. Beginners often struggle with, or forget the importance of defining a clear logic structure for their programs. We, the programmers, often hear people saying: “I learned X or Y language but I don’t really know where and when to apply the concepts“. This problem is partially due to “corner cutting” and programmers neglecting or forgetting to learn, practice and implement proper software design methodology. The term “software design” refers to defining the logic of a program. It has nothing to do with designing its graphical interface.

Summary

Now you know what a program flow is and how you would render it in a visual fashion using a flow diagram consisting of conventional flow diagram symbols. You also know that programs have an entry point and that many languages implement the aptly named Main function to serve as an entry point. We know that function bodies won’t execute themselves on their own and that functions need to be explicitly called.

Important words

  • Entry point
  • Main function
  • Explicit function call
  • Program flow
  • Flow diagrams
  • Flow diagram symbols
  • Software design

Exercise

To train yourself, try to draw flow diagrams for common, routine actions: getting dressed, cooking a meal, driving your car. This practice will develop your ability to give your computer clear instructions, turning you into a statement writing master. Share your work in the Forums! You can share your drawing by using a photo sharing site such as IMGUR.

Share to friends