cmsc 1041 functions ii functions that return a value

15
CMSC 104 Functions II Functions that return a value

Upload: lewis-mcbride

Post on 14-Dec-2015

216 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: CMSC 1041 Functions II Functions that return a value

CMSC 104 1

Functions II

Functions that return a value

Page 2: CMSC 1041 Functions II Functions that return a value

CMSC 104 2

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;

}

Page 3: CMSC 1041 Functions II Functions that return a value

CMSC 104 3

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;

}

Page 4: CMSC 1041 Functions II Functions that return a value

CMSC 104 4

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

Page 5: CMSC 1041 Functions II Functions that return a value

CMSC 104 5

#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

5 8 int int float int int float

Local Variables

Page 6: CMSC 1041 Functions II Functions that return a value

CMSC 104 6

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

Page 7: CMSC 1041 Functions II Functions that return a value

CMSC 104 7

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

Page 8: CMSC 1041 Functions II Functions that return a value

CMSC 104 8

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

Page 9: CMSC 1041 Functions II Functions that return a value

CMSC 104 9

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

Page 10: CMSC 1041 Functions II Functions that return a value

CMSC 104 10

Math Library

double sqrt (double x);o returns the square root of x

double pow (double x, double y)o 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)o trigonometric sine of x (x in radians)

All math library functions take doubles as arguments and return doubles

others on page 151 of text

Page 11: CMSC 1041 Functions II Functions that return a value

CMSC 104 11

Common stdlib functions

void exit (int x);o prematurely ends program execution

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

srand (200); int rand (void);

o 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

o num = rand( );

Page 12: CMSC 1041 Functions II Functions that return a value

CMSC 104 12

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 5o num = rand ( ) % 6

How about 1 to 6?o num = 1 + rand( ) % 6;

How about 5 to 20?o num = 5 + rand ( ) % 16;

Page 13: CMSC 1041 Functions II Functions that return a value

CMSC 104 13

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

Page 14: CMSC 1041 Functions II Functions that return a value

CMSC 104 14

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 numberswill 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);

}

}

Page 15: CMSC 1041 Functions II Functions that return a value

CMSC 104 15

<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) ) ;