for Loops and Functions - PowerPoint PPT Presentation

1 / 25
About This Presentation
Title:

for Loops and Functions

Description:

... is the only witness to a crime and sees the license plate of the get-away car. ... while and the for instructions can be replicated using only the while. ... – PowerPoint PPT presentation

Number of Views:86
Avg rating:3.0/5.0
Slides: 26
Provided by: EdwardDon8
Category:
Tags: functions | loops

less

Transcript and Presenter's Notes

Title: for Loops and Functions


1
for Loops and Functions
  • Making things easier

2
while
  • Called a pretest loop
  • initialize getting started
  • while (condition)
  • one or more instructions
  • increment

3
Example
  • i -20
  • while ( i lt40)
  • ..
  • i i 1

4
Example
  • fileIN gtgt a
  • while ( !fileIN.eof())
  • instructions
  • fileIN gtgt a

5
For loop
  • for ( k 1 k lt 10 k)
  • cout ltlt k ltlt
  • j kk
  • cout ltlt jltltendl

6
Basic form of the for
  • for(initial value final value increment)
  • instructions

7
Note The following are often used as increments
but commands like x x 3 or y y 0.1 can
also be used
  • k
  • k
  • k - -
  • - - k

8
Mystery
  • A mathematician is the only witness to a crime
    and sees the license plate of the get-away car.
    The license is all numeric and of the form
    xx-yy-zz. The mathematician only remembers that
    the license plate satisfied the Pythagorean
    theorem and xx, yy, and zz are all 10 or more.
    Display a list of possible license plates.

9
Solution hint
  • The easiest way to do this is with a triple
    nested loop.
  • for (i 10 i lt 100 i) for ( j 10 j lt
    100 j) for (k 10 k lt 100 k)
  • ..

10
Post test loops - do while
  • do
  • instructions
  • while (condition)

11
Note
  • The do while and the for instructions can be
    replicated using only the while.
  • These are often used to make coding easier.
  • A similar relation exits between ifelse and
    switch.

12
functions
  • A functions is a reusable block of code that can
    be shared.
  • A function is a box that has input (called
    parameters) and at most one well defined output.
  • The data type of the parameters and output must
    be known to use a function properly.
  • You can use functions to breakup a program into
    smaller, easier to code modules.

13
A function prototype
  • A function prototype tells you about the data
    type of the input and output.

Input Output
14
Function Prototypes in C
  • Basic prototype
  • outputDataType functionName ( inputDatatype1
    dummyVariable1, )
  • examples
  • double pow(double a, double b)
  • void main (void)
  • int product ( int a, int b)
  • Note prototypes are terminated with

15
Write prototype statements for the following
functions
  • Display only your name on the screen
  • void myNamePrint (void)
  • Find the area of a circle given the radius
  • double circleArea (double radius)
  • Find the first non-zero digit of a student_id.
  • int firstDigit (long id)
  • Display the largest of 3 integers
  • void printBiggest ( int a, int b, int b)

16
Find the error in the following prototypes
  • int funOne ( double x, float y)
  • no ending
  • void funTwo (void, void)
  • Can not use void, void. Just use void.
  • float funThree( float x, y)
  • each dummy variable must have its own data type.

17
Corrected prototypes
  • int funOne ( double x, float y)
  • void funTwo (void)
  • float funThree( float x, float y)
  • Proper C prototype. Though java an C use dummy
    variable C actually just uses the only data
    type in the prototype. However all work
    correctly.
  • int funOne(double, double)
  • float funThree (float, float)

18
A function definition or how to build your own
  • int product (int x, int y)
  • int t
  • t x
  • t t y
  • return t

19
Some comments
  • A function consists of
  • the header which is similar to the prototype
    statement with no
  • The function body
  • return
  • variables or parameters are never defined (x and
    y in this case)
  • other variable, like t, are called local
    variables.

20
More comments
  • local variables can not be seen outside the
    function in which they are defined
  • in this example the parameters are passed by
    value and can NOT be changed.
  • there can only be one function called main

21
Another look at product
  • // A function definition or how it works
  • int product (int x, int y)
  • int t
  • t x
  • t t y
  • return t

22
Find the error in the following
  • void fun (int x, int y)
  • int z
  • z xx 2xy yy
  • return z

23
Your own function in a C program
  • A function consists of two parts.
  • the function prototype
  • the function definition
  • Common location in source code
  • include
  • one or more function prototypes
  • main
  • one or more function definitions

24
Using a function
  • Use the defined functions as you would any other
    function (like y sqrt(x), y sin(x))

25
  • include ltiostreamgt
  • using namespace std
  • int product (int x, int y)
  • void main (void)
  • int a, b, c
  • a 10
  • b 20
  • c product (a,b)
  • cout ltlt cltlt endl
  • getchar()
  • return
  • int product (int x, int y)
  • int t
  • t x
  • t ty
  • return t
Write a Comment
User Comments (0)
About PowerShow.com