Overview - PowerPoint PPT Presentation

About This Presentation
Title:

Overview

Description:

argument list - comma separated list of arguments. Each must have a data type. ... float area; cout endl 'This program calculates the area of a triangle' ... – PowerPoint PPT presentation

Number of Views:19
Avg rating:3.0/5.0
Slides: 31
Provided by: engi47
Learn more at: http://web.cs.wpi.edu
Category:
Tags: all | area | codes | list | of | overview

less

Transcript and Presenter's Notes

Title: Overview


1
Overview
  • creating your own functions
  • calling your own functions

2
function definition
return-value-type function-name (argument
list) declarations and statements Example
int Square (int y) int result
result y y return result
3
function definition
  • return-value-type function-name (argument
    list)
  • declarations and statements
  • return-value-type - any valid data type, plus
    void
  • void  abort ()
  • double sqrt (double)
  • int main ()
  • function-name - any valid identifier. Our
    standard -
  • verb, each word capitalized
  • GetInput
  • IsValid
  • ShowMessage

4
function definition
  • return-value-type function-name (argument
    list)
  • declarations and statements
  • argument list - comma separated list of
    arguments.
  • Each must have a data type. Okay if
    function has no
  • arguments.
  • bool IsEmpty()
  • bool IsEmpty(void)
  • double sqrt (double x)
  • double pow( double x, double y )
  • Each argument is a declaration. (See square
    example)

5
Returning control to caller - no return value
  • void DisplayErrorMessage(int errorNumber)
  • cout ltlt Error Message number is ltlt
    errorNumber
  • or
  • void DisplayErrorMessage(int errorNumber)
  • cout ltlt Error Message number is ltlt
    errorNumber
  • return 

6
Returning control to caller with return value
bool IsEmpty() if (0 bufferCount )
return true else return
false
7
Exercises
1) write a function which accepts an integer
value as input, and returns an integer which is
the cube of that input.
3
y int
Cube
y
int
2) write a function that accepts a char as input
and returns true if the char is a digit from 0 to
9 or false if the character is not a digit from 0
to 9.
ch char
IsDigit
bool true if ch is 0 to 9 false if ch is
not 0 to 9
8
calling a function
int main() Function1 () return
0 void Function1() return
9
function flow of control
int main() cout ltlt About to call Function1
ltlt endl Function1 () cout ltlt Returned
from Function1 ltlt endl return 0 void
Function1() cout ltlt Inside Function1 ltlt
endl return Demonstrate this
10
function flow of control - 2 calls
int main() cout ltlt About to call Function1
ltlt endl Function1 () cout ltlt Returned
from Function1 ltlt endl cout ltlt calling
Function1 again ltlt endl Function1 ()
cout ltlt Returned from calling Function1 again
ltlt endl return 0 void Function1()
cout ltlt Inside Function1 ltlt endl
return Demonstrate this
11
benefits of functions
  • Function code implemented once. Changes happen in
    one place
  • main routine easier to understand because details
    of Function1 removed.
  • When writing main routine only have to think
    about main. When writing Function1 only have to
    think about Function1

12
cout statements as a debug technique
  • can determine flow of control
  • can determine how and when variables change.

13
Inputs and outputs
int main () int xSquared, int x 10
xSquared Square (x) cout ltlt the square
of ltlt x ltlt is ltlt xSquared return
0 int Square (int y) int result
result y y return result What
happens to xSquared, x, y, result? Walk through.
Show memory
14
variables inside a function are not visible
outside the function
  • int main ()
  • int xSquared
  • for (int x 1 x lt 10 x)
  • xSquared Square (x)
  • cout ltlt xSquared ltlt
  • cout ltlt result // would cause a compiler
    error
  • return 0
  • int Square (int y)
  • int result
  • result y y
  • return result

15
y is not visible outside the function
  • int main ()
  • int xSquared
  • for (int x 1 x lt 10 x)
  • xSquared Square (x)
  • cout ltlt xSquared ltlt
  • cout ltlt y // would cause a compiler error
  • return 0
  • int Square (int y)
  • int result
  • result y y
  • return result

16
Exercises
1) Write a main function which calls the Cube
function that you wrote earlier. Have it
calculate the cube of the numbers from 1 to 10
and display them. int Cube (int y)
2) Write a main function which calls the
IsDigit function that you wrote earlier. Have it
read in 10 input characters from a user and for
each one, display whether it is a digit or not.
bool IsDigit (char ch)
17
function prototype
  • Tells compiler
  • type of data returned from function
  • number of arguments function expects to receive
  • type of arguments the function expects to receive
  • order in which those arguments are expected.
  • Example
  • int Square (int y)

18
function prototype
Compiler must see either the function itself or
the function prototype before the function is
actually called in the code. int Square (int
y) int main () int xSquared for
(int x 1 x lt 10 x) xSquared
Square (x) cout ltlt xSquared ltlt
return 0 Function itself may be in
a different file as it is with library functions
19
Area of a Triangle
side1
(float)
AreaTriangle
side2
area of the triangle (float)
(float)
side3
(float)
20
include lt iostreamgt include lt cmathgt using
namespace std float AreaTriangle (float side1,
float side2, float side3) // prototype int
main () float a, b, c
// the three sides of the
triangle float area float area
cout ltlt endl ltlt "This program calculates the area
of a triangle" cout ltlt endl ltlt "with
sides of length 3.0, 4.0, and 5.0" ltlt endl
a 3.0 b 4.0 c 5.0 area
AreaTriangle(a, b, c) cout ltlt endl ltlt
"The area of the triangle is " ltlt area ltlt endl
return 0 / PRE side1, side2, and
side3 are positive numbers that form the sides
of a triangle POST returns the area of a
triangle with sides side1, side2, side3 /
float AreaTriangle (float side1, float side2,
float side3) float s
// local variable - the
semiperimiter s (side1 side2 side3)
/ 2.0 return (sqrt ( s (s - side1) (s
- side2) (s - side3) ) )
21
variables
s, side1, side2, side3 are variables in the
AreaTriangle function
s side3 side2 side1
6.0 5.0 4.0 3.0
                   
 
6.0 5.0 4.0 3.0
area c b a
area, a, b and c are variables in the main
program
At runtime a copy of a, b, c is made and used to
initialize side1, side2 and side3
22
miscellaneous
  • you could skip writing a prototype all together
    and just put the function ahead of main. Not
    intuitive. Makes source code hard to read.
  • you could implement AreaTriangle initially as a
    stub with no other code except return 0

23
1) Write a function named Smallest that takes
three integer inputs and returns an integer that
is the smallest of the three inputs. Write the
prototype for the Smallest function. Write a
program that gets 3 integers from a user and
displays the smallest 2) Write a function that,
given a letter of the alphabet, returns true if
the letter is a vowel (lower or uppercase) and
returns false if the letter is not a vowel.
true if letter is a vowel false if letter is not
a vowel
IsAVowel
letter (char)
3) Write the prototype for IsAVowel. Write a
program to invoke the IsAVowel function. Inputs a
letter and prints out whether it is or is not a
vowel.
24
Automatic conversions
What if the parameter you want to send is
different than that expected by the function?
double Square (double y) int main ()
double xSquared for (int x 1 x lt 10
x) xSquared Square (x)
cout ltlt xSquared ltlt return
0 x converted to double. Works fine.
25
Automatic conversions
What if the parameter you want to send is
different than that expected by the function?
int Square (int y) int main () double
d1 9.8 cout ltlt Square (d1) // displays
81 return 0 return 0 d1 converted to
int. Information is lost. Beware!
26
Promotion rules
  • Specify which types can be converted to other
    types without losing data.
  • As long as you follow the promotion rules then
    conversions are okay.
  • Must promote to a data type higher in the
    hierarchy

27
Promotion hierarchy
long double double float unsigned long int long
int unsigned int int unsigned short int short
int unsigned char char
28
Common programming error
  • Losing data by allowing the compiler to change a
    data type and not follow the promotion rules.

29
Exercises
1) Find the error in each of the following
program segments and explain how to fix it. a)
int sum (int x, int y) int result
result x y b) int sum (int n)
if (0 n) return 0 else
n n n c) in main program
double x 1E10 cout ltlt
"square of 1E10 " ltlt square (x) ltlt endl
int square (int x)
return x x
30
More Exercises
1) Find the error in the following function and
fix it. void displayErrorMessage (int
errorNumber) switch (errorNumber)
case 0 cout ltlt "Fatal Error!" ltlt endl
break case 1
cout ltlt "Error!" ltlt endl
break default cout ltlt "Invalid error
code" ltlt endl return true
Write a Comment
User Comments (0)
About PowerShow.com