SourceRally

Login Form

Continuing our discussion of understanding programming syntax, previously we have spoken about functions, and we're going to continue with a little bit more discussion on that before moving into some other key aspects of underlying program syntax.

Functions (cont.)

Using functions is, as we've covered, a vital part of programming. One consideration we briefly started to introduce at the end of our last "Understanding programming syntax (Part 1)" post was the concept of returning values. Great care should be taken when you're thinking about this, and it's a common mistake made by beginners to assume that you should be returning every part of altered variable in a specific return scope.

This is untrue, and a dangerous assumption to make! Rather than throwing huge objects around your program, it is often significantly easier to use variables passed by reference to modify the variable values in-memory instead of moving them around different functions. However, this is absolutely something you should try to maintain consistency of across your program. It can be incredibly confusing for collaborative programming (especially if you're doing it remotely!) to have some functions which take variables by reference and directly operate on them while others take variables by value and return changed value pairs. There can be times at which you would want to do this, but it's always best to try to make sure that you keep consistent across your code base. 

While Loops (and do while loops)

Another key aspect of basic syntax is the use of while loops and do-while loops. We'll start by defining the difference between them because this is often an area where beginners misunderstand key aspects of the syntax and can be very confusing if you're using incorrect logic.

  • While loops take an initial condition and then will iterate through a loop if this condition holds as true. The conditionality is checked at the start of every loop iteration, and the code block is then executed provided the condition still holds...whereas...
  • Do-While loops execute a code block and will then check a condition, looping back to execute the code block again if the condition holds as true. This means that the conditionality is checked at the end of every loop iteration, and the code block is then executed again if the condition still holds.

As you can see, the difference here is whether or not the code block is always executed at least once - it is in do-while loops, and not in while loops. This is an incredibly basic but vital thing to understand, and easily illustrates when you should use a dowhile loop in comparison to a while loop.

This being said, utilizing while loops is the primary way of keeping something executing until a specific condition is reached - such as performing a mathematical function on a number over and over until it reaches a base case (similar in type to recursion).

There are a huge number of different types of syntax depending on which programming language you're using. From Java to Golang, PHP to Perl and even the dreaded Visual Basic, every language has it's own specific type of coding syntax and standards. Even once you get past the syntax issues, many individual developers and teams will have their own forms of standardisation which define the specifics of the syntax you're encouraged to use while working on a given project...and this is before we even get anywhere vaguely near the whole "tabs vs spaces" debate. Fortunately though, there are some standard aspects of syntax which are generally abstract-able and continuous throughout most programming languages.

Understanding these different syntax aspects can give you a quick and rapid foothold into understanding any programming language and syntax - a tool well worth having in your mental toolkit! Doing this will mean you are able to quickly get behind any programming language you try to learn which will give you a significant leg-up in understanding the specifics of each language. Since every one has various quirks and unique attributes, this is an absolutely vital part of becoming an experienced developer.

Functions

The most fundamental aspect of any language is the function. In essence, a function is a self-contained block of code which can be referenced by anywhere else in your programming codebase. Functions can, obviously, reference other functions, which means that you can design your code in a modular way and avoid repetition of code.

This is an essential aspect of coding and forms the basis for "DRY" coding - Don't Repeat Yourself.

Effectively, you should always ensure that any time you are repeating code, in general, this is instead split out into modular and function based design scope. This means that rather than having repeated code blocks of logic, you create functions which are then referenced by your code so rather than repeating the logic, you have it in one place, and then make function calls to execute it.

Since functions can take an arbitrary number of arguments, this also means that you can easily repeat any aspect of logic in a function - not just simple consecutive logic. In essence, functions are almost like their own small programs which take input data and output data (typically).

You should always aim to leverage functions throughout your program code if you are doing anything remotely complex, and using a modular design pattern means that you should also try to group similar functions together into modules. For example, if you are creating a program which is used to create some interesting mathematical patterns and then display them, you could have one set of functions in a module called mathFunctions which performs the mathematical functions on inputted data, and another called displayPatterns which handles the rendering of the mathematical patterns into graphs etc.

Another consideration to make is that functions do not always have to return data, nor do they have to return all the data changed by the function. Passing variables by reference (the default in some programming languages anyway) means that it's entirely possible for a function to operate on data in-memory passed as a reference and then return either nothing (in the case of a function with void type) or something more specifically useful to determining logic flow in the original program (such as a success or failure code).