Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class RecapRecap Functions and CFunctions and C Using FunctionsUsing Functions.

Similar presentations


Presentation on theme: "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class RecapRecap Functions and CFunctions and C Using FunctionsUsing Functions."— Presentation transcript:

1 March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class RecapRecap Functions and CFunctions and C Using FunctionsUsing Functions Defining FunctionsDefining Functions Problem Solving with DecompositionProblem Solving with Decomposition Shelf Example (Lab 7)Shelf Example (Lab 7) Lab 10Lab 10

2 March 2005 2R. Smith - University of St Thomas - Minnesota Functions and C All C programs are functionsAll C programs are functions –‘main’ is a function and we always start from there We call functions to do ‘outside’ workWe call functions to do ‘outside’ work –Math library, I/O library (printf, scanf) Functions make the work easierFunctions 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 itEach time we touch/change something, we might break it

3 March 2005 3R. Smith - University of St Thomas - Minnesota Using Functions What are examples of functions we use?What are examples of functions we use? How do we use them?How do we use them? What does ‘main’ mean?What does ‘main’ mean?

4 March 2005 4R. Smith - University of St Thomas - Minnesota Using Functions Example functions: printf, scanfExample functions: printf, scanf –Some in class have used “pow” and “ceil” Most functions take a list of “parameters”Most functions take a list of “parameters” –Also called arguments –Tailors the result to specific input parameters Most functions ‘return’ a valueMost functions ‘return’ a value –pow(x,y) returns the result of x raised to the yth power –main returns an int, usually zero (it’s generally ignored) ‘main’ as a function‘main’ as a function –Declared an ‘int’ function and usually returns a 0 –No argument list: “(void)”

5 March 2005 5R. Smith - University of St Thomas - Minnesota Defining Functions It’s safest to declare functions BEFORE ‘main’It’s 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 What’s in the declaration?What’s in the declaration? –Consider ‘main’

6 March 2005 6R. Smith - University of St Thomas - Minnesota Defining Functions HeaderHeader –Data type returned “int” –Function’s name “main” –Parameter list in parentheses A list of variable declarations, separated by commasA list of variable declarations, separated by commas Actual contents are in curly bracesActual 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 headerAlways do a ‘return’ to match the header

7 March 2005 7R. Smith - University of St Thomas - Minnesota How do we pass arrays? What does the function get when we pass an array?What does the function get when we pass an array? How do we signal the size of an array?How do we signal the size of an array? –What did we talk about yesterday?

8 March 2005 8R. Smith - University of St Thomas - Minnesota How do we pass arrays? The function gets the array’s location in RAMThe function gets the array’s location in RAM The array must indicate its end somehowThe 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 March 2005 9R. Smith - University of St Thomas - Minnesota Problem Solving with Decomposition How do you solve a really big problem?How do you solve a really big problem? How can we divide up a programming problem?How can we divide up a programming problem?

10 March 2005 10R. Smith - University of St Thomas - Minnesota Decomposing a Problem Make a list of the things we must doMake a list of the things we must do Identify inputsIdentify inputs Identify outputsIdentify outputs Identify calculations to yield those outputsIdentify calculations to yield those outputs Each step above might make a good functionEach 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 March 2005 11R. Smith - University of St Thomas - Minnesota Decomposition and Functions The ‘tricky part’ is in variablesThe ‘tricky part’ is in variables –It’s easy to send data in (using the parameter list) –It can be hard to get data out “Good” decomposition“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 Don’t do I/O unless that’s the point of the functionDon’t do I/O unless that’s the point of the function Pass results out via arguments instead – more generally usefulPass results out via arguments instead – more generally useful

12 March 2005 12R. Smith - University of St Thomas - Minnesota Decomposition should make things easier Shorter functionsShorter functions Fewer variables in individual functionsFewer variables in individual functions Fewer loops in individual functionsFewer loops in individual functions If a function is simpler, then…If a function is simpler, then… –It’s easier to read and understand –it’s easier to debug

13 March 2005 13R. Smith - University of St Thomas - Minnesota Decomposition Hassle Factors Extra typing for separate headers and suchExtra typing for separate headers and such Requires thought and planningRequires thought and planning If you get a parameter list wrong, you may have to fix things in several different placesIf you get a parameter list wrong, you may have to fix things in several different places

14 March 2005 14R. Smith - University of St Thomas - Minnesota Shelf Example (Lab 7) Let’s analyze the problemLet’s analyze the problem Let’s identify some proceduresLet’s identify some procedures Putting some procedures in Lab 7Putting some procedures in Lab 7

15 March 2005 15R. Smith - University of St Thomas - Minnesota Looking at Lab 10 Example: the minimum functionExample: the minimum function Inputs? Outputs?Inputs? Outputs? Header?Header?

16 March 2005 16R. Smith - University of St Thomas - Minnesota 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.


Download ppt "March 2005 1R. Smith - University of St Thomas - Minnesota QMCS 130: Today’s Class RecapRecap Functions and CFunctions and C Using FunctionsUsing Functions."

Similar presentations


Ads by Google