Newbie Question

January 14, 2020

We have something different today.

Although most of my readers are accomplished, savvy programmers, (or at least those who are confident enough to comment) I will always include content designed for beginners; it’s part of the DNA of the blog, and sometimes brings in new readers. Thus, I spend a lot of time in beginner-programmer online forums. Today’s question at Reddit caught my eye:

Newbie question: What’s the difference between a “loop” and a “function”?

Sorry if this is answered somewhere but I am completely new to programming.

What is the difference between a function and a loop, when both of them can be used more than once?

The posting history of the person who asked that question indicates that he has recently been expelled from college (his GPA wasn’t great) and that

In the meantime I just started a computer programming course online. Was never really interested in it but everyone says it’s a skill you should have so I figured I’ll give it a shot. Plus it’s free so there’s that.

Your task is to answer the newbie question; if you answer on Reddit, please also leave your answer here, with a pointer to your Reddit posting. When you are finished, you are welcome to read my answer or discuss the exercise in the comments below.

Pages: 1 2

2 Responses to “Newbie Question”

  1. Zack said

    Both are useful programming structures, independent of each other. The loop ensures that several commands are executed until a certain condition is met (e.g. iterate over all the elements of a list, one by one, until one is found that is equal to a predefined value). The function is something more general as it can undertake a specific task when needed. You may call a function just once, but in another case call it several times. A loop can contain a function in it, in which case the function will potentially be executed several times. Also, a function can contain a loop in it, if the task it undertakes requires it. Hopefully, all this clarifies how the two programming structures are different, even if they sometimes involve repeated computations.

  2. Daniel said

    Functions are often created so that 1) various other parts of a program can utilize similar functionality, or 2) to factor out logic so that it is self-contained.

    Loops are often used to repeat the same pattern of computations consecutively.

    The ideas are similar because they both reduce code duplication. That is, the code in a function could alternatively be repeated throughout a program, rather than calling the function directly. Similarly, the pattern of code in a loop can be repeated sequentially, as opposed to using a loop. However, this type of duplication could make a program more difficult to read and maintain.

    A difference between functions and loops is that the computations within a function can be utilized be various code spread throughout a program (by calling the function), whereas the code within a loop is intended to be run during the loop’s execution (and can’t be called elsewhere).

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: