<http://lib.cnfolio.com/ENG421UserDefinedFunctions>
Introduction to Algorithms and Programming

User defined functions



Procedural languages like C execute statements sequentially until the end.

Compound or complex statements are enclosed in braces, { }. They contain one or more simple statements.

{ /* Start of compound statement */
   statement 1;
   statement 2;
   statement 3;
} /* end of compound statement */




Functions are similar to compound statements in that they contain one or more simple statements. They are different in that they:

Read this longer description about parts of a function.

returnValue functionName( parameters list )
{
   /* These 3 statements form the body of the function */
   statement 1;
   statement 2;
   statement 3;
}




All C programs are required to have a function with the name main.

int main( void )
{
   statement 1;
   statement 2;
   statement 3;
}




Functions are important to organize programs into modules to encourage:




Function prototypes should be declared at the top of your source code file before the main function.

Read this longer description about function prototypes.

The body of functions are typically defined after the main function.

Data variables with local scope lose their value outside of the function that it was defined.

Data variables with global scope keep their value during the full time that the program is running.

#include <stdio.h>

/* Global variable */
int sum1 = 0;

/* Function prototype */
int add3( int );

int main( void )
{
   /* Variable is local to the main function */
   int counter = 0;
   scanf( "%d", &counter );
   counter = add3( counter );
   counter = add3( counter );
   counter = add3( counter );
   printf( "Global sum is %d", sum1 );
}

/* Parameter is local to the add3 function */
int add3( int param1 )
{
   param1 += 3;
   sum1 += param1;
   printf( "%d \n", param1 );
   return param1;
}




Practice example 1


Below is part of a program with a user defined function that displays multiplication tables, multTable().
  1. Add the function prototype
  2. Call the function to display multiplication tables from 1 to 10

#include <stdio.h>

/* Add prototype here for multTable() function */

int main( void )
{
   /* Call multTable() function here to display multiplication tables */
}

/* Definition for multTable() function */
void multTable( int number )
{
   int counter;
   
   for ( counter = 1; counter <= 10; counter++ )
      printf( "%2d x %2d = %3d \n",
              number,
              counter,
              number * counter );
}




Practice example 2


Below is part of a program that uses Pythagoras' theorem to determine whether 3 sides form a right triangle.

Details about Pythagoras' theorem is available online:

Sample output when the input values are 3 4 5
3, 4 and 5 form a right triangle

Sample output when the input values are 3 5 6
3, 5 and 6 do NOT form a right triangle

Sample output when the input values are 5 12 13
5, 12 and 13 form a right triangle

Sample output when the input values are 6 12 14
6, 12 and 14 do NOT form a right triangle

Sample output when the input values are 0 1 1
0, 1 and 1 form a right triangle


/*
Pseudocode
*/


#include <stdio.h>

void testTheorem( int, int, int );

int main( void )
{

}