You are on page 1of 16

COMPUTER PROGRAMMING 1

Session 11: Handling functions in C

Iván Amaya
CONTENTS
 FUNCTIONS

 FUNCTION DEFINITION

 FUNCTION USAGE

 EXAMPLE: GET ME THE POWER!

 RANDOM NUMBER GENERATION (RNG)

 EXAMPLE: RPSLS V2.0

 FURTHER READING
COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 2
FUNCTIONS
Allow reusing computer code

Program / debug once

Simplify main() function

Reduces coding time since “modules” are already coded

Reduces debugging time since the program is more organized

Improves readability

Allows splitting work


COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 3
FUNCTION DEFINITION
Functions can be defined before or after main()

B: Whole definition

A: Declaration before main() and whole definition afterwards

Since defining afterwards is more work, it may be preferable to define


them at the beginning

All functions must define the data type to be returned and the data type of
its arguments

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 4


FUNCTION USAGE
User-created functions are used in the same way as standard functions

Since function definition is given within file, no library is required

Beware using library-specific instructions without including proper library

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 5


EXAMPLE: GET ME THE POWER!
Create a program for simulating the pow function

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 6


RANDOM NUMBER GENERATION (RNG)
rand() returns an int value between 0 and RAND_MAX

% operator can be used to limit results

/ operator can be used to change into floats

+, -, *, / operators can be used to define specific range

Numbers are actually pseudo-randoms, so same sequence is generated

Time can be used to seed the RNG and improve results

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 7


RANDOM NUMBER GENERATION (RNG)

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 8


RANDOM NUMBER GENERATION (RNG)

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 9


RANDOM NUMBER GENERATION (RNG)

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 10


RPSLS V2.0

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 11


BONUS: MOVING DOT

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 12


EXERCISES
1. Write a program that generates random numbers and count the amount
of positive and negative numbers. In particular, create a program that uses
a function for generating and counting N numbers in the range [-1, 1].
Then, use that function in your main program for N = 10, 100, 1000,
10000. The program should print lines with the number of trials run (N),
the number of positive values and the number of negative values (separated
by a tab stop). Thus, there should be four printed lines (one for each N).

Remember to initialize the random seed and analyze the resulting data. Are
those values expected? What is happening and why?

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 13


EXERCISES
2. Consider again the “Battle it out!” program. Modify said code to create a
basic Non Playable Character (NPC). Thus, the decisions of the second
player should be random. This means that the PC selects a random battler
type and randomly selects an action each turn.

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 14


EXERCISES
3. Consider again the program for simulating the car movement. Modify
said code to generate a random velocity at each iteration. Be wary of the
limits you impose on the system.

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 15


FURTHER READING
Look up and read information regarding:

Arrays (definition and usage)

Strings (definition, manipulation, and printing)

COMPUTER PROGRAMMING – SESSION 11 IVÁN MAURICIO AMAYA CONTRERAS 16

You might also like