Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSC 107 - Programming for Science Lecture 23: More on Function Parameters.

Similar presentations


Presentation on theme: "CSC 107 - Programming for Science Lecture 23: More on Function Parameters."— Presentation transcript:

1 CSC 107 - Programming for Science Lecture 23: More on Function Parameters

2 Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’ rope  $2.12 each Wire cutters  $4.49 each How much would one pay for 4 lock picking kits, 8 ropes, and 5 wire cutters? Nothing, they’d steal it.

3 Today’s Goal Today’s lecture continue discussion on functions  Using them, writing them, calling them  Difference between macro and function After today’s lecture, should be comfortable with programmer-written functions

4 Macro vs. Function Execution of code  Macro code copied into call by pre-processor  Function executed on its own Write them using following template:  Macro defined on single line: #define macroName(param1, param2) (code)  Function written on multiple lines: return_type fnName(type param1, type param2) {... }

5 Macro vs. Function Ability to use & create variables  Macro can name values copied that will be copied into multiple locations  Function may declare parameters or locals Returning a value  Macro cannot return data, but can be written so it is evaluated as expression  Non-void function returns single value each call

6 Macro vs. Function Macros do not really exist  “Disappear” after pre-processing  Better way of cutting-and-pasting code  Parameters also handled by cut-and-paste C organizes program via functions  Starts execution at main()  All functions operate identically  Functions can then call & execute others

7 Function Must Do Two Things Function cannot be changed once written int computeExtreme(int val) { static int firstTime = 0; static int extreme; if (firstTime == 0) { extreme = val; firstTime = 1; } if (val > extreme) { // May need to be “<” extreme = val; } return extreme; } Solution: pass a function as a parameter!

8 Function As Parameter Function like other parameters  Must be used, cannot change its value  Name of parameter not related to anything  Functions can already call any other function Calling function with function parameter  Function must be in order among arguments  List function name like a variable  Function can be built-in or programmer-defined

9 Function As Parameter Type of function parameter listed differently return_type (*name)(parameters)  return_type is parameter function’s return type  name is name given to the parameter  parameters should only include types, not names

10 Example of Function Param int greaterThan(int a, int b) { if (a > b) { return 1; } else { return 0; } } int lessThan(int a, int b) { if (a < b) { return 1; } else { return 0; } }

11 int computeExtreme(int val, int (*fn)(int, int)) { static int firstTime = 0; static int extreme; if (firstTime == 0) { extreme = val; firstTime = 1; } if (fn(val, extreme) == 1) { extreme = val; } return extreme; }... printf(“%d”, computeExtreme(4, greaterThan)); printf(“%d”, computeExtreme(8, greaterThan)); printf(“%d”, computeExtreme(6, greaterThan)); printf(“%d”, computeExtreme(6, lessThan)); printf(“%d”, computeExtreme(2, lessThan));

12 Your Turn Try doing following problems on your own

13 For Next Lecture Continue programming assignment #2  Due before next lecture – 11/01/06  Could include updated version of last lab Start week #10 weekly assignment


Download ppt "CSC 107 - Programming for Science Lecture 23: More on Function Parameters."

Similar presentations


Ads by Google