4. Pseudocode With Style

Hi again. So far, we’ve used English to represent bits of code. This wasn’t done in a very consistent fashion. Sometimes we used parentheses, other times we indented lines, and so on. So before we continue, let’s agree on the syntax we’re going to use.Like with any human language, the syntax is the set of grammatical rules one needs to use to construct a correct sentence. In programming, the syntax is the same thing: the set of grammatical rule a coder needs to adhere to in order to write statements a compiler or an interpreter can understand and translate to binary code.

Not Quite A Language

In some of the previous examples, we used simple bullet point lists to demonstrate sequences of statements.

  • Stand up
  • Walk to the machine
  • Execute the MakeDrink function
  • Deliver the return value of MakeDrink (the cup) to someone

Notice how we used English, and not a programming language, to represent a program. English is often used by programmers to communicate with non programmers or to organize their thoughts before translating them into real code. This is called pseudocode. It looks like code, it’s structured like code, but it is not code. There are no compilers nor interpreters for pseudocode.

You may ask why we’re using pseudocode instead of learning a real programming language right now. So go ahead and ask! Well, we want to first introduce you to the basics of programming through pseudocode so that you can focus on understanding the logic at the heart of programming in general, instead of mixing it with the specifics of a programming language. One compelling reason for this is that all of these concepts are not tied to a particular programming language. So if you decide to change track, give up learning app development, and instead jump into web development, all of these concepts still apply.

A Change In Style

So since we’re starting to use pseudocode to demonstrate some basic programming concepts, now would be a good time to define a style guide for its syntax and how we are going to write our pseudo source code.

Source Code

Instead of using a bullet point list, everything that represents source code will be shown in a block using a monospaced font like this:

This is an example of source code 

Functions

We know that functions have a signature and a body. Should we need to write multiple functions, it would be good to illustrate which function bodies belong to which function signatures. To do that, we’ll wrap the bodies of each function between curly braces { }. We will also prefix the function name with the word func. And we’ll indent (adding empty spaces on the left) all of the statements inside of function bodies to give us a quick visual cue to indicate that they belong to a particular function.

func MyFunctionOne {   this statement belongs to MyFunctionOne   this statement also belongs to MyFunctionOne }  func MySecondFunction {   this statement belongs to MySecondFunction }  This statement doesn't belong to any function 

See? Now we can tell where a function starts and where it ends. Guess what? This is exactly how functions are written in several languages, including Java, C, C# and Swift.

Return Values

Functions return a value. So that we know what to expect as a result of calling a certain function, we will state it within the function signature. We used to write MyFunction returns a number:

AddNumbers(Number1, Number2) returns a number   return Number1 + Number2 

Not anymore! Instead, let’s replace returns a with an arrow, like this: ->. This ‘arrow’ is comprised of a ‘dash’ and a ‘greater than’ sign. Let’s also use the keyword return at the end of the body, followed by the value we wish the function to return.

func MyFunctionOne -> Number {   a statement   a second statement   return the result }  func MySecondFunction -> Text {   a statement   return the result } 

This is very close to the Swift language syntax. Writing this in pseudocode will help your brain translate func MyFunction -> Number to a number will be returned when we call MyFunction.

Tip: when you see code, always translate it into English in your head. It will help you understand what the code actually does.

No Return Value

Plot twist: sometimes, we need to write functions that do not return a value. Wild, eh?

For example, we could write a function that deletes a file on your computer. It doesn’t need to return a value. It just needs to delete a file. Behind the scenes, though, a value will be returned. It’s a special value called void and it basically means no return value.

To indicate that a function will not return anything, we would omit the return statement at the end of the body and write its signature without the expected return (the ->) like this:

func DeleteFile {   statement that deletes a file } 

The above function doesn’t return a value (or returns void, you can use both terms interchangeably). The below function returns a message in text:

func SayHello -> Text {   return Hello! } 

Arguments

If a function uses arguments to do its job, we will list them in the function signature as well. Right after the function name, we’ll place the arguments within parentheses ( ) and separate them with commas , like so:

func AddTwoNumbers(Number1, Number2) -> Number {   return Number1 + Number2 } 

If a function doesn’t take any argument, we will still add empty parentheses after its name. That is yet another visual cue that it is a function signature: if you see parentheses, chances are you are looking at a function signature.

func SayHello() {   display the word Hello on the screen } 

Conditional Statements And Loops

We need to make sure we differentiate the statements that should be executed only if a condition is true. The same goes for statements that should be repeated a certain number of times. We will use curly braces { } and indentation to mark these blocks of statements:

func SayHello(Gender) {    if Gender is Female {     display the words Hello Madam on the screen   }    if Gender is Male {     display the words Hello Sir on the screen   }    if Gender is anything else {     display the word Hello on the screen   }  } 

And with a loop:

func SayHelloThreeTimes() {    Do 3 times {     display the word Hello on the screen   }  } 

Calling A Function

In all of the previous lessons, we always wrote call the function named XYZ in the source code. Let’s change that. Now, when we call a function, we’ll do so simply by writing its name without the expected return value. If the function accepts arguments, we pass them within parentheses. If it doesn’t accept an argument, we use empty parentheses after its name. Calling the previous func SayHello(Gender) function would be written like this within a source code file:

Do something SayHello(Female) Do something else 

In the above example, the 3rd line calls the function SayHello with the value Female for the argument Gender.

Calling a function that doesn’t take any argument would be written like this (notice the empty parentheses):

Do something SayHelloThreeTimes() Do something else 

In the above example, the 3rd line calls the function SayHelloThreeTimes without passing any value to it as the function doesn’t require any argument. When reading the example source code, we know a function is called because of the parenthesis (()) after its name.

Summary

Fantastic! In this lesson, we defined a set of rules delineating the pseudocode we’ll use throughout this course. Of course, when we start learning a real programming language, we’ll use the language’s own syntax. For the time being though, let’s continue learning a few more basic programming concepts using pseudocode.

Important Words

  • Pseudocode
  • Syntax
  • return
  • Void
  • Block

Exercise

Write the pseudocode for the program Make a sandwich. Use the style guide we defined in this lesson. And make sure you share the recipe for your awesome sandwich making skills in the Forums! Will it be peanut butter or jelly? We prefer both. Of course, you can also use the Forums if you are stuck or if something is not clear.

Share to friends