Sameer Siruguri

My Blog

Learning Programming: Functions, Inputs, Syntax and Outputs

In our previous lesson, we started using spreadsheets (Excel or Google Docs Spreadsheets) to start learning some common ideas that apply to all programs. We wrote down a simple formula to calculate the sum of two numbers: =sum(A1, B1)

So what does this tell us about programming? The first thing is to notice that we used a function to compute the sum.

Spreadsheets call them formulas (or formulae, perhaps, to be exact) but in most other programming situations, you call them functions: bits of code that will take some inputs and execute a series of tasks, generally ending in a single output value.

As we discussed in the Lesson 1, there are three concepts here that we should learn:

  1. Inputs
  2. Syntax
  3. Outputs

Let’s go over these now.

Inputs

In this case, sum is a function that is taking two inputs – the numbers that need to be added – and will compute, and respond with or “return,” their sum. It’s a pretty simple function, but it helps illustrate a lot of things we need to learn.

What happens if the inputs aren’t numbers? In a spreadsheet, an input that’s not a number will be treated as if it’s a 0, for the purposes of the sum function. This is not necessarily the case with all functions or formulae – the internal behavior of a built-in function like sum is sometimes documented, but is sometimes either not documented at all, or is incorrectly documented. Many times you will have to discover the real behavior by experimenting.

Read about the QUOTIENT function. Figure out what happens when you supply a non-numerical input.

Syntax

The next thing to notice is how you define the function. Here are some observations you might make:

  1. The function name has to be preceded by an = sign
  2. Its inputs are separated by commas, and
  3. They are enclosed within parentheses.

These are some of the conventions used by spreadsheet programs – both Excel and Google Spreadsheets use the same convention.

This convention is commonly referred to as the syntax used by the programming language. Each language and framework will have its own syntax. At first, this might seem very daunting – it’s like having to learn a new script each time you learn to speak a new language (like Mandarin, or Arabic.) But just as many languages have scripts that have lots of common characteristics (like English, French and Spanish do,) so do many programming languages follow very similar conventions.

For example, many modern languages have a very similar syntax for defining functions – the name of the function, followed by a left-parenthesis (also called a “left paren”), then the inputs, and the closed by a right paren.

Modern-day conventions are still conventions – there’s nothing obvious or intuitive about them. Look up the syntax of much older languages, like BASIC, Cobol or FORTRAN, to get a sense of alternative syntax structures. These sometimes have similarities with the conventions we use today and are sometimes very different (for example, FORTRAN calls functions, subroutines.)

Outputs

Functions are used to do many things, but one action that is fairly common is to produce an “output” – a value that is the final computation done by a function.

For example, if we used the AVERAGE formula in a spreadsheet, it has to perform two mathematical steps – take the sum of the numbers, and then divide by how many numbers there are. The final output is the result of the division, which is defined as the “average.” 1)or more technically, the mean.

The output generated by a function is also called its “return value.”

In this lesson, we started diving into some key vocabulary that you have to get comfortable with, as you start learning how to code. An important thing to remember is that as you pick up more programming languages, you will learn lots of synonyms for the same concept. As we noted, what are called “formulae” in spreadsheets are also referred to as “functions,” “methods,” “subroutines,” and “procedures.”

What is more, you can also have two different concepts that sometimes have the same, or very similar, names. Usually, when two names are related, what they mean in two different languages tends to also be related or similar but you’re always advised to try and make certain for yourself.

Now that we know what to call bits of code that perform actions for us, let’s move on to the next important concept: data and variables.

References   [ + ]

1. or more technically, the mean.

Single Post Navigation

Leave a Reply

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