Tutorial 7.1: Functions

Introduction
Programs that solve the real-world problem are usually consist of thousands (or more) of lines of code. In that case, it is not easy to develop, maintain and manage a large source code in a single function - main(). So when we are writing a big program, the best way to deal with it is to break the large program into smaller sets of statements (functions) according to their tasks. For example we could define functions that performs the printing of user instructions, reading inputs from user, calculating equation and etc . Whenever we need to do something, we just call the relevant function in the main() that do the specific task. In that case, our program will be neat and tidy and errors can be easily detected and solved. The following figure illustrates the sequence of execution when functions are called.

When a function (function1) is called, in this case within function main(, te statements within the block of function1 are executed. When it reaches at the end of the block, it returns to the main function and continues at the point at which the function1 was called. Similarly, a function can be invoked by another function. The sequence of execution is the same as when the main function calls a function. This is illustrated in the figure where function3 calls function4.

#include <stdio.h>

int main()
{
 int count;
 float score;
 float sum = 0;
 float average;
 printf("Enter scores for");
 printf(" class A (-1 to end input):\n");
 for (count = 0; ; count++) {
  scanf("%f", &score);
  if (score == -1) break;
  else {
   sum += score;
  }
 }
 average = sum / count;
 printf("Class A average is %.2f\n\n", average);
 
 sum = 0;
 printf("Enter scores for");
 printf(" class B (-1 to end input):\n");
 for (count = 0; ; count++) {
  scanf("%f", &score);
  if (score == -1) break;
  else {
   sum += score;
  }
 }
 average = sum / count;
 printf("Class B average is %.2f\n\n", average);
 return 0;
}
B) Program with functions
#include <stdio.h>

void userInstruct(char clsName);
float getSum(float score);
float calcAverage(float sum, int count);
void printAverage(float average, char clsName);

int main()
{
 float score;
 float sum;
 float average;
 
 userInstruct('A') // call userInstruct function for class A
 for (count = 0; ; count++) {
  scanf("%f", &score);
  if (score == -1) break;
  else {
   sum = getSum(score);  // call getSum function
  }
 }
 average = calcAverage(sum, count) // call calcAverage function
 printAverage(average, 'A');   // call printAverage function
 
 userInstruct('B') // class A
 for (count = 0; ; count++) {
  scanf("%f", &score);
  if (score == -1) break;
  else {
   sum = getSum(score);  // call getSum function
  }
 }
 average = calcAverage(sum, count)
 printAverage(average, 'B');
 
 return 0;
}

void userInstruct(char clsName)
{
 printf("Enter scores for");
 printf(" class %c (-1 to end input):\n", clsName);
}

float getSum(float score)
{
 sum += score;
}

float calcAverage(float sum, int count)
{
 return sum / count;
}

void printAverage(float average, char clsName)
{
 printf("Class %c average is %.2f\n\n", clsName, average);
}
Sample output:

Enter scores for class A (-1 to end):
75
85
64
-1
Class A average is 74.67

Enter scores for class B (-1 to end):
37
97
71
-1
Class B average is 68.33


Both program A and B are written to perform the same task which is to calculate average score for two classes. But program A does not use functions and program B uses function. As you can see in program B, the codes of user instructions, read inputs, calculate and display the average are segmented into functions. In function main(), we invoke/call the relevant functions to perform the required tasks.

Next: Variable Scope and Sequence of Execution

No comments:

Post a Comment