Title: CSE 1341 Principles of Computer Science I
1CSE 1341Principles of Computer Science I
2QA about Program 1
3Semester Overview
- Functions
- Character File I/O
- Arrays
- Pointers and Dynamic Memory Allocation
- Structures/Records
- Binary File I/O
4Lecture Overview
- Using inline functions to improve performance
- Function Overloading
5 6Inline Example
- inline float circleArea(float rad)
-
- return(3.14159 rad rad)
-
- int main()
-
- cout.precision(2)
- cout.setf(iosfixed)
- for (float r1.0rlt2.0r0.1)
-
- coutltltRadius ltltrltlt\tArea
- ltltcircleArea(r)ltltendl
-
- return 0
Compiler could replace with 3.14159rr
7Inline Functions
- Compiler may replace function call with actual
code from inline function - Should only be used with very small functions
- Can potentially gain performance increase, but
can cause code to become bloated - When in doubt, dont inline a function
8 9Function Overloading
- Two functions that have the same name, but
different signatures - Signature is
- Name of the function
- Data types of the functions parameters in the
proper order
int square(int) double square(double)
10Function Signatures
void f(int, float, char) void f(char, char) int
f()
Signature can be ascertained from function
prototype.
11Example w/ Overloaded Functions
- int square(int)
- int square(float)
- int main()
-
- int userInt
- float userFloat
- cout ltlt Enter an Integer and Float
- cin gtgt userInt gtgt userFloat
- cout ltlt endl ltlt square(userInt) ltlt
- ltlt square(userFloat)
- return 0
12Overloading contd
int main() int userInt float userFloat
. . . coutltlt square(userInt) ltlt
square(userFloat) . . .
int square(int number) return (number
number) float square(float number) return
(number number)
13Overloaded Quiz
- int someFunction(int)
- float someFunction(int) yes or no
- void someFunction(int, short)
- void someFunction(float) yes or no
- int someFunction(int)
- int someFunction(float) yes or no
- int someFunction(int)
- int somefunction(int, int) yes or no
14Example
- int someFunc(int, char)
- int someFunc(char, char, float)
- int main()
-
- int x, y
- x someFunc(c, v, 4.5)
- y someFunc(3, q)
- return 0
15exit()
- exit()
- causes program to terminate
- provide an integer argument as exit code
- include ltcstdlibgt
void function() cout ltlt BEGIN ltlt endl
exit(0) cout ltlt WILL NEVER BE PRINTED ltlt
endl
16Stubs
- Useful for testing and debugging
- Stub
- dummy function that is called instead of the
actual function in represents - usu. displays test message
void showFees(double memberRate, int months)
cout ltlt showFees called with arguments cout
ltlt memberRate ltlt ltlt months ltlt endl
17Questions??