150 likes | 321 Views
Functions II. Functions that return a value. A function that returns a value. /*************************************************** ** AverageTwo ** calculates and returns the average of num1 and num2 ****************************************************/
E N D
Functions II Functions that return a value
A function that returnsa value /*************************************************** ** AverageTwo ** calculates and returns the average of num1 and num2 ****************************************************/ float AverageTwo (int num1, int num2) { float average; average = (num1 + num2) / 2.0; return average; }
Using AverageTwo #include <stdio.h> float AverageTwo (int num1, int num2); main ( ) { float average; int num1 = 5, num2 = 8; average = AverageTwo (num1, num2); printf (“The average of %d and %d is %f\n”, num1, num2, average); } float AverageTwo (int num1, int num2) { float average; average = (num1 + num2) / 2.0; return average; }
Local Variables • Functions only “see” their own local variables. This includes main ( ) • The variables that are passed to the function are matched with the formal parameters in the order they are passed • The parameters are declarations of local variables. The values passed are assigned to those variables • Other local variables can be declared within the function
Local Variables #include <stdio.h> float AverageTwo (int a, int b) float AverageTwo (int num1, int num2); { main ( ) float average; { float average; average = (a + b) / 2.0; int num1 = 5, num2 = 8; return average; } average = AverageTwo (num1, num2) ; printf (“The average of “); printf (“%d and %d is %f\n”, num1, num2, average) ; } num1 num2 average a b average 58 int int float int int float
Changes to Local Variables do NOTchange other variables with the same name #include <stdio.h> void AddOne (int num1); void AddOne (int num1) { main () num1++; { printf (“In AddOne: “); int num1 = 5; printf (“num1 = %d\n”, num1); AddOne (num1); } printf (“In main: “); printf (“num1 = %d\n”, num1); num1 } num1 int 5 OUTPUT int In AddOne: num1 = 6 In main: num1 = 5
Data Types andConversion Specifiers Data Type printf scanf conversion conversion float %f %f double %f %lf long double %Lf %Lf int %d %d long int %ld %ld unsigned int %u %u unsigned long int %lu %lu short int %hd %hd char %c %c
Header Files • Header files contain function prototypes for all of the functions found in the corresponding library • It also contains definitions of constants and data types used in that library
Commonly Used Header Files header file Contains function prototypes for <stdio.h> the standard input/output library functions & information used by them <math.h> the math library functions <stdlib.h> the conversion of number to text, text to number, memory allocation, random numbers and other utility functions <time.h> manipulating time and date <ctype.h> functions that test characters for certain properties and that can convert case others see page 159 of text
Math Library • double sqrt (double x); • returns the square root of x • double pow (double x, double y) • x raised to the y power • pow (3.0, 2.0) is 9.0 • pow (8.0, 1.0 / 3) is 2.0 • double sin (double x) • trigonometric sine of x (x in radians) • All math library functions take doubles as arguments and return doubles • others on page 151 of text
Common stdlib functions • void exit (int x); • prematurely ends program execution • void srand (unsigned int x); • “seeds” the random number generator with an unsigned integer that is used to start the calculations that generate the pseudo-random number • srand (200); • int rand (void); • returns an unsigned pseudo-random integer in the range of 0 to 65535 or 0 to 4294967295 depending on the size of an integer on the system your on • num = rand( );
Manipulating what rand() returns • Since rand( ) returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes • Suppose we want only random numbers in the range from 0 to 5 • num = rand ( ) % 6 • How about 1 to 6? • num = 1 + rand( ) % 6; • How about 5 to 20? • num = 5 + rand ( ) % 16;
srand ( ) and rand ( ) • The pseudo-random number generator needs an unsigned int as it’s seed • Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers • To get different random numbers each time we run our program, we have to give a different seed each time
srand ( ) and rand ( ) #include <stdio.h> Since we are always #include <stdlib.h> using the value 67 to seed the #define SEED 67 generator, the same numbers will be produced whenever we main ( ) run our program. { int i, num; srand (SEED); for (i = 0; i < 5; i++) { num = rand ( ); num = 1 + num % 6; printf (“%d\n”, num); } }
<time.h> • One of the most useful functions in the time library is the time( ) function • It returns the time of day as seconds • Since this number is different every time we call it, a common use is as a seed for the random number generator • Each time we run our program, a different sequence of random numbers will be produced • srand (time ( NULL) ) ;