CALL FUNCTIONS AND SUBROUTINES

After you write your subprogram, you need to call (or run) it from within your larger programs. Most programming languages use a syntax similar to the following for calling functions. To call your average3 function you wrote earlier, you'd write an instruction such as:

the_avg = average3(31.5, 29.8, 22.7)

In this example, you provided the three numbers to be averaged as literal numbers in the call statement. Alternatively, you could provide variables by name or a mix of variables and literal numbers. For example, to call the show_age subroutine, you could write something similar to:

call show_age( this_year, 1972)

Some programming languages require the use of a special instruction (such as call ) to run a subroutine; other languages let you simply state the name of the subroutine as an instruction. Fortran is an example of a language that requires a call statement; C/C++ does not.

A call to a function or subroutine can be embedded in another function or subroutine. In fact, this happens all the time, even in simple programs. Putting together complex computation by combining simpler parts in the form of subprograms is essential to programming.

Subprograms with Complex Variables

Recall from Lesson 2 that you can have a variable that contains other variables. Arrays are a common example used with subprograms, both as input parameters and as returned values. For example, you might write a subroutine to find the largest number in an array of numbers, or the longest string in an array of strings. The definition of the subroutine would simply state that it takes the name of an array variable as a parameter.

When to Write Subprograms

When you get rolling on writing a program, it's easy to find yourself with a single chunk of code that just goes on and on for pages and pages instead of breaking up the job into easily understood subprograms. You may think you're in control and can remember what all the bits in that great monolithic block of code do, but it's certain you'll find it easier to understand and maintain if you use subprograms. I once had to maintain a Fortran program (that somebody else wrote) that had 4,000 lines of code in a single chunk -- what a nightmare!

If you find your code getting longer than a page or so, start looking for opportunities to break it up

A Comment about Comments

This is a good time to talk about a good programming practice -- commenting your code. All programming languages provide a syntax for putting notes in your program that the computer just skips over. This is usually done by a special character sequence. In this example using the C convention, the computer ignores all text after the double slash:

// in C any text following // is a comment

Other languages, like Perl and UNIX shell, use the hash character for a comment:

# a comment in Perl or UNIX shell

And Visual Basic uses an apostrophe:

` a comment in Visual Basic

In some languages, if you have a lot of comments that need to stay together, you can place them between special character sequences:

/* 
    In many languages, you can place comments
    between special character sequences
*/

One of the marks of a good programmer is providing clear comments.