Tuesday, June 8, 2021

Grouping instructions

Similar instructions

Everyday routine tasks

  • brush teeth
  • take a bath
  • get dressed
  • do homework

These are a few activities that we do daily! These activities follow a similar set of sequence and repeatedly performed in similar ways.

Ever wonder if we could get them done in just one step?

Unfortunately a real life doesn't allow us a shortcut. But computer world does!

The computer programs as well contain parts that are repeated many times. For example, what happens when user copy and paste the text?

The copy and paste process involves remembering the text and pasting it whenever required.

Figure 2

Create a group of instructions

For the computer to bundle an activity or group a set of instructions, programmer need to tell the computer, what the procedure should be called and what it should do?

Hey Computer:

Remember as (copy and paste):

Remember the selected text and

     paste the selected text

Almost everything programmer tells the computer to do (such as even copy and paste) is actually a group of instructions which we also call predefined procedure.

Hey Computer:

Create a list of my_inspirations

Add “Mark Zuckerberg”, “Bill Gates”, and “Steve Jobs” to my_inspirations

Sort my_inspirations in alphabetical order

Display my_inspirations

 

Output:

     Bill Gates

     Mark Zuckerberg

     Steve Jobs

The above sequence shows how to create a list, sort it and display it too.

Now, let's make the computer remember sorting in alphabetical order and displaying it using stored group of instructions

Hey Computer:

Remember as sort_and_display:

     Sort my_inspirations in alphabetical order

     Display my_inspirations

Create a list of my_inspirations

Add “Mark Zuckerberg”, “Bill Gates”, and “Steve Jobs” to my_inspirations

     sort_and_display

 

Output:

     Bill Gates

     Mark Zuckerberg

     Steve Jobs

In the example, program has defined a sequence of steps called sort_and_display and reuses it when required (at the end).

Don't worry if the concept is little bit confusing. The doubt will be cleared in next part when actual program is written using programming language keywords.

Procedures and functions

The procedures may be used to remember steps and perform when required.

Similarly programmer can use functions when he wants to:

  • provide input to the procedures
  • display result from the procedures

In a nutshell, procedures perform task, while functions produce a result other than doing task.

Summary

The group of similar functions are written together and stored as procedures or functions.

The procedures and functions are used to make the program reusable and shorter in length.

The functions can display the result while procedures can only carry out the tasks.

PS: I have composed this post-series based on my pdf eBook on playstore:

Basics of Computer, Programming and C

The series is posted continuously one each next day, and reader can found any post by referring blogs’ time line tab.

--till next post, bye-bye and take care.

 

No comments:

Post a Comment