Presentation is loading. Please wait.

Presentation is loading. Please wait.

 Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 

Similar presentations


Presentation on theme: " Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 "— Presentation transcript:

1  Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7  Exercises: p. 74, #1-3, 12-15  New files/handouts: FunctionEx3.cpp, Homework #2

2  Wednesday, 9/25/02, Slide #2 “Black box” idea of functions  A function can be thought of as a “black box”, meaning its inner workings are hidden  We supply the information it needs (objects to process)  The function does its work privately  We don’t see (in main) how the function does its work PrintAverage( ) homework grade exam grade participate grade

3  Wednesday, 9/25/02, Slide #3 Arguments and Parameters  When we call the function we specify what values to use -- these are called the arguments of the function  The system then matches each argument with the corresponding parameter, assigning the value of the argument to the parameter  LOCAL SCOPE: Variables and parameters declared inside a function exist only in that function  Other functions may have variables or parameters with the same name -- they are different memory locations!

4  Wednesday, 9/25/02, Slide #4 ComputeAverage( ) Functions that return values  Just as parameters send information into a function from where it’s called, a function can send out information when it finishes:  For example, we could modify PrintAverage() so that, instead of printing the average, it computes and returns that average to main(): homework grade exam grade participate grade student average

5  Wednesday, 9/25/02, Slide #5 Example 3: A function to compute and return a weighted average  See FunctionEx3.cpp: //ComputeAverage() Computes and returns a weighted // average of homework, exam, and participation grades double ComputeAverage(double hw, double ex, double pa) { double HwWeight = 0.50; double ExWeight = 0.35; double PaWeight = 0.15; double average = hw * HwWeight + ex *ExWeight + pa * PaWeight; return average; }

6  Wednesday, 9/25/02, Slide #6 Summary of void functions  Syntax of function definition:  Syntax of function call -- A void function call is an entire statement:  Function action: When called each parameter is given the value of the corresponding argument, and the function body is executed. Control then returns to the line immediately after where the function was called. void FunctionName(type1 param1, type2 param2,...) { body of function } FunctionName(arg1, arg2,...);

7  Wednesday, 9/25/02, Slide #7 Summary of value-returning functions  Syntax of function definition:  Syntax of function call -- A value-returning function call is a word, not an entire statement:  Function action: When return statement is reached, the return value is computed, and function terminates. Control is sent back to the calling line, and value is used in place of the function name. return-type FunctionName(type1 param1, type2 param2,...) { body of function: must contain at least one line: return value } cout << FunctionName(arg1, arg2,...); if ( FunctionName(arg1, arg2,...) > 0)....

8  Wednesday, 9/25/02, Slide #8 Scope of variables  Local variables:  Both the variables declared inside a function definition, and the parameters of the function, are “local:” they exist only in the scope of the function  Parameters are local variables that are initialized using the values of the arguments  If the same names are used for local variables in different functions, they refer to different variables  When a function terminates, its local variables are destroyed.

9  Wednesday, 9/25/02, Slide #9 Variables and constants  Sometimes we may want to name a value that we don’t want to permit to be changed.  Example: Pi = 3.14159  The modifier const before the declaration makes it illegal to change the object’s value.  const float Pi = 3.14159; //Makes Pi a constant  Pi = 42.7; //Causes a compiler error!!  Constants give us added protection from programming errors.

10  Wednesday, 9/25/02, Slide #10 Global constants  If several functions make use of the same constant, we can declare it  In the global scope -- not inside any function  Before the functions that want to use it  FunctionA() and FunctionB() can’t use Pi  FunctionC() and FunctionD() can use Pi int FunctionA( ); void FunctionB(int x); const double Pi = 3.14159; float FunctionC( ); double FunctionD (int a, int b);

11  Wednesday, 9/25/02, Slide #11 Dangers of global variables  In general, we avoid the use of global variables, particularly if they are not const objects.  In large modular programs, it may be difficult to understand a function if some object definitions appear elsewhere  If a global object is not const and one function accidentally changes it -- that may adversely affect other functions that use the object. This is called an undesirable side effect.

12  Wednesday, 9/25/02, Slide #12 Attributes of Variables (Summary)  Every variable (and constant) has:  A name  A type (what kind of data and how stored?)  A value (which, of course, can change in the case of a variable, hence the term!)  A scope (in what module(s) does it exist?)


Download ppt " Wednesday, 9/25/02, Slide #1 CS106 Introduction to CS1 Wednesday, 9/25/02  QUESTIONS??  Today: More on functions  Reading: Chapter 3 through 3.7 "

Similar presentations


Ads by Google