CSE 1341 Principles of Computer Science I - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

CSE 1341 Principles of Computer Science I

Description:

Compiler may replace function call with actual code from inline function ... Signature can be ascertained from. function prototype. 11. Example w/ Overloaded Functions ... – PowerPoint PPT presentation

Number of Views:141
Avg rating:3.0/5.0
Slides: 18
Provided by: markfo7
Category:

less

Transcript and Presenter's Notes

Title: CSE 1341 Principles of Computer Science I


1
CSE 1341Principles of Computer Science I
  • Note Set 4

2
QA about Program 1
3
Semester Overview
  • Functions
  • Character File I/O
  • Arrays
  • Pointers and Dynamic Memory Allocation
  • Structures/Records
  • Binary File I/O

4
Lecture Overview
  • Using inline functions to improve performance
  • Function Overloading

5
  • Inline Functions

6
Inline 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
7
Inline 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
  • Function Overloading

9
Function 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)
10
Function Signatures
void f(int, float, char) void f(char, char) int
f()
Signature can be ascertained from function
prototype.
11
Example 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

12
Overloading 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)
13
Overloaded 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

14
Example
  • 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

15
exit()
  • 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
16
Stubs
  • 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
17
Questions??
  • ?
Write a Comment
User Comments (0)
About PowerShow.com