Title: CS304: Lecture 3
1CS304 Lecture 3
- Functions and Recursion
- Deitel, Ch. 6
- http//www.cis.uab.edu/cs304
2Things I Wont Cover That You Should Know!TM
- Recursion! Didnt you do this in CS302?
- Read 6.19-6.21, please!
3A Clarifying Note on Definitions
- Implementations of a function require variable
names in the argument list Prototypes do not!
Why? - class test int method1(int)int
method2(string, int) -
- int testmethod1(int intname)
- int testmethod2(string stringname, int intname)
4More on Prototypes
- Prototype Interface Definition
- Formal parameters in the prototype must conform
to the function signature (i.e. types must
match), but names of parameters are of no
importance
5Divide and Conquer
- Ideal way to construct large programs
- Manageability
- Simplicity
- Accomplished in many ways in C
- Functions
- Classes
- Libraries
- STL
6Prepackaged Functions
- Many common functions are provided for you
- Math
- String Parsing
- Time
- Containers
- Vector
- List
- Queue
- Algorithms
7Math
- include ltcmathgt
- All functions are global
- Most functions take doubles as arguments
- See page 242 for a list of common functions
8ltcmathgt functions
- ceil(x) rounds x to the smallest integer not less
than x - exp(x) exponential function ex
- fabs(x) absolute value of x
- floor(x) rounds x to the largest integer not
greater than x - fmod(x, y) remainder of x/y as a floating-point
number - log(x) natural logarithm of x (base e)
- log10(x) logarithm of x (base 10)
- pow(x, y) x raised to the power of y
- Complex numbers, too!
9We cant do math with floats? Or ints?
- Argument Coercion can adapt the math functions to
our uses. - This happens (largely) behind the scenes and
automatically - Square (4.5) 16
- Data could be lost in the conversion
- E.g., long to int, double to int.
- Promotion rules should be followed
- Hierarchy of data types (pp. 250)
- Explicit override can be given by casting
- Also applies to mixed-type expressions
10Other Header Files
- ltiostreamgt Standard I/O
- ltiomanipgt Functions for stream manipulation
- ltcmathgt Math functions
- ltcstdlibgt Mishmash! String/number conversion,
memory allocation, random numbers, etc. - ltctimegt Time functions
- ltvectorgt, ltlistgt, ltdequegt, ltqueuegt, ltstackgt,
ltmapgt, ltsetgt, ltbitsetgt Standard Template
Library ADTs - ltcctypegt Character functions (upper-lower
conversion, type tests) - ltcstringgt Contains C-style string processing
functions - ltexceptiongt, ltstdexceptgt Exception handling
classes - ltfstreamgt Functions for file I/O
- ltstringgt Contains C string class from STL
- See page 251 for a list of common functions
11Generate a Random Number Using the Standard
Library
- include ltiostreamgt
- include ltcstdlibgt
- include ltctimegt
- using namespace std
- int main()
- unsigned int seed
- cout ltlt "Enter seed, or 0 for time "
- cin gtgt seed
- srand((seed 0)?time(0)seed)
- cout ltlt "Your random number is " ltlt rand() ltlt
endl - return 0
12Want to generate specific numbers?
- Within a range, between at least start and less
than end - int number rand()(end-start)start
Only works for integers!!
Scaling Factor
Shifting Factor
Question given a random float number generator,
how to generate a float within a range?
13The Enum Datatype
- Have you ever done this?
- int first 0, second 1, third 2, nth
n-1 - int Sunday 0, Monday 1, , Saturday 6
- Use this instead!
- enum Numbers FIRST, SECOND, THIRD, , NTH
- enum Days SUNDAY, MONDAY, TUESDAY, WEDNESDAY,
THURSDAY, FRIDAY, SATURDAY - This creates a datatype called (Numbers or Days)
that can hold one of the values contained (i.e.
FIRST, SECOND, MONDAY, etc. where appropriate).
This is NOT a class. - See Section 6.8 for this applied
14Storage Classes
- Storage classes are modifiers that change how the
memory associated with the variable behaves
within a function - There are five storage classes (auto, register,
extern, mutable, static), but we will discuss
three - auto Allocate the variable when it is declared,
deallocate it when it is no longer in scope.
This applies to local variables. - register Used with an automatic variable to
suggest to the compiler that this variable be
kept in a hardware register - static Used with global variables
(automatically) or local variables (explicit).
This causes a variable to retain its value
throughout the execution of the program.
Please read Section 6.9 for details!
15Scope
- Scope refers to where in a program a variable is
usable. - Alive ! usable e.g., you cannot use a static
variable defined inside of a function outside of
the function scope. - Six types of scopes Function, File, Block,
Function-Prototype, Class, and Namespace - File scope A static variable declared outside
of any class or function (or is a function!) - Function scope Known throughout a function, no
matter where declared (labels!) - Block scope A variable declared within
- Careful about hiding other variables!
- Function-Prototype scope Variables declared
within the parameter list - See Fig. 6.12 for a scope example
16Function Calls in C
- The C runtime environment can be described as a
stack LIFO structure. - Each function call is represented by an
activation record - Each record holds all information needed for that
particular instance of the function - Each time the function gets called, a new
activation record is added
17Activation Records Example On Board
- include ltiostreamgt
- using namespace std
- int square (int)
- int main() int a 10cout ltlt a ltlt squared
ltlt square( a ) ltlt endlreturn 0 -
- int square(int x) return x x
Read 6.11 for details, nice figure!
18Inline functions
- Function calls are slow!
- the call stack
- Function calls are good!
- e.g., reusable
- Inline function calls are fast and good!
- Acts as advice to the compiler to insert the
body of the function directly
19Cube function
- inline double cube (const double side) return
side side side -
- int main() cout ltlt cube(3.0) ltlt endl
- cout ltlt cube(4.0) ltlt endlreturn 0
20Turns to
- int main () cout ltlt 3.0 3.0 3.0 ltlt
endlcout ltlt 4.0 4.0 4.0 ltlt endlreturn 0
21Reference Parameters
- From our discussion of activation records, you
know that copies occur when parameters are passed
(a new local copy is created). - What if we want to make changes to the parameter
within the function call and keep the change
after leaving the function? - What if we dont want to copy whole structures?
- Passing classes?
22Passing an Argument by Reference
- Pass by value int funcall(int arg1)
- Pass by reference int funcall(int arg1)
- reference (or address-of) operator
- This requires no change to the body of the
function! - Use const modifier to prevent changes to the
structure - Why would we want to do this?
- References are first-class datatypes, and can be
used everywhere in a program
23Reference Example
- void makesquared(int num) num num
-
- int main() int num 3makesquarednum(num)cou
t ltlt num // output 3return 0
24Reference Example
- void makesquared(int num) num num
-
- int main() int num 3makesquarednum(num)cou
t ltlt num // output 9return 0
25Remember what Java does?
- Always pass-by-reference for Classes
- Always pass-by-value for primitive types, e.g.,
int - What if we want to pass two int values to a
function and keep the changes after the function
call is finished?
26Default arguments
- Specify a default argument this way
- int myfun(int x 0, int y 1, int z 2)
- Arguments that have a default should be rightmost
- Must have it make sense! (No ambiguity!)
- This is done in one place, typically the prototype
27Overloading Functions
- Specify different arguments (without conflicts,
of course). - int sqrt(int x)
- float sqrt(float x)
- double sqrt(double x)
- string sqrt(string x)
- int sqrt()
- What is different arguments?
- Number of parameters
- Type of each parameter
- Order of the parameters
? How about float sqrt()
28Templates
- Hate overloading every function for every
argument? - Generalize! Templates allow everything that
makes sense (more on this later in the course) - Our square root function written using templates
- templatelt class T gt
- T sqrt(T value)
- // Do some fancy calculations return answer