locked
Please explain Subroutines RRS feed

  • Question

  • I did not understand the "Subroutines" part of the Small Basic tutorial.I feel like I got the use,but not completely.Could someone please explain to me the usage of the Subroutines in it's entirety.I'm a a beginner to programming (DUH) so I don't get the meaning of "Sub."Is it some sort of loop,or a "repeat" kind of thing.

    Thank You

    Monday, July 30, 2012 11:53 AM

Answers

  • A Subroutine is a section of code that you can run from anywhere in your program just by naming the subroutine. Here is a subroutine:

    Sub name_of_subroutine

      do this

      and this

      and this

    EndSub

    To make the code in the subroutine run, just call it, like this:

    name_of_subroutine()

    Obviously, you might prefer shorter names. :) Once the program reaches the end of the code, it will go right back to where it was called from.

    Why use a subroutine? It's reusable! You can call a subroutine from as many different places as you'd like. If you want to modify heavily used code, you can just modify it in one place, instead of at every single place you use it.

    Personally, I like to use subroutines as much as I can. It keeps code readable and reusable.

    Thursday, August 2, 2012 2:45 AM

All replies

  • Hi - I'll have a stab at this but I too am a complete beginner.

    Subroutine - part of your program that; does a something in particular, and that may need to be used more than once - so writing that code as a named subroutine allows you to "call" that subroutine, and the job that it does, simply by calling it's name followed by () rather than having to write the all of the code that's contained in the subroutine over and over again.

    The lesson itself explains it better than I can.

    For me too they are a natural way to take one part of a coding problem to one side and solve it. Then I can call that "solved part" and use it in the main body of the program as and when needed. It's much easier having written the subroutine to move it around, to alter that exact place at which it runs by simply moving that one line of code that calls the subroutine rather than having to move a great chunk of code around each time.

    this is my attempt at a solution to the problem given in lesson 1.5

    http://smallbasic.com/program/?CZX934




    Monday, July 30, 2012 5:15 PM
  • What helps me to remember is I break down the word, Sub-routine. I know from basic English that routine is a sequence of actions followed regularly. So if something is "sub" of that "routine", I automatically think "a part of".In one form or another, it is apart of that "main action".

    Hope that helps you out.

    Happy programming!


    Allen Gibbs Allen2633@hotmail.com A+/Network+ Certified

    Monday, July 30, 2012 7:07 PM
  • A Subroutine is a section of code that you can run from anywhere in your program just by naming the subroutine. Here is a subroutine:

    Sub name_of_subroutine

      do this

      and this

      and this

    EndSub

    To make the code in the subroutine run, just call it, like this:

    name_of_subroutine()

    Obviously, you might prefer shorter names. :) Once the program reaches the end of the code, it will go right back to where it was called from.

    Why use a subroutine? It's reusable! You can call a subroutine from as many different places as you'd like. If you want to modify heavily used code, you can just modify it in one place, instead of at every single place you use it.

    Personally, I like to use subroutines as much as I can. It keeps code readable and reusable.

    Thursday, August 2, 2012 2:45 AM