Computers in Engineering - 308-208 Lecture 17
Computers in Engineering - 308-208


Lesson 17 - Learning Goals



17.1 Learn how to use C Functions

17.2 Learn how to use C Preprocessor directives

17.3 Learn how to use Arrays and functions in C

17.4 Learn how to use Call-by-Value versus Call-by-Reference



FUNCTIONS

IN FORTRAN :

! THIS PROGRAM COMPUTES THE CUBE OF ITS INPUT.
REAL FUNCTION CUBE(X)
IMPLICIT NONE
REAL :: X
CUBE = X * X * X
END FUNCTION CUBE

IN C :

/* This program computes the cube of its input */
real cube (x)
float x;
{
   return (x * x * x);
}

SIMILARITIES :

DIFFERENCES :

SUBROUTINES

In FORTRAN :

! THIS PROGRAM PRINTS N STARS (N<=100)
SUBROUTINE STARS(N)
INTEGER, INTENT( IN ) :: N
PRINT 5,('*',I=1,N)
5 FORMAT(' ',100A1)
END SUBROUTINE STARS
...
CALL STARS (NSALES)

In C :

In C, a subroutine is just a function that returns a ''void'', and is called like a function (no CALL keyword).

/* This program prints n stars */

void stars (n)
int n;
{
   int i; /* local variable */
   for (i=1; i<=n; i+=1) 
   {
      putchar ('*') ;
   }
   putchar ('\n') ;
   return ;
}
...
stars (nsales) ;



ADVANTAGES OF MULTIPLE FILES


EXAMPLE OF #DEFINE AND ITS USE

#include < stdio.h >
#define PI 3.14159
#define SQUARE(X) ( (X) * (X) )

main ( )
{
   double radius, area ;
   clrscr( ) ;
   radius = 5.0 ;
   area = 2 * PI * SQUARE ( radius ) ;
   printf ("Area of a circle of radius r = %f is %f",
             radius, area ) ;
}


EXAMPLE SHOWING NEED FOR PARENTHESES IN #DEFINE DIRECTIVE

#include < stdio.h >
#define PI 3.14159
#define SQUARE(X) ( X * X )

main ( )
{
   double radius, area ;
   clrscr( ) ;
   radius = 5.0 ;
   area = 2 * PI * SQUARE ( radius + 1.0 ) ;

    /* Here SQUARE will compute ( radius + 1.0 * radius
       + 1.0 ) which is not what we want. This gives 
       ( 2*radius + 1.0 ) instead of ( ( radius + 1.0 )
       * ( radius + 1.0 ) ) */ 

   radius = radius + 1.0 ;
   printf ("Area of a circle of radius r =%f is %f",
             radius, area);
}


CODE ILLUSTRATING THE DIFFERENCE BETWEEN FUNCTION AND #DEFINE

#include <stdio.h>

#define SQUARE( X ) ( (X) * (X) )

int square(int x)
{
   return(x*x)
}

main ( )
{
   int i = 3 ;
   int j ;
   clrscr( ) ;
   j = square (i) ;

   printf ( " Square of %d = %d \n", i, j ) ;

   j = SQUARE (i) ;

   printf ( " Square of %d = %d", i, j ) ;
}
Output :
Square of 3 = 9

Square of 3 = 9


USING FUNCTION TO AVERAGE ELEMENTS OF AN ARRAY

float average_floats ( float x [ ], int n )
/*
where x is the array of elements and n the number 
of elements 
*/
{
   float average = 0.0 ;
   int i ;
   for( i = 0 ; i < n ; i++ ) average + = x [ i ] ;
   return(average/n) ;
}


THREE IMPORTANT CONCEPTS

For a variable x : EXAMPLE

1) VALUE OF X is -27

2) ADDRESS OF X is &X

3) POINTER TO X is *POINTER_X


EXAMPLE : USE OF POINTER

int x, *pointer_x ;
/*declaration of variable x and pointer x */

x = -27 ; /* Assigning -27 to variable x */

pointer_x = &x ; 
/* Let pointer_x ''pointing'' to the address of 
variable x */

*pointer_x = -54;  
/* Assigning the value -54 to pointer_x also 
assigns -54 to variable x */

POINTER DATA TYPE


DECLARING POINTERS

int x,y ;
int *px, *py ;

CREATING POINTERS USING &

px = &x ;
py = &y ;

ACCESSING OBJECTS VIA POINTERS :

y = *ptr1 ;   /* Read, and then write, the */
*ptr1 = 4 ;   /* variable pointed to by ptr1 */

COPYING POINTERS :

ptr1 = ptr2 ;
if (ptr1 == ptr2) 
    printf ("They point to the same thing\n") ;

CALL-BY-VALUE

/* This program prints n stars */

void stars (n) int n;
{
   for ( ; n > 0 ; n -= 1 )
   {
      putchar ('* ') ;
   }
   putchar ('\n') ;
   return;
}


ADVANTAGES OF CALL-BY-VALUE :

DISADVANTAGES :


CALL-BY-REFERENCE

IN FORTRAN :

PROGRAM REFERENCE
   IMPLICIT NONE
   REAL :: A,B
   INTERFACE
   SUBROUTINE SWAP(X,Y)
     IMPLICIT NONE
     REAL, INTENT(IN OUT) :: X,Y
   END SUBROUTINE SWAP
   END INTERFACE
   ...
   CALL SWAP(A,B)
   ...
   STOP
END PROGRAM REFERENCE
! THIS PROGRAM SWAPS ITS TWO INPUTS
SUBROUTINE SWAP(X,Y)
   IMPLICIT NONE
   REAL, INTENT(IN OUT) :: X,Y
   REAL :: TEMP
   TEMP = X
   X = Y
   Y = TEMP
END SUBROUTINE SWAP


IN C ( USING POINTERS ) :

main ( )
{
   float a,b;
   ...
   swap (&a, &b) ;
   ...
}
/* This function swaps its two inputs. */
void swap(float *ptrx, float *ptry)
{
   float temp ;
   temp = *ptrx ;
   *ptrx = *ptry ;
   *ptry = temp ;
} 


On to the next lecture
Go back to lecture menu

Copyright © 1996 McGill University. All rights reserved.