Saturday, January 29, 2022

Before You Learn To Code - Part II

 More Foundational Concepts Of Programming

In Part I we covered type, value, variable and assignment.  Please read Part I before reading this section.  In this part we are going to cover 2 more important foundational concepts in all programming languages, functions and scope.  By understanding these concepts learning to code will be much easier and more productive.

Concept #5 function

A function is a named list of actions that described what input it takes and what output it produces.  

There is a lot in that sentence, so let's break it down.

Named

Functions have a name which represents the list of actions.

As an example let's take a common list of actions most of us have some experience with, taking out the trash.

Here is the list of actions describing taking out the trash.

1. Remove filled garbage bag from trash can.
2. Close and tie filled garbage bag.
3. Place tied garbage bag outside for collection.
4. Put new garbage bag in trash can.

Now you could describe these steps every time the trash needed to be taken out, but instead you just say please "take out the trash".

So if we were to make a function out of this list of actions we would give it the name


 takeOutTheTrash

Arguments

 
The input is to a function is a list of  variables known as arguments.  Each variable having a type and variable name.

To continue our example from above, the argument of our function takeOutTheTrash is the currently filled garbage bag.

In most languages arguments have an open parenthesis before and closing parenthesis after arguments.

                                      type                   variable Name

takeOutTheTrash(GarbageBag bag) 

function name             arguments
                                      

Return Type

The output of a function is known as the return type of the function which indicates the type of the value that the function outputs.

Back to our example after we are done taking out the trash there should be a new empty bag in the can.

GarbageBag takeOutTheTrash(GarbageBag bag)
return type       function name             arguments

Function Signature

The function name, arguments, and return type make up the function signature which is the first part of defining a function.

Function Body

The second part of defining a function is the list of actions that make up what the function does.

Based off the list of steps from before our function would look like

GarbageBag takeOutTheTrash(GarbageBag bag){
    Remove filled garbage bag from trash can
    Close and tie filled garbage bag.
    Place tied garbage bag outside for collection.
    Put new garbage bag in trash can.
}

For now we will just use the text from above to list the actions, but eventually as we learn more concepts we will be able to replace these words with code.

You will probably have also noticed the curly bracket symbols { }.  Those indicate where the function body starts and ends.  We will discuss those more when we discuss concept #6 scope.

Declaring A Function

When you enter in your code a function signature and the function body you have declared your function.  Once you have declared a function it is ready to be called.  

Function Output

In most languages the output of a function is defined by using the word return.  In the example below you will see return being used to describe the output of the function.

Calling A Function

The point of creating a function is to be able to easily run a list of actions with clearly defined input and output.  Once a function is declare you can call it take the input passed to the function, execute each of the actions and generate output.

Here is the declaration of a simple function to add two integers.

int add(int x, int y){
    return x + y
}

The function signature describes a function named add that has the arguments of 2 integers x and y which outputs another integer.

int z = add(2, 3)

This is a call to the function which is setting the value of the variable z.  We will what happens during the call after discussing the next concept.

Concept #6 Scope

To understand the concept of scope let's discuss the idea of a sign like the sign you see around a swimming pool:
Or in a library 


Or a traffic sign
or a walk sign


For each of these signs, the apply to a particular place.  The No Running applies when around the pool, the Quiet Please when in the library, the Stop Sign when on the road and in front of it, the walk sign in on the side walk or in the cross walk.  Each of those signs have a scope, a place where they apply.  

Variables have scope as well.  Places where the variable applies.  Here is some code to illustrate this point further.

int b = 5

int add(int x, int y){
    return x + y
}

This code defines a variable
b and a function add.  When a variable or function is in scope then they can be referred to or called.    

Let's talk about the scope of the function arguments and function body which is defined by the curly bracket symbols { }.  The add function has arguments of x and yx and y are only in scope within the function body.  Any attempt to refer to x or y outside of the function would fail due to those variables being out of scope.

int b = 5

int add(int x, int y){
    return x + y
}

int c = x


The attempt to assign the value of x to a variable c would fail due to x being out of scope.  

Bringing It All Together

Let's look at this code below and describe what is going on using all of the 5 concepts. 

int b = 5

int add(int x, int y){
    return x + y
}

int d = add(20, b)

The code creates a variable b and using assignment sets it value to be 5.

Next a function add is defined which takes the arguments x, an integer variable, and y, also an integer variable.  The add function returns an integer.  The variables x and y are only in scope within the add function.

A variable d, using assignment, is set to the value returned from a call to the add function.  

During the call to the add function a scope is created and the variables x and y are set to the values passed as arguments.  x becomes 20 and y is set to the value of b which is 5.

The add function returns x (20) plus y (5) which is the value 25.

After this code runs the variable d will have the value of 25.

You Are Prepared

With these 5 concepts you will be prepared to begin your journey to learn to code.  These concepts are fundamental in all programming languages and by understanding them learning a new programming language will be more about learning the syntax and less about understanding these fundamentals.

Sunday, December 6, 2020

Before You Learn To Code - Part I

Foundational Concepts of Programming

Being able to code is an exceptionally useful skill, but learning that first language can be challenging.  Generally the first step to learning to code is finding a tutorial, opening an editor and start coding.  I understand and sympathize with this instinct entirely, but I believe there are some important concepts everyone should understand before learning to code.

One challenge when learning programming is the foundational knowledge that in some cases is assumed. By understanding these concepts your ability to learn to code will be much easier and more productive.

Concept #1 : type

To understand how something behaves and how something can be interacted with, you must understand its type.  As an example, let's take a cup. 


Something that has the type of cup tells you actions you can take with it.
  •   I can fill a cup.  
  •   I can empty a cup.
This really simple concept is an essential foundation of programming.  Think of all the different cups you have seen in your life; with handles, without handles, big, small, glass, ceramic, wood, and thousands of other varieties. You know how to interact with everyone of those cups because you know its type is a cup.

Here are some of the foundational types that are included in all programming languages:
  • int - Number without a decimal place
  • float - Number with a decimal place
  • char - A single character
  • string - Multiple characters

Concept #2 : value


The concept of type is a general one while value is a specific concept.  A value has a type.  I say cup and you, knowing the type cup, can envision a conceptual cup in your mind.   I say here is your coffee in my Baby Yoda coffee cup.  That is no longer a conceptual general cup.  That is a very specific cup.


Think of the object in your hand as a value with type cup.  If this seems a little confusing, hopefully the next concept will start making things clearer.

Here are some values with types found in all programming languages.
  • 5 is a value with type int
  • 26.8 is a value with type float
  • c is a value with type character
  • hello  is a value with type string

Concept #3 : variable

A variable is a name with a type and a value.  You express actions when programming using the names of variables.  So let's define a variable with type cup which refers to the value of the Baby Yoda mug.  The name of the variable is yourBabyYodaMug.

Now with yourBabyYodaMug variable which is of type cup your program can take actions on the value Baby Yoda mug.  Hopefully these kind of contrived descriptions start give you a understanding of the foundational concepts. 

All programming languages have ways to define a variable. Some languages require you to declare the type and name of a variable:

int myInteger

type variable

In these languages requiring variables to be declared with type, those variables can only hold values with the type defined.

Other languages have ways of just declaring variables with just a name:

myInteger

variable

In those languages, only requiring variables to be declared with just a name, then the type of the variable is defined by the type of the value the variable refers to.

Concept #4 : assignment

Variables are only useful when they refer to a value.  A variable refers to a value through the action of assignmentAssignment is generally communicated by using the equal symbol =, with a variable left of the = and a value to the right of the =.

int      myInteger           =     5

type   variable     assignment     value

This code describes a variable with the name myInteger with type int, is assigned to the value 5 which has a type of int.  An important point is the type of the variable and the type of the value is the same.

Thank you for taking the time to read Part 1.  I will describe a few more important concepts in Part 2, scope, functions and references.

Before You Learn To Code - Part II

 More Foundational Concepts Of Programming In  Part I we covered type, value, variable and assignment.  Please read Part I before reading t...