Docsity

Prototype-Computer Fundamentals-Lecture Slides, Slides of Computer Fundamentals

Toggle sidebar
bg1
.
5.6 Function Prototypes
• Function prototype
– Function name
– Parameters – what the function takes in
– Return type – data type function returns (default int)
– Used to validate functions
– Prototype only needed if function definition comes after use
in program
– The function with the prototype
int maximum( int x, int y, int z );
• Takes in 3 ints
• Returns an int
• Promotion rules and conversions
– Converting to lower types can lead to errors
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Prototype-Computer Fundamentals-Lecture Slides and more Slides Computer Fundamentals in PDF only on Docsity!

5.6 Function Prototypes

• Function prototype

– Function name

– Parameters – what the function takes in

– Return type – data type function returns (default int)

– Used to validate functions

– Prototype only needed if function definition comes after use

in program

– The function with the prototype

int maximum( int x, int y, int z );

• Takes in 3 ints

• Returns an int

• Promotion rules and conversions

– Converting to lower types can lead to errors

5.6 Function Prototypes

5.7 Header Files

5.8 Calling Functions: Call by Value and

Call by Reference

• Call by value

– Copy of argument passed to function

– Changes in function do not effect original

– Use when function does not need to modify argument

• Avoids accidental changes

• Call by reference

– Passes original argument

– Changes in function effect original

– Only used with trusted functions

• For now, we focus on call by value

5.9 Random Number Generation

• srand function

– <stdlib.h>

– Takes an integer seed and jumps to that location in its

"random" sequence

srand( seed );

– srand( time( NULL ) );/*load <time.h> */

• time( NULL )

– Returns the time at which the program was compiled in

seconds

– “Randomizes" the seed

Outline

fig05_07.c

Outline

fig05_08.c (Part 1

of 3)

Outline

fig05_08.c (Part 2

of 3)

Outline

fig05_09.c (Part 1

of 2)

Outline

fig05_09.c (Part 2

of 2)

Enter seed: 67 Program Output

Enter seed: 867 2 4 6 1 6 1 1 3 6 2 Enter seed: 67 6 1 4 6 2 1 6 1 6 4