Presentation is loading. Please wait.

Presentation is loading. Please wait.

CMSC 1041 Functions II Functions that return a value.

Similar presentations


Presentation on theme: "CMSC 1041 Functions II Functions that return a value."— Presentation transcript:

1 CMSC 1041 Functions II Functions that return a value

2 CMSC 1042 A function that returns a value /*************************************************** ** AverageTwo takes two integers arguments ** and returns their average as a float ****************************************************/ float AverageTwo (int num1, int num2) { float average; average = (num1 + num2) / 2.0; return average; }

3 CMSC 1043 Using AverageTwo #include 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; }

4 CMSC 1044 Local Variables l Functions only “see” their own local variable. This includes main ( ) l The variables that are passed to the function are matched with the formal parameters in the order they are passed l The parameters are declarations of local variables. The values passed are assigned to those variables l Other local variables can be declared within the function

5 CMSC 1045 #include float AverageTwo (int num1, num2) float AverageTwo (int num1, int num2); { main ( ) float average; { float average; average = (num1 + num2) / 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 5 8 int int float int int float Local Variables

6 CMSC 1046 More about Local Variables #include void AddOne (int num1); void AddOne (int num1) { main ()num1++; {printf (“In AddOne: “); int num1 = 5;printf (“num1 = %d,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

7 CMSC 1047 Data Types and Conversion 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 shortint%hd%hd char%c%c

8 CMSC 1048 Header Files l Header files contain function prototypes for all of the functions found in the corresponding library l It also contains definitions of constants and data types used in that library

9 CMSC 1049 Commonly Used Header Files header fileContains function prototypes for the standard input/output library functions & information used by them the math library functions the conversion of number to text, text to number, memory allocation, random numbers and other utility functions manipulating time and date functions that test characters for certain properties and that can convert case otherssee page 159 of text

10 CMSC 10410 Math Library l double sqrt (double x); oreturns the square root of x l double pow (double x, double y) ox raised to the y power l pow (3.0, 2.0) is 9.0 l pow (8.0, 0.33) is 2.0 l double sin (double x) otrigonometric sine of x (x in radians) l All math library functions take doubles as arguments and return doubles l others on page 151 of text

11 CMSC 10411 Common stdlib functions l void exit (int x); oprematurely ends program execution l void srand (unsigned int x); o“seeds” the random number generator with an unsigned integer that is used to start the calculations that generate the pseudo-random number l srand (200); l int rand (void); oreturns 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 onum = rand();

12 CMSC 10412 Manipulating what rand() returns l Since rand() returns unsigned integers in a large range, we often have to manipulate the return value to suit our purposes l Suppose we want only random numbers in the range from 0 to 5 onum = rand () % 6 l How about 1 to 6? onum = 1 + rand( ) % 6; l How about 5 to 20? onum = 5 + rand ( ) % 16;

13 CMSC 10413 srand ( ) and rand ( ) l The pseudo-random number generator needs an unsigned int as it’s seed l Although it produces what appear to be random numbers, if we use the same seed, we get the same sequence of random numbers l To get different random numbers each time we run our program, we have to give a different seed each time

14 CMSC 10414 srand ( ) and rand ( ) #include Since we are always include using the value 67 #define SEED 67generator, the same numbers will be main ( )produced whenever we run our {program int i, num; srand (SEED); for (i = 0; i < 5; i++) { num = rand ( ); num = 1 + num % 6; printf (“%d\n”, num); }

15 CMSC 10415 l One of the most useful functions in the time library is the time() function l It returns the time of day as seconds l Since this number is different every time we call it, it’s greatest use is as a seed for the random number generator l Each time we run our program, a different sequence of random numbers will be produced l srand (time ( NULL) );


Download ppt "CMSC 1041 Functions II Functions that return a value."

Similar presentations


Ads by Google