BITG 1113: Function (Part 2) LECTURE 5

Similar documents
LECTURE 06 FUNCTIONS

Chapter 6: Functions

6.1. Chapter 6: What Is A Function? Why Functions? Introduction to Functions

Standard Version of Starting Out with C++, 4th Edition. Chapter 6 Functions. Copyright 2003 Scott/Jones Publishing

BITG 1113: POINTER LECTURE 12

Functions. Arizona State University 1

Lecture 23: Pointer Arithmetic

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7.

Functions and Recursion

Scope and Parameter Passing

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Functions, Arrays & Structs

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

Call-by-Type Functions in C++ Command-Line Arguments in C++ CS 16: Solving Problems with Computers I Lecture #5

Programming in C++ The manager of a company. Lecture Notes 6. Functions (Procedures) 4/24/2018. he he he. Does Does Does

Name MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

Scope and Parameter Passing

Global & Local Identifiers

EECS402 Lecture 02. Functions. Function Prototype

Programming Language. Functions. Eng. Anis Nazer First Semester

C++ For Science and Engineering Lecture 15

Array Elements as Function Parameters

Course "Data Processing" Name: Master-1: Nuclear Energy Session /2018 Examen - Part A Page 1

BITG 1113: Array (Part 2) LECTURE 9

CHAPTER 4 FUNCTIONS. 4.1 Introduction

CSc Introduc/on to Compu/ng. Lecture 17 Edgardo Molina Fall 2011 City College of New York

4. C++ functions. 1. Library Function 2. User-defined Function

The University Of Michigan. EECS402 Lecture 02. Andrew M. Morgan. Savitch Ch. 3-4 Functions Value and Reference Parameters.

Chapter 6: User-Defined Functions. Objectives (cont d.) Objectives. Introduction. Predefined Functions 12/2/2016

Computer Programming

Chapter 7 - Notes User-Defined Functions II

Exam 3 Chapters 7 & 9

Functions, Arrays & Structs

Agenda. The main body and cout. Fundamental data types. Declarations and definitions. Control structures

Sample Final Exam. 1) (24 points) Show what is printed by the following segments of code (assume all appropriate header files, etc.

Lab 2: Pointers. //declare a pointer variable ptr1 pointing to x. //change the value of x to 10 through ptr1

CS111: PROGRAMMING LANGUAGE II

(5-1) Object-Oriented Programming (OOP) and C++ Instructor - Andrew S. O Fallon CptS 122 (February 4, 2019) Washington State University

CSCE 206: Structured Programming in C++

CSc Introduc/on to Compu/ng. Lecture 19 Edgardo Molina Fall 2011 City College of New York

Programming in C++: Assignment Week 6

! Pass by value: when an argument is passed to a. ! It is implemented using variable initialization. ! Changes to the parameter in the function body

CSC 126 FINAL EXAMINATION Spring Total Possible TOTAL 100

Object Oriented Design

CS2255 HOMEWORK #1 Fall 2012

C++ Programming: From Problem Analysis to Program Design, Fifth Edition. Chapter 6: User-Defined Functions I

C++ Functions. Last Week. Areas for Discussion. Program Structure. Last Week Introduction to Functions Program Structure and Functions

Lesson 05 Methods. MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Arrays in C++ Instructor: Andy Abreu

Chapter 6 - Notes User-Defined Functions I

CAAM 420 Fall 2012 Lecture 29. Duncan Eddy

Chapter 4 Computer Science with C++ Name: Review Worksheet A Mr. Ferwerda

Ch 6. Functions. Example: function calls function

Homework #3 CS2255 Fall 2012

Basic memory model Using functions Writing functions. Basics Prototypes Parameters Return types Functions and memory Names and namespaces

Implementing an ADT with a Class

Introduction to C++ Introduction to C++ 1

8. Functions (II) Control Structures: Arguments passed by value and by reference int x=5, y=3, z; z = addition ( x, y );

BITG 1113: Array (Part 1) LECTURE 8

Topics. Functions. Functions

CSC 126 FINAL EXAMINATION FINAL Spring 2012 B. Name (last, First) Instructor. Total Possible. Received

Lecture on pointers, references, and arrays and vectors

FORM 2 (Please put your name and form # on the scantron!!!!)

Fundamentals of Programming. Lecture 19 Hamed Rasifard

Week 3: Pointers (Part 2)

More on Functions. Lecture 7 COP 3014 Spring February 11, 2018

Note 12/1/ Review of Inheritance Practice: Please write down 10 most important facts you know about inheritance...

Module 07: Programming in C++

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

QUIZ Lesson 4. Exercise 4: Write an if statement that assigns the value of x to the variable y if x is in between 1 and 20, otherwise y is unchanged.

C++ For Science and Engineering Lecture 2

2. Functions I: Passing by Value

3. Functions. Modular programming is the dividing of the entire problem into small sub problems that can be solved by writing separate programs.

Introduction to Programming. Lecture 6: Functions & Program Structure

Local and Global Variables

Using Parallel Arrays. Parallel Array Example

Come and join us at WebLyceum

CSC1322 Object-Oriented Programming Concepts

UEE1302(1066) F12: Introduction to Computers and Programming Function (II) - Parameter

CPSC 427: Object-Oriented Programming

by Pearson Education, Inc. All Rights Reserved. 2

Chapter 4: Subprograms Functions for Problem Solving. Mr. Dave Clausen La Cañada High School

CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II

Function. Mathematical function and C+ + function. Input: arguments. Output: return value

COMP 111 PROGRAMMING I MODULARITY USING FUNCTIONS

C++ Programming Lecture 11 Functions Part I

CMSC202 Computer Science II for Majors

C++ Programming: Functions

BITG 1233: Introduction to C++

Exam 2. CSI 201: Computer Science 1 Fall 2016 Professors: Shaun Ramsey and Kyle Wilson. Question Points Score Total: 80

Lecture 4. 1 Statements: 2 Getting Started with C++: LESSON FOUR

C Functions. Object created and destroyed within its block auto: default for local variables

Lab Instructor : Jean Lai

Parameter Passing Mechanisms. Reference Parameters

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

What we will learn about this week: Declaring and referencing arrays. arrays as function arguments. Arrays

1. Which of the following best describes the situation after Line 1 has been executed?

2/3/2018 CS313D: ADVANCED PROGRAMMING LANGUAGE. Lecture 3: C# language basics II. Lecture Contents. C# basics. Methods Arrays. Dr. Amal Khalifa, Spr17

Pointers and Strings. Adhi Harmoko S, M.Komp

Transcription:

BITG 1113: Function (Part 2) LECTURE 5 1

Learning Outcomes At the end of this lecture, you should be able to: explain parameter passing in programs using: Pass by Value and Pass by Reference. use reference variables as parameters. 2 LECTURE 5

Passing Variables to a Function 3 A variable has both a value and a unique address (memory location) You can pass/send either the variable s value or its address to the receiving function using : Pass by value Pass by reference 3 LECTURE 5

Sending Data into a Function Values are passed to a function during the function call: c = pow(a, b); Values passed to function are arguments. An argument can also be called an actual parameter. Variables in a function that hold the values passed as arguments are parameters. A parameter can also be called a formal parameter. An argument will be matched to a parameter.

Passing Multiple Arguments When calling a function and passing multiple arguments: the number of arguments in the call must match the function prototype and definition. the first argument will be used to initialize the first parameter, the second argument to initialize the second parameter, etc.

Passing Variables By Value 6 In a pass by value, the computer passes the content of each actual variable (argument) to the parameter of the receiving function (creates a copy/duplicate of the actual value). Receiving function is not given access to the actual variable in memory. It cannot change value stored inside the actual variable. By default, variables are passed by value in C++. 6 LECTURE 5

Pass By Value Concept = 7 ; = 5 ; arguments The values of the two numbers have NOT been exchanged by function exchange Output??? parameters 7 7 5 5 Pass by value (num1 is a copy of a, num2 is a copy of b) Purpose of Function exchange : To exchange the values of two numbers 7 LECTURE 5

Pass By Value : Programming Example //prototype declaration #include <iostream> void fun (int x); int main ( ) { int a = 5; cout << a << endl; fun(a); cout << a << endl; return 0; } // main void fun (int x) { x = x + 3; return; } // fun a 5 One-way communication x 5 Only a copy Output : 5 5 Pass by value (x is a copy of a) 8 LECTURE 5

9 Passing Variables By Reference Passing a variable s address is referred to as passing by reference Receiving function has access to the passed variable (the actual variable) Use when you want the receiving function to change contents of actual variable To pass a variable by reference in C++ : Include an & (the address-of operator) before the name of the corresponding formal parameter in receiving function s header 9 LECTURE 5

Using Reference Variables as Parameters A mechanism that allows a function to work with the original argument (value) from the function call, not a copy of the argument (value) Allows the function to modify values stored in the calling environment (modify original values) Provides a way for the function to give more than one value as the RESULT of using the function. 10 LECTURE 5

Passing by Reference A reference variable is an alias (different name) for another variable. Its formal parameter (known as referenced parameter) declared with an ampersand (&). Example : void getdimensions(int&, int&); Changes to a reference variable value are actually changing the original value for which it refers to. Use reference variables to implement passing parameters by reference 11 LECTURE 5

Pass By Reference Concept The values of the two numbers have been exchanged by function exchange Output??? = 7 ; = 5 ; 7 5 5 7 Pass by Reference (num1 is an alias for a, num2 is an alias for b) Reference variables 7 Purpose of Function exchange : To exchange the values of two numbers 12 LECTURE 5

Pass By Reference : Programming Example //prototype declaration #include <iostream> void fun (int& x); int main ( ) { int a = 5; cout << a << endl; fun(a); cout << a << endl; return 0; } // main void fun (int& x) { x = x + 3; return; } // fun a 5 8 x Output : 5 8 Pass by Reference (x is an alias for a) 13 LECTURE 5

Reference Variable : TAKE NOTE Each reference parameter must contain & Space between type and & is unimportant Must use & in both function prototype and header Argument passed to reference parameter must be a variable cannot be an expression or constant Use when appropriate don t use when argument should not be changed by function, or if function needs to return only 1 value 14 LECTURE 5

15 Parameter passing : TAKE NOTE (pass by value vs. pass by reference) Pass by value a copy of data is created and placed in a local variable in the called function ensure that regardless of how the data is manipulated and changed in the called function, the original data in the calling function are safe and unchanged Pass by reference sends the address of a variable to the called function use the address operator (&) in the parameter of the called function anytime we refer to the parameter, therefore we actually referring to the original variable if the data is manipulated and changed in the called function, the original data in the calling function are changed 15 LECTURE 5 -END-