Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998.

Slides:



Advertisements
Similar presentations
CS 11 C track: lecture 7 Last week: structs, typedef, linked lists This week: hash tables more on the C preprocessor extern const.
Advertisements

C Structures and Memory Allocation There is no class in C, but we may still want non- homogenous structures –So, we use the struct construct struct for.
Chapter 7 Introduction to Procedures. So far, all programs written in such way that all subtasks are integrated in one single large program. There is.
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.
Programming and Data Structure
What is a pointer? First of all, it is a variable, just like other variables you studied So it has type, storage etc. Difference: it can only store the.
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.
Lesson 19 Recursion CS1 -- John Cole1. Recursion 1. (n) The act of cursing again. 2. see recursion 3. The concept of functions which can call themselves.
CS 1400 Chap 6. Functions General form; type Name ( parameters ) { … return value ; } parameters is a list of comma-separated declarations.
1 Homework Turn in HW2 at start of next class. Starting Chapter 2 K&R. Read ahead. HW3 is on line. –Due: class 9, but a lot to do! –You may want to get.
CS Feb 2007 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Function with Output Parameters 4 We have seen that functions can return a single value or no value (void return type) 4 It is quite often useful to be.
 Monday, 9/30/02, Slide #1 CS106 Introduction to CS1 Monday, 9/30/02  QUESTIONS (on HW02, etc.)??  Today: Libraries, program design  More on Functions!
CS Oct 2006 Chap 6. Functions General form; type Name ( parameters ) { … return value ; }
Chapter 6. 2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single Value Pass by Reference Variable Scope.
1 ICS103 Programming in C Lecture 10: Functions II.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 The first step in understanding pointers is visualizing what they represent at the machine level. In most modern computers, main memory is divided into.
CS 106 Introduction to Computer Science I 10 / 15 / 2007 Instructor: Michael Eckmann.
Computer Science 1620 Reference Parameters. Parameters – Pass by Value recall that the parameter of a function is assigned the value of its corresponding.
CS 1400 Chap 6 Functions. Library routines are functions! root = sqrt (a); power = pow (b, c); function name argument arguments.
CS201 – C Functions Procedural Abstraction Code Reuse C Library.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 6: User-Defined Functions I Instructor: Mohammad Mojaddam
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: Function and Parameter Declarations Returning a Single.
CS50 Week 2. RESOURCES Office hours ( Lecture videos, slides, source code, and notes (
Object-Oriented Programming in C++
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.
(6-3) Modular Programming H&K Chapter 6 Instructor - Andrew S. O’Fallon CptS 121 (October 2, 2015) Washington State University.
Chapter 3: Developing Class Methods Object-Oriented Program Development Using Java: A Class-Centered Approach.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
CS1201: PROGRAMMING LANGUAGE 2 FUNCTIONS. OVERVIEW What is a Function? Function Prototype Vs Decleration Highlight Some Errors in Function Code Parameters.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
Methods Awesomeness!!!. Methods Methods give a name to a section of code Methods give a name to a section of code Methods have a number of important uses.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Functions Programming Applications. Functions every C program must have a function called main program execution always begins with function main any.
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 2/2/05CS250 Introduction to Computer Science II Pointers.
1 CSC103: Introduction to Computer and Programming Lecture No 16.
Searching CSE 103 Lecture 20 Wednesday, October 16, 2002 prepared by Doug Hogan.
CS-1030 Dr. Mark L. Hornick 1 References & Pointers.
Chapter 7 Modularity Using Functions: Part II. A First Book of ANSI C, Fourth Edition 2 Variable Scope If variables created inside a function are available.
A First Book of ANSI C Fourth Edition
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 Functions Part 1 Prototypes Arguments Overloading Return values.
APS105 Functions (and Pointers) 1. Modularity –Break a program into manageable parts (modules) –Modules interoperate with each other Benefits of modularity:
CSE 220 – C Programming Pointers.
Module 4 Functions – function definition and function prototype.
Pointers and Pointer-Based Strings
CNG 140 C Programming (Lecture set 10)
Pointer.
Lecture 6 C++ Programming
User-Defined Functions
Object Oriented Programming COP3330 / CGS5409
Pointers & Functions.
Defining methods and more arrays
Parameter Passing in Java
Pass by Reference.
Simulating Reference Parameters in C
CS150 Introduction to Computer Science 1
Pointers and References
CS150 Introduction to Computer Science 1
Pointers and Pointer-Based Strings
Fundamental Programming
CS150 Introduction to Computer Science 1
Pointers & Functions.
ICS103: Programming in C 6: Pointers and Modular Programming
Introduction to Pointers
C Parameter Passing.
Introduction to Pointers
Presentation transcript:

Passing Function Arguments by Reference : A Sample Lesson for CS 1 using the C Programming Language Andy D. Digh Friday, May 29th, 1998

So far, all the function calls we’ve seen have passed one or more arguments into a function using a method we call pass by value. Consider our call to function find_max: Passing Function Arguments by Value max = find_max (num1, num2);

int find_max (int x, int y) /* Input : The values of two integers, x and y */ /* Output : Returns the larger of the two input values */ { int temp; if (x > y) temp = x; else temp = y; return temp; }

Pictorially here showing the pass by value we have: In Our Main Program: Num1:Num2:74

Now, once function find_max is called we have: In Our Main Program: Num2:7 Num1: 4 In find_max: 7x: 4y:Temp: Max:? ?

Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: Num2:7 Num1: 4 In find_max: 7x: 4y:Temp: Max:? 7

Now, the maximum is determined in our subprogram and stored in Temp: In Our Main Program: Num2:7 Num1: 4 In find_max: 7x: 4y:Temp: Max:? 7

Once our function returns our result, the function data area is always erased and will be recreated when the procedure is called again. In Our Main Program: Num2:7 Num1: 4Max: 7

n The functions we’ve seen with pass by value have also always returned a single value. n This has corresponded nicely to our understanding of functions from mathematics. Passing Function Arguments by Value F(x) = x F(2) = = 7

n They’ve also never modified a variable in the calling function. n But, sometimes we aren’t always interested in cranking out one result in our subprograms. That is, we want our subprograms to produce multiple values. Passing Function Arguments by Value

n In effect, we want to be able to alter a variable in the calling function. This is where pass by reference comes in handy. n Problem: Write a subprogram to swap the contents of two variables in the calling function. Passing Function Arguments by Reference

We want to input two integer variables num1 and num2 to a function swap, swap their contents, and return these changes to the calling function. Specification for Swap y: x:5 3 y: x:3 5 Swap Function

Your first thought (a common one) : x = y; y = x; X The Swapping Algorithm Y

Algorithm for Swap A temporary bucket is mandatory to make this algorithm work. temp = x; x = y; y = temp; X Temp Y

Now, we might consider calling this function by: swap (num1, num2); Since C passes arguments to functions by value only, there is no direct way for the called function to alter a variable in the calling function. Coding Our Swap Algorithm

For instance, the following function will not work: void swap (int x, int y) /* WRONG */ { int temp; temp = x; x = y; y = temp; }

Because of pass by value, swap can’t effect the arguments num1 and num2 in the routine that called it. The function we just saw only swaps copies of num1 and num2. Coding Our Swap Algorithm

The way to obtain the desired effect is for the calling program to pass pointers to the values to be changed: swap (&num1, &num2); Since the operator & produces the address of a variable, &num1 is a pointer to num1. In swap itself, the parameters are declared to be pointers, and the operands are accessed indirectly through them.

Pictorially, we want: In Our Main Program: Num2: Num1: In swap: x: y:Temp: Num1 Address Num2 Address

Coding Our Swap Algorithm void swap (int *x, int *y) /* Switch *x and *y */ { int temp; temp = *x; *x = *y; *y = temp; }

Coding Our Swap Algorithm WARNING: Coding this algorithm using global variables is not an acceptable alternative to avoid passing pointers to the values to be changed. Remember the only variables our functions should use are those in the argument list or those declared locally.

Summary of Key Points Functions can modify the variables in the calling function using the pass by reference method. The pass by reference method involves passing pointers to the values to be changed. The swap algorithm is a very common application of the pass by reference method. We’ll see it again in a few weeks when coding sorting algorithms.