C Handout 4 - Functions

(10/10/00 - Page 2)

 

Functions

A function in C is used whenever you want to do the same operation more than once, oftentimes with different arguments.  The function ”printf()” is one example, as are the functions you used to access the beeper and the sensors on the handyboard.

 

There are two general types of functions.  Those that return values and those that don’t. The program below is an example of a program that uses both types of functions.

 

/*This function sets the speed on the left and right motors*/

void SetSpeed(int LeftSpeed, int RightSpeed) {

      motor(0,LeftSpeed);

      motor(1,RightSpeed);

}

 

 

/*This function calculates and returns the average of two numbers*/

int Avg2(int c0, int c1) {

      int sum;        /*This is a local variable*/

 

      sum=c0+c1;        /*Add up inputs to function*/

      return sum/2;      /*Calculate average and return value*/

}

 

 

void main() {

      int a0, a1, a2, a3, LS, RS;

      /* a0 through a3 are the readings from the analog inputs.

         LS and RS are the speeds of the left and right motors.  */

     

      printf("\nPush Start to begin.");

      start_press();      /*Wait for start button to be pressed*/

 

      /*Read in four analog values*/

      a0=analog(0);  a1=analog(1);  a2=analog(2);  a3=analog(3);

 

      /*Set LS to average of the leftmost sensors*/

      LS=Avg2(a0,a1); 

      /*Set RS to average of the rightmost sensors*/

      RS=Avg2(a2,a3);

 

      /*Set motor speeds based upon light readings*/

      SetSpeed(LS,RS);

 

      printf("\nPush Stop button to end.");

      stop_press();      /*Wait for stop button*/

      ao();             /*Turn off all motors*/

}

 


 

Variables in functions

·        A variable declared at the top of the program can be used by all functions (including main()).

·        A locally (within a function) declared variable can only be used within that function.

·        Changing variables passed to a function does not change their value in the calling function.

·        If a variable is declared both locally and at the top of the program, the local one is used.

 

 

 

 

 

Review Sheet, Functions

 

Some Functions

Given the program

 

int i=4;        /*Declare a variable, and initialize it*/

 

int reset() {

return i;

}

 

int next(int j) {

return j=i++;

}

 

int new(int i) {

      int j;

      j=2;

      j=j+i;

      return i=j;

}

 

void main() {

      int i, j;

      i=reset();

      j=1;

 

      printf(“\n %d %d”,i,j);         /*Print 1*/

      printf(“\n %d”,next(i));      /*Print 2*/

      printf(“\n %d”,new(i+j));      /*Print 3*/

      printf(“\n %d %d”,i,j);         /*Print 4*/

}

 

 

What does the LCD screen show at Print 1? _________________________________

 

What does the LCD screen show at Print 2? _________________________________

 

What does the LCD screen show at Print 3? _________________________________

 

What does the LCD screen show at Print 4? _________________________________