QMCS 130: Today - PowerPoint PPT Presentation

1 / 17
About This Presentation
Title:

QMCS 130: Today

Description:

Each time we touch/change something, we might break it. March 2005. 3 ... Each is its own int i' If we change one, it doesn't affect the others. March 2005. 10 ... – PowerPoint PPT presentation

Number of Views:11
Avg rating:3.0/5.0
Slides: 18
Provided by: ricks6
Category:
Tags: qmcs | license | today

less

Transcript and Presenter's Notes

Title: QMCS 130: Today


1
QMCS 130 Todays Class
  • Exam Status
  • Functions and C
  • Using Functions
  • Defining Functions
  • Problem Solving with Decomposition
  • Shelf Example (Lab 7)
  • Lab 11

2
Functions and C
  • All C programs are functions
  • main is a function and we always start from
    there
  • We call functions to do outside work
  • Math library, I/O library (printf, scanf)
  • Functions make the work easier
  • We can break programs into smaller pieces
    easier to write
  • If a function works, we can reuse it without
    touching it
  • Each time we touch/change something, we might
    break it

3
Using Functions
  • What are examples of functions we use?
  • How do we use them?
  • What does main mean?

4
Using Functions
  • Example functions printf, scanf
  • Some in class have used pow and ceil
  • Most functions take a list of parameters
  • Also called arguments
  • Tailors the result to specific input parameters
  • Most functions return a value
  • pow(x,y) returns the result of x raised to the
    yth power
  • main returns an int, usually zero (its generally
    ignored)
  • main as a function
  • Declared an int function and usually returns a
    0
  • No argument list (void)

5
Defining Functions
  • Its safest to declare functions BEFORE main
  • Not always essential, but it generally works best
  • Why? Because then the compiler knows the
    function before it has to use the function
  • Whats in the declaration?
  • Consider main

6
Defining Functions
  • Header
  • Data type returned int
  • Functions name main
  • Parameter list in parentheses
  • A list of variable declarations, separated by
    commas
  • Actual contents are in curly braces
  • Just like main
  • Contents are invisible to other functions,
  • EXCEPT through passing arguments and results
  • Always do a return to match the header

7
Some Tricks with Variables
  • What does it mean to pass arguments ?
  • What if we re-use variable names?

8
What does it mean to pass arguments ?
  • Make a copy of the value of a variable
  • The function uses a copy of the value
  • We can change the value in the function, but it
    remains unchanged in the main

9
What if we use the same variable names?
  • We can define int i as often as we want in
    different functions
  • Each is its own int i
  • If we change one, it doesnt affect the others

10
Problem Solving with Decomposition
  • How do you solve a really big problem?
  • How can we divide up a programming problem?

11
Decomposing a Problem
  • Make a list of the things we must do
  • Identify inputs
  • Identify outputs
  • Identify calculations to yield those outputs
  • Each step above might make a good function
  • Or parts of those steps might make good functions
  • If a job is isolated from other data and likely
    to be used more than once, then it should
    probably be a function

12
Decomposition and Functions
  • The tricky part is in variables
  • Its easy to send data in (using the parameter
    list)
  • It can be hard to get data out
  • Good decomposition
  • Minimize the number of variables and results
    passed back and forth between main and the
    functions
  • Sometimes we just have to figure out what the
    right functions are to put in their own
    separate places
  • To I/O or not to I/O a major decision for a
    function
  • Dont do I/O unless thats the point of the
    function
  • Pass results out via arguments instead more
    generally useful

13
Decomposition should make things easier
  • Shorter functions
  • Fewer variables in individual functions
  • Fewer loops in individual functions
  • If a function is simpler, then
  • Its easier to read and understand
  • its easier to debug

14
Decomposition Hassle Factors
  • Extra typing for separate headers and such
  • Requires thought and planning
  • If you get a parameter list wrong, you may have
    to fix things in several different places
  • Good design breaking it into the right pieces

15
Shelf Example (Lab 7)
  • Lets analyze the problem
  • Lets identify some procedures
  • Putting some procedures in Lab 7

16
Looking at Lab 11
  • Break it into the necessary pieces
  • Function Inputs? Outputs?
  • Header?

17
Creative Commons License
  • This work is licensed under the Creative Commons
    Attribution-Share Alike 3.0 United States
    License. To view a copy of this license, visit
    http//creativecommons.org/licenses/by-sa/3.0/us/
    or send a letter to Creative Commons, 171 Second
    Street, Suite 300, San Francisco, California,
    94105, USA.
Write a Comment
User Comments (0)
About PowerShow.com