Functions: - using Library functions - writing our own - PowerPoint PPT Presentation

1 / 13
About This Presentation
Title:

Functions: - using Library functions - writing our own

Description:

ceil(x) nearest integer larger than x. floor(x) nearest integer smaller than x ... sin(x) sine of x, where x is in radians. cos(x) cosine of x, where x is in radians ... – PowerPoint PPT presentation

Number of Views:143
Avg rating:3.0/5.0
Slides: 14
Provided by: msresearch
Category:

less

Transcript and Presenter's Notes

Title: Functions: - using Library functions - writing our own


1
Functions- using Library functions- writing
our own
2
include ltcmathgt
  • a math library
  • x pow( 12, 2 )

" function "
3
functions
  • a math library
  • x pow( 12, 2 )
  • that's what parenthesis are for!!

sent TO a function
returned FROM a function
4
cmath functions
note the value passed TO the function
  • Function Description
  • abs(x) computes absolute value of x
  • sqrt(x) computes square root of x, where x gt0
  • pow(x,y) computes xy
  • ceil(x) nearest integer larger than x
  • floor(x) nearest integer smaller than x
  • exp(x) computes ex
  • log(x) computes ln x, where x gt0
  • log10(x) computes log10x, where xgt0
  • sin(x) sine of x, where x is in radians
  • cos(x) cosine of x, where x is in radians
  • tan(x) tangent of x, where x is in radians

5
writing our own
  • we can write functions right in our main program
  • it involves 6 steps
  • why? to isolate code that you want to run
    (call) repeatedly

6
Step 1 - think of a good name
  • addTwoNumbers( )

7
Step 2 - decide what the function needs to do its
job
  • addTwoNumbers ( int addend1, int addend2)
  • you can name those variables anything you want,
    but you have to specify their type

8
Step 3 - what will the function return as a
service?
  • int addTwoNumbers (int addend1, int addend2)

9
Step 4 - write the function, outside of the main
brackets
  • include ltiostreamgt
  • using namespace std
  • int main ( )
  • int addTwoNumbers ( int addend1, int addend2 )
  • int sum 0
  • sum addend1 addend2
  • return sum

10
Step 5 - Introduce it to main
  • include ltiostreamgt
  • using namespace std
  • int addTwoNumbers ( int addend1, int addend2 )
  • int main ( )
  • int addTwoNumbers ( int addend1, int addend2 )
  • int sum 0
  • sum addend1 addend2
  • return sum

11
Step 6 - use (call) the function from main
  • include ltiostreamgt
  • using namespace std
  • int addTwoNumbers ( int addend1, int addend2 )
  • int main ( )
  • int x, y 12, z 44
  • x addTwoNumbers ( y, z )
  • cout ltlt x

note different variables (the "hand-off")
12
  • include ltiostreamgt
  • using namespace std
  • int addTwoNumbers ( int addend1, int addend2
    )
  • int main ( )
  • int x, y 12, z 44
  • x addTwoNumbers ( y, z )
  • cout ltlt x
  • int addTwoNumbers ( int addend1, int addend2
    )
  • int sum 0
  • sum addend1 addend2
  • return sum

13
return types
  • functions can return ints, doubles, bools, chars,
    strings what if a function doesn't return
    anything?
  • void drawLine ( int length )
Write a Comment
User Comments (0)
About PowerShow.com