Download presentation
Presentation is loading. Please wait.
1
© Janice Regan, CMPT 102, Sept. 2006 0 CMPT 102 Introduction to Scientific Computer Programming Functions Call by reference
2
© Janice Regan, CMPT 102, Sept. 2006 1 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
3
© Janice Regan, CMPT 102, Sept. 2006 2 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)
4
© Janice Regan, CMPT 102, Sept. 2006 3 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
5
© Janice Regan, CMPT 102, Sept. 2006 4 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)
6
© Janice Regan, CMPT 102, Sept. 2006 5 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
7
© Janice Regan, CMPT 102, Sept. 2006 6 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 }
8
© Janice Regan, CMPT 102, Sept. 2006 7 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
9
© Janice Regan, CMPT 102, Sept. 2006 8 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.
10
© Janice Regan, CMPT 102, Sept. 2006 9 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;
11
© Janice Regan, CMPT 102, Sept. 2006 10 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
12
© Janice Regan, CMPT 102, Sept. 2006 11 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.
13
© Janice Regan, CMPT 102, Sept. 2006 12 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 * );
14
© Janice Regan, CMPT 102, Sept. 2006 13 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.
15
© Janice Regan, CMPT 102, Sept. 2006 14 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
16
© Janice Regan, CMPT 102, Sept. 2006 15 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
17
© Janice Regan, CMPT 102, Sept. 2006 16 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*/ }
18
© Janice Regan, CMPT 102, Sept. 2006 17 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
19
© Janice Regan, CMPT 102, Sept. 2006 18 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)
20
© Janice Regan, CMPT 102, Sept. 2006 19 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
21
© Janice Regan, CMPT 102, Sept. 2006 20 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
22
© Janice Regan, CMPT 102, Sept. 2006 21 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
23
© Janice Regan, CMPT 102, Sept. 2006 22 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
24
© Janice Regan, CMPT 102, Sept. 2006 23 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
25
© Janice Regan, CMPT 102, Sept. 2006 24 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*/ }
26
© Janice Regan, CMPT 102, Sept. 2006 25 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
27
© Janice Regan, CMPT 102, Sept. 2006 26 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
28
© Janice Regan, CMPT 102, Sept. 2006 27 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
29
© Janice Regan, CMPT 102, Sept. 2006 28 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
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.