The Basics of Programming

Subroutines

Programs can get very big as more and more lines of instructions are added. Also, in some cases you may need to re-use bits of code over and over again. This is where subroutines and functions come in.

A subroutine is a set of instructions that does a specific task, for example, you could have a subroutine that calculates the average of an array of numbers and displays the result. A subroutine will always have a name, and sometimes provided with a list of values (known as parameters) that it can use, in this case an array and the number of items in the array (called num). A subroutine never returns a value, so we use the word void to say 'no value is returned'. We will use a counted loop, called a for loop, which will count through the array from 1 to num and use a variable called sum to store the sum of the values, and another variable called avg to store the average. The printf (prinf format) command will display the result on the computer screen.

E.g.

void average (int numbers[],int num)
{
  int sum, n; /* Define variables here */
  float avg;
  sum = 0; /* Set sum of numbers to zero */

  for (n=1; n<=num; n++) { /* Count from 1 to num, and increase count by 1 */
    sum = sum + numbers[n];
  }
  avg = sum / num;
  
printf( "The average is %f \n", avg); /* Display string 'The average is ' and replace %f (floating point value) with contents of avg and end with a new line (\n) */

}
To use this subroutine, you can call it from the main program or even other subroutines, using its name and pass it the variables or values that it needs.

e.g.

int numbers[5+1]; /* Define array of five integers, including one for index 0 */
numbers[1] = 456;
numbers[2] = 172;
numbers[3] = 78;
numbers[4] = 102;
numbers[5] = 321;
average( numbers[], 5);

and it should display the following result:

The average is 225.8

In C, there is one very important subroutine, it is called main. Main is the subroutine that is executed first in any C program, it is also passed two arguments that get the number and values (or arguments) passed from the command line (these are the argc and argv values), and returns an integer value (usually zero, or 0).
e.g.

int main(int argc, char** argv)
{
  /* some instructions */
  return(0);
}

alternatively, you can use int main(void), where you do not need to pass any arguments. Note the the keyword void is used where no arguments or no value is returned for a subroutine or function.

Functions

A function is very similar to a subroutine, it is still a set of instructions that does a specific task, but this time it will return a value, the type of value is specified by the variable type e.g. int, char, float, long etc. To return a value, you use the return statement and give it a value. Then the function also can be used in expressions, so if you have a complex calculation to perform regularly, you can create a function and and use it in expressions and assignments.

e.g.

float average (int numbers[], int num) /* Function is called average and returns a floating point number */
{
  int sum, n; /* Define variables here */
  float avg;

  sum = 0; /* Set sum of numbers to zero */
  for (n=1; n<=num; n++) { /* Count from 1 to num */
    sum = sum + numbers[n];
  }
  avg = sum / num;
  return (avg);
}

Now you can use it in expressions or assignments from the main program, subroutines or even other functions.
e.g.

float MyAverage = average(numbers[], 5);
printf("The average is %f\n", MyAverage);

Many programming languages have built in libraries of functions which you can use in your programs. There are functions that do complex arithmatic such as square root, sines, cosines, tangents, minimum, maximum values. There are functions that are performed on strings such as length of a string, substrings, concatenate strings and so on.

Next Page