CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang.

Slides:



Advertisements
Similar presentations
AU/MITM/1.6 By Mohammed A. Saleh 1. Arguments passed by reference  Until now, in all the functions we have seen, the arguments passed to the functions.
Advertisements

Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 6 Functions.
Function Overloading Can enables several function Of same name Of different sets of parameters (at least as far as their types are concerned) Used to create.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
 2003 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction.
 2003 Prentice Hall, Inc. All rights reserved. 1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined.
C++ Functions CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
Functions. 3Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece more manageable than the original program.
 2007 Pearson Education, Inc. All rights reserved C++ as a Better C; Introducing Object Technology.
1 Functions Modules: functions and classes Programs use new and “prepackaged” modules –New: programmer-defined functions, classes –Prepackaged: from the.
Introduction to C++ Systems Programming.
You gotta be cool. C++ ? C++ Standard Library Header Files Inline Functions References and Reference Parameters Default Arguments and Empty Parameter.
Operator Precedence First the contents of all parentheses are evaluated beginning with the innermost set of parenthesis. Second all multiplications, divisions,
Programming in C++ Language ( ) Lecture 6: Functions-Part2 Dr. Lubna Badri.
Review 2 - Chapter 3 and 4 Function 1 Function 2 Function 3 Function 4
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
Introduction to C++ Systems Programming. Systems Programming: Introduction to C++ 2 Systems Programming: 2 Introduction to C++  Syntax differences between.
Chapter 6: User-Defined Functions
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 7 Clicker Questions September 22, 2009.
Chapter 4: Subprograms Functions for Problem Solving Mr. Dave Clausen La Cañada High School.
1 Chapter 6 - Functions Outline 6.1Introduction 6.2Program Components in C++ 6.6Math Library Functions 6.4Functions 6.5Function Definitions 6.6Function.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 15 - C++ As A "Better C" Outline 15.1Introduction 15.2C A Simple Program: Adding Two Integers.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions.
1 Chapter 3 - Functions Outline 3.1Introduction 3.2Program Components in C++ 3.3Math Library Functions 3.4Functions 3.5Function Definitions 3.6Function.
Structure Programming Lecture 8 Chapter 5&6 - Function – part I 12 December 2015.
 In this chapter you ‘’ll learn: ◦ To construct programs modularly from functions ◦ To use common math library functions ◦ The mechanism for passing.
1 Lecture 14 Functions Functions with Empty Parameter Lists Empty parameter lists  void or leave parameter list empty  Indicates function takes.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 6 Functions.
1 Introduction to C++ Noppadon Kamolvilassatian Department of Computer Engineering Prince of Songkla University.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
EEL 3801 C++ as an Enhancement of C. EEL 3801 – Lotzi Bölöni Comments  Can be done with // at the start of the commented line.  The end-of-line terminates.
1 Advanced Topics in Functions Lecture Unitary Scope Resolution Operator Unary scope resolution operator ( :: )  Access global variable if.
 2003 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions Outline 3.15Functions with Empty Parameter Lists 3.16Inline Functions 3.17References.
EC-111 Algorithms & Computing Lecture #6 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 3 - Functions.
 2003 Prentice Hall, Inc. All rights reserved Storage Classes Variables have attributes –Have seen name, type, size, value –Storage class How long.
Reference Parameters There are two ways to pass arguments to functions: pass- by-value and pass-by-reference. pass-by-value –A copy of the arguments’svalue.
 2000 Prentice Hall, Inc. All rights reserved Introduction Divide and conquer –Construct a program from smaller pieces or components –Each piece.
C++ Programming Lecture 12 Functions – Part IV
Chapter 6 Functions. 6-2 Topics 6.1 Modular Programming 6.2 Defining and Calling Functions 6.3 Function Prototypes 6.4 Sending Data into a Function 6.5.
CHAPTER 4 FUNCTIONS Dr. Shady Yehia Elmashad. Outline 1.Introduction 2.Program Components in C++ 3.Math Library Functions 4.Functions 5.Function Definitions.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
 2000 Prentice Hall, Inc. All rights reserved Program Components in C++ Function definitions –Only written once –These statements are hidden from.
Prepared by Andrew Jung. Contents A Simple program – C++ C++ Standard Library & Header files Inline Functions References and Reference Parameters Empty.
FUNCTIONS (C) KHAERONI, M.SI. OBJECTIVE After this topic, students will be able to understand basic concept of user defined function in C++ to declare.
1 This week Basics of functions Stack frames Stack vs. Heap (brief intro) Calling conventions Storage classes vs. scope Library functions Overloading.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Chapter 15 - C++ As A "Better C"
Functions.
Functions.
-Neelima Singh PGT(CS) KV Sec-3 Rohini
Dr. Shady Yehia Elmashad
IS Program Design and Software Tools Introduction to C++ Programming
Introduction to C++ Systems Programming.
Chapter 3 - Functions Outline 3.1 Introduction
CSC113: Computer Programming (Theory = 03, Lab = 01)
FUNCTIONS IN C++.
Dr. Shady Yehia Elmashad
6.11 Function Call Stack and Activation Records
Functions Najah Alsubaie Kingdom of Saudi Arabia
Dr. Shady Yehia Elmashad
Variables have attributes
Chapter 15 - C++ As A "Better C"
Chapter 3 - Functions Outline 3.1 Introduction
Presentation transcript:

CPSC230 Computers & Programming I Lecture Notes 20 Function 5 Dr. Ming Zhang

Function 5 ( L20 ) u Functions with Empty Parameter Lists u Inline Functions u Function Call-by-Value u Function Call-by- Reference u Reference for Other Variables u Default Arguments u Unary Scope Resolution Operator (::) u Function Overloading u Exercise/Home Work Dr. Ming Zhang

Functions with Empty Parameter Lists u In C++, an empty parameter list is specified by writing either void or nothing at all in parentheses. u The prototype void print( ); specifies that function print does not take any arguments and does not return a value u Examples void function1( ) { cout<<“function1 takes no argument”<<endl;} void function2(void) { cout<<“function2 takes no argument”<<endl;} Dr. Ming Zhang

Inline Functions u Function calls involve execution-time overhead. C++ provides inline functions to help reduce function-call overhead - especially for small functions. u The qualifier inline before a function’s return type in the function definition ”advises” the compiler to generate a copy of the function’s code in place (when appropriate) to avoid a function call -saving running time. u Example inline double cube(const double s) { return s*s*s;} Dr. Ming Zhang

Function Call-by-Value u When an argument is passed call-by-value, a copy of argument’s value is made and passed to called function. u Changes to the copy do not affect the original variable’s value in the caller. u This prevents the accidental side effects. u Example …... double cube(const double s) { return s*s*s;} …... cin >> side; cout << “Volume of cube is” << cube(side); …... Dr. Ming Zhang

Function Call-by- Reference u With call-by-reference, the caller gives the called function the ability to access the caller’s data directly, and to modify that data if the called function so choose. u A reference parameter is an alias for its corresponding argument. u To indicate that a function parameter is passed by reference, simple follow the parameter’s type in the function prototype by an ampersand (&); use the same convention when listing the parameter’s type in the function header. Dr. Ming Zhang

Example of Call-by-Reference (Fig.3.20) u …... int x=2, z=4; cout <<x<<squareByValue(x) <<x<< endl; cout <<z; squareByReference(z); cout<<z<<endl; …... int squareByValue( int a) { return a*=a; } //caller’s argument not modified void squareByRefernce( int &cRef) { cRef *= cRef; } // caller’s argument modified u output: // x x*x x // z z*z z Dr. Ming Zhang

Reference for Other Variables u Reference can also be used as aliases for other variables within a function. u Once a reference is declared as an alias for another variable, all operations supposedly performed on the alias (the reference) are actually performed on the original variable itself. u The alias (the reference) is simply another name for the original variable. u Reference variables mush be initialized in their declarations. Dr. Ming Zhang

Example of Reference Variable (Fig. 3.21) u …. int main( ) { int x =3, &y = x; // y is now an alias for x cout <<“x=”<<x<<“ “<<“y=“<<y<<endl; y=7;// y is 7 now cout <<“x=”<<x<<“ “<<“y=“<<y<<endl; return 0; } u Output: x=3 y=3 x=7 y=7 // x is 7 now Dr. Ming Zhang

Default Arguments u Function calls may commonly pass a particular value of an argument, the programmer can provide a default value for that argument (default argument). u When a default argument is omitted in a function call, the default value of that argument is automatically inserted by the compiler and passed in the call. u Default arguments can be constant, global variables, or function calls. u Default arguments must be the rightmost (trailing) argument in a function’s parameter list. u The default values should only be defined in the function prototype. Dr. Ming Zhang

Example of Default Arguments (Fig. 3.23) int boxVolume(int length=1, int width=1, int height =1); ….. cout <<“Default box volume:” << boxVolume( ); cont<<“\nLength 10, width 1, & height 1:” << boxVolume(10); ….. int boxVolume(int length, int width, int height) { Return length*width*height; } Output: Default box volume: 1 Length 10, width 1& height 1: 10 Dr. Ming Zhang

Unary Scope Resolution Operator (::) u It is possible to declare local and global variable of the same name. u C++ provides the unary scope resolution operator(::) to access a global variable when a local variable of the same name is in scope. u The unary scope resolution operator (::) can NOT be used to access a local variable of the same name in an outer block. Dr. Ming Zhang

Example of Operator (::) (Fig. 3.24) …... const double PI = int main( ) { const float PI = static_cast<float. ( :: PI); cout << setprecision (20) <<“Local float value of PI =“ << PI <<endl <<Global double value of PI =“ << ::PI <<endl; return 0 } output: Local float value of PI = Global double value of PI = Dr. Ming Zhang

Function Overloading u C++ enables several functions of the same name to be defined as long as these function have different sets of parameters (at least their types are concerned). This capability is called function overloading. u Function overloading is commonly used to create several functions of the same name that perform similar tasks, but on different data types. u Overloaded functions can have different return types, but must have different parameter lists. Dr. Ming Zhang

Example of Function Overloading(Fig. 3.25) …… int square (int x) { return x*x ;} double square(double y) { return y*y;} int main( ) { cout<<The square of integer 7 is “<< square(7) <<\nThe square of double 7.5 is “<< square(7.5) ; return 0;} Output: The square of integer 7 is 49 The square of double 7.5 is Dr. Ming Zhang

Question 1 - Exercise/Home Work (1) Write a complete C++ program with the two alternate functions specified below, of which each simply triples the variable count defined in main. Then compare and contrast the two approaches. These two functions are: a) Function tripleCallByValue that passes a copy of count call-by-value, triples the copy and return the new value. B) Function tripleByReference that passes count with true call-by-reference via a reference parameter and triples the original copy of count through its alias (i.e., the reference parameter). Dr. Ming Zhang

Question 2 -Exercise/Home Work (1) Write function distance that calculates the distance between two points (x1, y1) and (x2, y2). All numbers and return values should be of type double. Then write a working program to calculate the distance between two points. The user of program could input the values of these two points. Dr. Ming Zhang