Learning to Program (Stage 2)—Flowcharting – Methods and Classes


This entry is part 7 of 22 in the series Learning to Program

In previous posts, I described the symbols used in flowcharting and an idea of the flow in a program.  I also discussed the different decisions and loops that you may encounter in a program.  I pointed out that while you may use a name for the loop or decision, you do not put that name in the flowchart.  This is because the flowchart is only meant to be a graphical representation of how the program will flow through (not the actual code it will use to make that flow).

In this post, I will discuss the flow of using methods and classes.  However, a little explanation of where you will use methods and classes is in order.

A method is used whenever you have some code that you will use in different places throughout your program.  An example of this would be if you print output that follows a certain format (a string, then a number, then a number for example).  As long as the variables passed into the method are the same type as the variables receiving in the method, they don’t have to be the same (or contain the same information).

A class is a special type of method.  In general, a class is a method that is able to be used by more than one program.  For example, if you have an accounting program, and a program that handles cash register duties, they both may need to use a method which calculates sales tax.  Instead of writing this method twice, you would create a class and call it in both programs as needed.

The beauty of methods and classes (when it comes to flowcharts) is this.  They are both called in the exact same way.  The main difference is in the pseudocode or code, when you declare that you’ll use a class (you don’t declare methods which are a part of the same program).  Classes are stored as a separate file.

There are four scenarios for calling a method: calling a method without arguments and without returning a value, calling a method without arguments and returning a value, calling a method with arguments and not returning a value, and calling a method with arguments and returning a value.  I will show you the flowcharts for each of these in order.

Calling a method without arguments and not returning a value.

 Method-No-Args-No-Return

Calling a method with arguments and not returning a value.

 Method-w-Args-No-Return

Calling a method without arguments and returning a value.

Method-No-Args-w-Return

Calling a method with arguments and returning a value.

 Method-w-Args-w-Return

In older programming languages (COBOL or Pascal for exmaple), you would see the calling without arguments and returning a value in read statements.  However it’s rare to do this now.  The most common methods that you will see are calling with arguments and not returning a value, and calling with arguments and returning a value.

There are other aspects of flowcharting that you will run into.  However most of them can be broken down into the different types that I showed you here already.  It’s more a matter of breaking the requirement down into the individual steps to achieve it and flowcharting it out.

So, with this, we shall move onto pseudocode. 

Have a great day:)
Patrick.

Series Navigation<< Learning to Program—Flowcharting (Stage 2) – Case StatementsLearning to Program – Structure and Spaghetti Code >>

Leave a comment

Your email address will not be published. Required fields are marked *