© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference.

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

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)
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
Chapter 7: User-Defined Functions II
Chapter 7 User-Defined Methods. Chapter Objectives  Understand how methods are used in Java programming  Learn about standard (predefined) methods and.
Chapter 7: User-Defined Functions II Instructor: Mohammad Mojaddam.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Structures.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to Arrays.
An Introduction to Programming with C++ Fifth Edition
1 Lecture 18:User-Definded function II(cont.) Introduction to Computer Science Spring 2006.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Pointers.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Lesson 6 Functions Also called Methods CS 1 Lesson 6 -- John Cole1.
Chapter 6: Functions.
Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.
Chapter 4:Functions| SCP1103 Programming Technique C | Jumail, FSKSM, UTM, 2005 | Last Updated: September 2005 Slide 1 Functions Lecture 4 by Jumail Bin.
Call-by-Value vs. Call-by-Reference Call-by-value parameters are used for passing information from the calling function to the called function (input parameters).
C++ for Engineers and Scientists Third Edition
C Functions Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call. The.
Chapter 6: Modularity Using Functions. In this chapter, you will learn about: – Function and parameter declarations – Returning a single value – Returning.
Copyright © 2012 Pearson Education, Inc. Chapter 6: Functions.
Chapter 6: User-Defined Functions
Variable Scope Storage Class Recursion
CPS120: Introduction to Computer Science Functions.
Functions CIS Feb-06. Summary Slide Using Functions Mathematical Functions Misc. Functions Naming Conventions Writing Functions –Function Prototype.
Functions Top-down design Breaking a complex problem into smaller parts that we can understand is a common practice. The process of subdividing a problem.
Classes In C++ 1. What is a class Can make a new type in C++ by declaring a class. A class is an expanded concept of a data structure: instead of holding.
User Defined Functions Chapter 7 2 Chapter Topics Void Functions Without Parameters Void Functions With Parameters Reference Parameters Value and Reference.
Built-In and user-Defined functions Software Design Concepts Lecture IV Dr. Sothy Vignarajah.
1 Announcements Note from admins: Edit.cshrc.solaris instead of.tcshrc Note from admins: Do not use delta.ece.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions Lecture 12.
Functions in C CSE 2451 Rong Shi. Functions Why use functions? – Reusability Same operation, different data – Abstraction Only need to know how to call.
© Janice Regan, CMPT 128, February CMPT 128: Introduction to Computing Science for Engineering Students Pointers.
A FIRST BOOK OF C++ CHAPTER 6 MODULARITY USING FUNCTIONS.
© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Introduction to simple functions.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 6: User-Defined Functions I.
Chapter Functions 6. Modular Programming 6.1 Modular Programming Modular programming: breaking a program up into smaller, manageable functions or modules.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions Parameters passed by reference.
An Introduction to Programming with C++ Sixth Edition Chapter 10 Void Functions.
Functions Math library functions Function definition Function invocation Argument passing Scope of an variable Programming 1 DCT 1033.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students Functions (2)
Chapter 3: User-Defined Functions I
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 6: User-Defined Functions I.
ECE 103 Engineering Programming Chapter 31 C Scopes Herbert G. Mayer, PSU CS Status 8/1/2015 Initial content copied verbatim from ECE 103 material developed.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 6: Functions.
© Janice Regan, CMPT 128, Feb CMPT 128: Introduction to Computing Science for Engineering Students Structures.
Lecture 4 – Function (Part 1) FTMK, UTeM – Sem /2014.
© Janice Regan, CMPT 128, January CMPT 128: Introduction to Computing Science for Engineering Students Introduction to Arrays.
Chapter 7 - Functions. Functions u Code group that performs single task u Specification refers to what goes into and out of function u Design refers to.
Building Programs from Existing Information Solutions for programs often can be developed from previously solved problems. Data requirements and solution.
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.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 4.
Chapter 9: Value-Returning Functions
User-Written Functions
Chapter 6: User-Defined Functions I
Introduction to C++ computers and programming
Chapter 10: Void Functions
Function There are two types of Function User Defined Function
Chapter 6: Functions Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley.
User-Defined Functions
6 Chapter Functions.
Functions Pass By Value Pass by Reference
Chapter 6: User-Defined Functions I
Chapter 9: Value-Returning Functions
Based on slides created by Bjarne Stroustrup & Tony Gaddis
C Parameter Passing.
Presentation transcript:

© Janice Regan, CMPT 102, Sept CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference

© Janice Regan, CMPT 102, Sept User defined functions  Written by the user for applications specific to the developers needs  Part of libraries associated with particular tools being used by the developer to built more complex applications  Graphics libraries  Statistical analysis libraries

© Janice Regan, CMPT 102, Sept Defining a function (1)  The first line of a function is the function definition  The function has a type  void quadraticSolver( double CoeffA, double CoeffB, double CoefC, double *xroot1p, double *xroot2p, int *solutionTypep ) A void function does not return a function value You cannot use a void function as part of an expression  The function has a name or identifier  void quadraticSolver( double CoeffA, double CoeffB, double CoefC, double *xroot1p, double *xroot2p, int *solutionTypep)

© Janice Regan, CMPT 102, Sept Defining a function (1)  The first line of a function is the function definition  The function has a type  void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *root1p, double *root2p, int *solutionTypep )  The function has a name or identifier  void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *root1p, double *root2p, int *solutionTypep )  There is no ; at the end of a function definition The function has 0 or more parameters (arguments)  void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *root1p, double *root2p, int *solutionTypep )  Parameters are passed by value or by reference

© Janice Regan, CMPT 102, Sept Defining a function (2)  Each of a function’s parameters have types  A function may have parameters of more than one type, each parameter must have a type  void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *xroot1p, double *xroot2p, int *solutionTypep )  Parameters passed by reference have types ending with * for example double * is a reference to a double int * is a reference to an int  Programming style: identifiers of variables passed by reference should end with p (indicate reference or pointer to)

© Janice Regan, CMPT 102, Sept Sample Function void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *xroot1p, double *xroot2p, int *solutionTypep ) { double xroot1; double xroot2; int numroots; … *xroots1p = xroot1; *xroots2p = xroot2; *numrootsp = numroots; } Function definition Function body

© Janice Regan, CMPT 102, Sept The body of a function  After the function definition the body of the function is enclosed in {} void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *xroot1p, double *xroot2p, int *solutionTypep ) { variable declarations: may include declarations of variables to hold values passed by reference function declarations calculations to determine values of variables passed by reference (no function value passed back for void function) assure variables passed by reference contain their newly calculated values }

© Janice Regan, CMPT 102, Sept Returning values from a function (1 )  In general a function will take the supplied values of the parameters, calculate a result, then return that result to the calling program as the value of the function call expression  In the process of calculating the return value  Values of parameters passed by value cannot be changed in the calling program by changing the copies in the called functions  Values of parameters passed by reference can be changed in the called function and are simultaneously changed in the calling function.  References to variables passed by reference cannot be changed in the calling program by changing the copies of them in the called program

© Janice Regan, CMPT 102, Sept Returning values from a function (2 )  Our sample function void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *xroot1p, double *xroot2p, int *solutionTypep ) determines the roots of a quadratic equation when we supply the coefficients  The function quadraticSolver will take the values of the parameters, calculate the roots of the specified quadratic and return them as values of variables originally passes to the called function by reference.  The type of the function is void so no values are passes back to the calling function as values of the function calling expression. 

© Janice Regan, CMPT 102, Sept Returning values from a function (3 )  An example of a function calling expression is quadraticSolver( coeffA, coeffB, coeffC, &answer1, &answer2, &solutionKind);  This calling express assumes that the following variables have been declared and given values in the calling program before the function is called (the statement above is executed) double coeffA, coeffB, coeffC; double answer1, answer2; int solutionKind;

© Janice Regan, CMPT 102, Sept Local Variables  Local variables are variables that are declared and used within a particular function (or other scope)  Local variables cannot be used outside the function in which they are declared  The value of a local variable can be returned by a function

© Janice Regan, CMPT 102, Sept Global Variables  Global variables are variables that are declared outside any function (including main)  Global variables can be accessed within any function in the program  Global variables should not usually be used as parameters (arguments) of functions or as variables whose values are returned by functions.  Global variables should be used with care, it is poor programming practice to use global variables rather than passing values as parameters of functions.

© Janice Regan, CMPT 102, Sept Declaring a user’s function  If we wish to use a function in our main program we must declare that function in our main program  If we wish to use another function within a function we are writing we must declare that function within the function we are writing  A function prototype (what we call a function declaration) usually follows the variable declarations within the main program or function  A function prototype tells us the name and type of the function, and the types and order of the parameters.  A function prototype looks like the definition of a function followed by a ; parameter identifiers may be omitted void quadraticSolver( double coeffSq, double coeffLin, double coefConst, double *xroot1p, double *xroot2p, int *solutionTypep ); OR void quadraticSolver( double, double, double, double *, double *, int * );

© Janice Regan, CMPT 102, Sept Using a function  Once we have declared a function we can then use it in the body of our function or main program  If we are using a built in function from a C library we need not declare it (the declaration is inside the libname.h file)  To use a function we write an expression of the form  FunctionName(myintvariable, myfloatvariable, myintvariable2);  sinc(MyValue);  quadraticSolver( coeffA, coeffB, coeffC, &answer1, &answer2, &solutionKind);  The value of the functional calling expression has the same type as the function and will contain the value returned by the function.  The identifier for a variable that is used in place of a parameter must identify a variable with the same type as that parameter.

© Janice Regan, CMPT 102, Sept What happens: calling a function  Main program calls function Value=fun1(value1, value2);  Computer makes a new ‘frame’ for executing function fun1  value1 and value2 are copied to the new frame and used to determine the return value of the function  The return value of the function is sent back to main and becomes the value of the expression fun1(value1, value2)  The new ‘frame’ for fun1 is destroyed  Any changes to the values of variables in the new ‘frame’ are lost Computer memory value1 value2 Variables used in main Variables used in fun1 Copy of value2 Copy of value1 Return value Value of fun1

© Janice Regan, CMPT 102, Sept Consequences of what happens  The value of a parameter cannot be changed inside a function  When the new frame is made the parameters are copied  If the value of the parameter is changed in the function, only the value of the copy in the new ‘frame’ is changed.  When the new ‘frame’ is destroyed all record of what happened within the function is gone  The only information remaining for the calling function after the ‘frame’ is destroyed is the value returned by the function  The function cannot access the original variable it was passed, the value of that variable cannot be changed

© Janice Regan, CMPT 102, Sept Swap function  Consider a function to swap two values void swap( double value1, double value2) { double tmp; tmp = value1; value1 = value2; value2 = tmp; /*The values in variables value1 and value 2 have been exchanged*/ }

© Janice Regan, CMPT 102, Sept What happens: calling a function  Main program calls function swap(value1, value2);  Computer makes a new ‘frame’ for executing function swap  value1 and value2 are copied to the new frame  swap(value1, value2) exchanges the values of value1 and value2 in the function frame  Void return value: nothing is returned  The function ‘frame’ for swap is destroyed  Any changes to the values of variables in the function ‘frame’ are lost  In the main program value1 and value2 still have their original values: They have not been swapped Computer memory value1 value2 Variables used in main Variables used in swap Copy of value2 Copy of value1 Return value Value of fun1

© Janice Regan, CMPT 102, Sept Passing variables by reference  So far we have been using the values of variables as parameters in our functions  This is called passing arguments by value  We have seen that when passing a variable by value it is not possible to change the value of that variable within the function  In some applications we need to change the value of a variable within a function  To do this we must pass the variable by reference  Instead of using the variable as our parameter, we use a reference or pointer to the variable as our argument  Then, we cannot change the reference, but we can change what it refers to (the value of the variable)

© Janice Regan, CMPT 102, Sept What happens when you call a function: 1  Main program declares variables value1 and value2 and initializes the value of value1 to 123  Main program calls function Value=fun1(value1, &value2);  The reference to value2 (&value2) is a variable which contains the address of value2  The notation &value2 means the address in memory of variable value2 Computer memory value1 Variables used in main Value value2=123 &value2

© Janice Regan, CMPT 102, Sept What happens: 2  Computer makes a new ‘frame’ for executing function fun1  Function fun1 has declaration fun1( double value1, double *value2p);  Main program calls function Value=fun1(value1, &value2);  value1 and &value2 are copied to the new frame  In the new frame &value2 is referred to as value2p  The copy of the address (value2p) also points to value2  value2 has a value of 123. This value has not been changed Computer memory Local variables used in fun1 value2p (copy of &value2) Copy of value1 Value value1 Variables used in main Value value2=123 value1 &value2

© Janice Regan, CMPT 102, Sept What happens: 3  In the function value1 and the value2p (*value2p=123) are used to determine the return value of the function  During the determination of the return value of the function (Value) the value referred to by value2p is changed  To change the value in memory location value2 from within the function the following notation is used within the function  *value2p = 345;  This means the value in the variable at the address value2p is assigned to 345  This changes the contents of the memory location at address value2p to 345. value2 in the main program is at location value2p, so its value has been changed to 345 Computer memory Local variables used in fun1 value2p (copy of &value2) Copy of value1 Value value1 Variables used in main Value value2=345 value1 &value2

© Janice Regan, CMPT 102, Sept What happens: 4 Computer memory  The calculation of return value Value is completed.  Value is sent back to the calling program using a return statement (return (Value) );  In the main program the value of the expression fun1(value1, &value2); is now the return value of the function  In the main program the expression Value=fun1(value1, &value2); has placed this return value in variable Value Local variables used in fun1 value2p (copy of &value2) Copy of value1 Value value1 Variables used in main Value value2=345 value1 &value2

© Janice Regan, CMPT 102, Sept What happens: 5 Computer memory  The frame created to execute the function fun1 is destroyed  The copy of the address of the variable value has been destroyed  The variable value2 holds the value that the function fun1 put there value1 Variables used in main Value value2=345 value1 &value2

© Janice Regan, CMPT 102, Sept Swap function  Consider a function to swap two values void swap( double *value1p, double *value2p) { double tmp; tmp = *value1p; *value1p = *value2p; *value2p = tmp; /*The values in addresses value1p and value2p have been exchanged*/ }

© Janice Regan, CMPT 102, Sept What happens when you call a function: 1  Main program declares variables value1 and value2 and initializes their values to 123 and 456  Main program calls function swap(&value1, &value2);  The reference to value1 (&value1) is a variable which contains the address of value1  The reference to value2 (&value2) is a variable which contains the address of value2  The notation &value2 means the address in memory of variable value2 Computer memory Variables used in main &value1 &value2 value2=345 value1=123

© Janice Regan, CMPT 102, Sept What happens: 2  Computer makes a new ‘frame’ for executing function swap  Function swap has declaration swap( double *value1p, double *value2p);  Main program calls function swap(&value1, &value2);  &value1 and &value2 are copied to the new frame  In the new frame &value1 is referred to as value1p  In the new frame &value2 is referred to as value2p  The copy of the address (value2p) also points to value2  value2 has a value of 123. This value has not been changed Computer memory before swap Variables used in main &value1 &value2 value2=345 value1=123 Local variables used in fun1 value1p (copy of &value1) value is 123 value2p (copy of &value2) value is 345

© Janice Regan, CMPT 102, Sept What happens: 3  In the function value1p and the value2p (*value1p=123, *value2p=456) are swapped  While swapping the values referred to by value2p and value1p are changed  To swap the values in memory locations value1p and value2p tmp = *value1p *value1p = *value2p; *value2p = tmp;  This means the value in the variable at the address value1p is assigned to 345 ane the variable at address value2p is assigned to 123  This changes the contents of the memory locations at address value1 and value2 in the main program Computer memory after swap Local variables used in fun1 value1p (copy of &value1) value is 345 Variables used in main &value1 &value2 value2=123 value1=345 value2p (copy of &value2) value is 123

© Janice Regan, CMPT 102, Sept What happens: 5 Computer memory  The frame created to execute the function swap is destroyed  The copy of the address of the variable value has been destroyed  The values of variables value1 and value2 have been swapped Variables used in main &value1 &value2 value2=123 value1=345