QMCS 130: Today - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

QMCS 130: Today

Description:

main' is a function and we always start from there. We call functions to ... Some in class have used 'pow' and 'ceil' Most functions take a list of 'parameters' ... – PowerPoint PPT presentation

Number of Views:15
Avg rating:3.0/5.0
Slides: 17
Provided by: ricks6
Category:
Tags: qmcs | ceil | today

less

Transcript and Presenter's Notes

Title: QMCS 130: Today


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

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
How do we pass arrays?
  • What does the function get when we pass an array?
  • How do we signal the size of an array?
  • What did we talk about yesterday?

8
How do we pass arrays?
  • The function gets the arrays location in RAM
  • The array must indicate its end somehow
  • Option provide second parameter with the array
    size
  • Option put a sentinel at the end of the array,
    like in strings

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

10
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

11
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

12
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

13
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

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

15
Looking at Lab 10
  • Example the minimum function
  • Inputs? Outputs?
  • Header?

16
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