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

Slides:



Advertisements
Similar presentations
Review What is a virtual function? What can be achieved with virtual functions? How to define a pure virtual function? What is an abstract class? Can a.
Advertisements

Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
CSC 107 – Programming For Science. Today’s Goal  Get familiar with multi-dimensional arrays  Creating variables for multi-dimensional array  Multi-dimensional.
Lists Introduction to Computing Science and Programming I.
CSC Programming for Science Lecture 30: Pointers.
ספטמבר 04Copyright Meir Kalech1 C programming Language Chapter 3: Functions.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
CSE 115 Week 4 February 4 - 8, Monday Announcements Software installation fest Tuesday & Wednesday 4-7 in Baldy 21. Software installation fest Tuesday.
Macro & Function. Function consumes more time When a function is called, the copy of the arguments are passed to the parameters in the function. After.
CSC 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
COMP More About Classes Yi Hong May 22, 2015.
CSC 107 – Programming For Science. Today’s Goal  Learn C functions to input and output data  Using printf to print out variables and strings  Read.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Methods Chapter 6. 2 Program Modules in Java What we call "functions" in C++ are called "methods" in Java Purpose Reuse code Modularize the program This.
CSC 107 – Programming For Science. Today’s Goal ALL  Understand why ALL I/O is file I/O  Common bugs to avoid when coding with files in C++  Get a.
CSC 212 – Data Structures Lecture 12: Java Review.
CSC 107 – Programming For Science. Announcements  Textbook available from library’s closed reserve.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing & using functions  How to declare them, use them, & trace them  Could write programs.
Overloading Binary Operators Two ways to overload –As a member function of a class –As a friend function As member functions –General syntax Data Structures.
CSC 107 – Programming For Science. Today’s Goal Variables  Variable  Variable name location to store data  Only for humans; 0 x 7E8A2410 harder to.
COMPUTER PROGRAMMING. Functions What is a function? A function is a group of statements that is executed when it is called from some point of the program.
CSC 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
Announcements  If you need more review of Java…  I have lots of good resources – talk to me  Use “Additional Help” link on webpage  Weekly assignments.
PHY 107 – Programming For Science. Announcements  Slides, activities, & solutions always posted to D2L  Note-taking versions before class, for those.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. Today’s Goal  When lecture over, start understanding pointers  What a pointer is and what it is not  Why pointers.
CSC 107 – Programming For Science. The Week’s Goal.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
CSC 107 – Programming For Science. Today’s Goal  Become familiar with simple arrays  Declaring an array variable  Assigning data to array entries 
CS Class 08 Today  Exercises  Nested loops  for statement  Built-in functions Announcements  Homework #3, group solution to in-class.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
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.
CSC 107 – Programming For Science. Today’s Goal  Discuss writing functions that return values  return statement’s meaning and how it works  When and.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
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.
CSC Programming for Science Lecture 36: Structures.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Arithmetic Expressions Addition (+) Subtraction (-) Multiplication (*) Division (/) –Integer –Real Number Mod Operator (%) Same as regular Depends on the.
Exceptions in C++. Exceptions  Exceptions provide a way to handle the errors generated by our programs by transferring control to functions called handlers.
CSC 212 – Data Structures Lecture 23: Iterators. Question of the Day Thieves guild states it will sell to members: lock picking kits  $0.67 each 40’
CSC 212 – Data Structures Lecture 5: Variables. Problem of the Day Why do underground subway stations always have more escalators going up than down?
Chapter 8 Functions in Depth. Chapter 8 A programmer-defined function is a block of statements, or a subprogram, that is written to perform a specific.
CSC Programming for Science Lecture 8: Character Functions.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  Using typedef to make meaningful names.
CSC Programming for Science Lecture 28: Multi-dimensional Arrays.
Question of the Day  What three letter word completes the first word and starts the second one: DON???CAR.
PHY 107 – Programming For Science. Today’s Goal  Discuss how to hand data to functions  Review loopholes in variables & scoping rules  Ways to get.
1 Introduction to Object Oriented Programming Chapter 10.
CSC Programming for Science Lecture 37 : Course Review.
CSC 107 – Programming For Science. Final Exams Dec. 16, 8AM – 10AM in OM221  Exam for CSC107: Dec. 16, 8AM – 10AM in OM221  Will be done using paper.
PHY 107 – Programming For Science. Science Means Solving Problems  Physics – How does an atom work?
CSC Programming for Science Lecture 27: Arrays as Parameters, Searching, & Sorting.
CSC 107 – Programming For Science. Today’s Goal  Discover best uses of structures in a program  How we can mix pointers inside structures  Assigning.
JAVA METHODS (FUNCTIONS). Why are they called methods? Java is a strictly object-oriented programming language Methods are functions inside of objects.
Functions and Program Structure Chapter 4. Introduction Functions break large computing tasks into smaller ones Appropriate functions hide details of.
Problem of the Day  Why are manhole covers round?
CSC 107 – Programming For Science. Today’s Goal  Write functions that take & return values  How parameters declared and how we call functions  What.
Methods Chapter 6.
CS 326 Programming Languages, Concepts and Implementation
CS 2304 Function Pointers & Lambda Functions
Templates.
Using local variable without initialization is an error.
CSC 253 Lecture 8.
Local Variables variables which are declared within a
METHODS AND BEHAVIORS AKEEL AHMED.
CSC 253 Lecture 8.
Zhen Jiang West Chester University
Cs212: DataStructures Computer Science Department Lab 3 : Recursion.
Templates Generic Programming.
Corresponds with Chapter 5
Presentation transcript:

CSC Programming for Science Lecture 23: More on Function Parameters

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.

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

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) {... }

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

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

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!

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

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

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; } }

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));

Your Turn Try doing following problems on your own

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