Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions.

Slides:



Advertisements
Similar presentations
Lecture Computer Science I - Martin Hardwick Strings #include using namespace std; int main () { string word; cout
Advertisements

Chapter 4 Constructors and Destructors. Objectives Constructors – introduction and features The zero-argument constructor Parameterized constructors Creating.
Operator overloading redefine the operations of operators
Lecture 14 Today: Overloading: Revision on this Revision on increment operators the assignment operator the [] operator Book: p , 215,
Spring Semester 2013 Lecture 5
Pass by Value. COMP104 Pass by Value / Slide 2 Passing Parameters by Value * A function returns a single result (assuming the function is not a void function)
Factorial Preparatory Exercise #include using namespace std; double fact(double); int fact(int); int main(void) { const int n=20; ofstream my_file("results.txt");
Chapter 16 Exception Handling. What is Exception Handling? A method of handling errors that informs the user of the problem and prevents the program from.
Call By Address Parameters (Pointers) Chapter 5. Functions that “return” more than a single value What if we need more than one value to be returned from.
Functions Prototypes, parameter passing, return values, activation frams.
ELEC 206 Computer Applications for Electrical Engineers Dr. Ron Hayne
Data Structures (Second Part) Lecture 2 : Pointers Bong-Soo Sohn Assistant Professor School of Computer Science and Engineering Chung-Ang University.
Riyadh Philanthropic Society For Science Prince Sultan College For Woman Dept. of Computer & Information Sciences CS 102 Computer Programming II (Lab:
Templated Functions. Overloading vs Templating  Overloaded functions allow multiple functions with the same name.
第三次小考. #include using namespace std; int aaa(int *ib,int a1,int a2) { int u,v; int m=(a1+a2)/2; if(a1==a2)return ib[a1]; u=aaa(ib,a1,m); cout
Chapter 8. Operator Overloading Operator overloading gives the opportunity to redefine C++ Operator overloading refers to redefine C++ operators such.
Chapter 8 Scope, Lifetime and More on Functions. Definitions Scope –The region of program code where it is legal to reference (use) an identifier Three.
This set of notes is adapted from that provided by “Computer Science – A Structured Programming Approach Using C++”, B.A. Forouzan & R.F. Gilberg, Thomson.
Tinaliah, S. Kom.. * * * * * * * * * * * * * * * * * #include using namespace std; void main () { for (int i = 1; i
Triana Elizabeth, S.Kom. #include using namespace std; void main () { for (int i = 1; i
PASSING PARAMETERS 1. 2 Parameter Passing (by Value) Parameters Formal Parameters – parameters listed in the header of the function Variables used within.
More on Functions Programming. COMP104 Lecture 19 / Slide 2 Passing Parameters by Reference l To have a function with multiple outputs, we have to use.
// Functions that take no arguments #include using namespace std; void function1(); void function2( void ); int main() { function1(); function2(); return.
1 Lab Session-4 CSIT121 Fall 2004 Scope of Variables Top Down Design Problem The Solution Lab Exercise for Demo.
Computer Science 1620 Function Scope & Global Variables.
Functions Modules in C++ are called functions and classes
Pass by Reference. COMP104 Pass by Reference / Slide 2 Passing Parameters by Reference * To have a function with multiple outputs, we have to use pass.
Templates Overload function: define more than one function With same function name Different parameter type Different type Different number of parameter.
C++ function call by value The call by value method of passing arguments to a function copies the actual value of an argument into the formal parameter.
Functions Pass by Reference Alina Solovyova-Vincent Department of Computer Science & Engineering University of Nevada, Reno Fall 2005.
COMPUTER PROGRAMMING. Functions’ review What is a function? A function is a group of statements that is executed when it is called from some point of.
CSIS 113A Lecture 8 Parameters.  Two methods of passing arguments as parameters  Call-by-value  ‘copy’ of value is passed  Call-by-reference  ‘address.
Engineering Problem Solving with C++, Second edition, J. Ingber 1 Engineering Problem Solving with C++, Etter/Ingber Chapter 5 Parameter Passing 11/06/13.
1 More Operator Overloading Chapter Objectives You will be able to: Define and use an overloaded operator to output objects of your own classes.
Instructor - C. BoyleFall Semester
Function 2. User-Defined Functions C++ programs usually have the following form: // include statements // function prototypes // main() function // function.
C++ Programming Lecture 13 Functions – Part V The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
1 Object-Oriented Programming Using C++ A tutorial for pointers.
1 Reference Variables Chapter 8 Page Reference Variables Safer version of C/C++ pointer. "Refers to" a variable. Like a pointer. Effectively.
Cop3530sp12. Parameter passing call by value- appropriate for small objects that should not be altered by the function call by constant reference- appropriate.
CMSC 202 Lesson 6 Functions II. Warmup Correctly implement a swap function such that the following code will work: int a = 7; int b = 8; Swap(a, b); cout.
Templates. C++ 2 Outline Function templates  Function template definition  Function template overloading Class templates  Class template definition.
Lecture 9 – Array (Part 2) FTMK, UTeM – Sem /2014.
C++ Programming Lecture 13 Functions – Part V By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 6 Arrays.
1 C++ Classes and Data Structures Course link…..
Chapter 5 Function Basics
Functions and an Introduction to Recursion
Chapter 5 Functions DDC 2133 Programming II.
Pointers and Pointer-Based Strings
#include "std_lib_facilities
Using local variable without initialization is an error.
Dynamic Memory Allocation Reference Variables
Extra.
Chapter 5 Function Basics
Chapter 4 (part 2).
CS150 Introduction to Computer Science 1
Pointers & Functions.
Anatomy of a Function Part 1
CS2011 Introduction to Programming I Methods (II)
Functions and an Introduction to Recursion
Function Overloading.
Pointers and Pointer-Based Strings
Function Defaults C++ permits functions to be declared with default values for some, or all, of its parameters Allows for the function to be called without.
Functions Imran Rashid CTO at ManiWeber Technologies.
Pointers & Functions.
CMSC 202 Lesson 6 Functions II.
Anatomy of a Function Part 1
Unit-1 Introduction to Java
Introduction to Algorithms and Programming COMP151
Presentation transcript:

Chapter 6 Advanced Function Features Pass by Value Pass by Reference Const parameters Overloaded functions

#include using namespace std; // function prototypes void swap(int, int); int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; // Invoke the swap function to attempt // to swap two variables swap(num1, num2); cout << "After invoking swap” << “ num1 is " << num1 << " and num2 is " << num2 << endl; return 0; } void swap(int n1, int n2) { // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; }

Primitive variables are pass-by-value. This means that only the value in the symbol table for that variable is passed to the function. In order to actually change the value of a primitive in a function you must use pass –by-reference. This means that the reference to the variable is passed to the function. The reference is the variable’s actual address in the symbol table. This is done by putting an ampersand before the name of the variable name in the function header.

#include using namespace std; // function prototypes void swap(int&, int&); int main() { // Declare and initialize variables int num1 = 1; int num2 = 2; // Invoke the swap function to attempt to swap two variables swap(num1, num2); cout << "After invoking swap” << “ num1 is " << num1 << " and num2 is " << num2 << endl; return 0; } void swap(int &n1, int &n2) { // Swap n1 with n2 int temp = n1; n1 = n2; n2 = temp; }

Functions that allow changes to happen to their parameters are said to have side effects. We need to make sure that the side effects are always intentional and not by mistake. With primitive data types side effects are pretty much always intentional, we have to use pass by reference on purpose. Objects (as we will find out later) are pass by reference by default and we must use the keyword const to prevent unintended side effects.

Functional Abstraction and Stepwise Refinement Functional Abstraction is separating the use of a function from its implementation. A client can use a function without know the details of the implementation. Those details are hidden in the implementation … encapsulated within the function.

Top-Down Design Top down design is a process used to design a large program by breaking it down into functional bits and writing the details after you have the overall design done.

Function overloading We can define multiple functions with the same name but different signatures. The correct function is executed based on the parameters passed to it. int sum (int, int); double sum (double, double); void sum(int[], int[], int[]); This is very useful for building libraries of functions for special purposes.

Review Questions 6.1, 6.5, 6.7