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 :
IN C :
SIMILARITIES :
SUBROUTINES
In FORTRAN :
In C :
In C, a subroutine is just a
function that returns a ''void'', and is called like a function
(no CALL keyword).
ADVANTAGES OF MULTIPLE FILES
EXAMPLE OF #DEFINE AND ITS USE
EXAMPLE SHOWING NEED FOR PARENTHESES IN #DEFINE DIRECTIVE
CODE ILLUSTRATING THE DIFFERENCE BETWEEN FUNCTION AND #DEFINE
USING FUNCTION TO AVERAGE ELEMENTS OF AN ARRAY
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
POINTER DATA TYPE
DECLARING POINTERS
CREATING POINTERS USING &
ACCESSING OBJECTS VIA POINTERS
:
COPYING POINTERS :
CALL-BY-VALUE
ADVANTAGES OF CALL-BY-VALUE :
DISADVANTAGES :
CALL-BY-REFERENCE
IN FORTRAN :
IN C ( USING POINTERS ) :
On to the next lecture
! THIS PROGRAM COMPUTES THE CUBE OF ITS INPUT.
REAL FUNCTION CUBE(X)
IMPLICIT NONE
REAL :: X
CUBE = X * X * X
END FUNCTION CUBE
/* This program computes the cube of its input */
real cube (x)
float x;
{
return (x * x * x);
}
DIFFERENCES :
! 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)
/* 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) ;
#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 ) ;
}
#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);
}
#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
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) ;
}
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 */
int x,y ;
int *px, *py ;
px = &x ;
py = &y ;
y = *ptr1 ; /* Read, and then write, the */
*ptr1 = 4 ; /* variable pointed to by ptr1 */
ptr1 = ptr2 ;
if (ptr1 == ptr2)
printf ("They point to the same thing\n") ;
/* This program prints n stars */
void stars (n) int n;
{
for ( ; n > 0 ; n -= 1 )
{
putchar ('* ') ;
}
putchar ('\n') ;
return;
}
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
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 ;
}
Go back to lecture menu