Wednesday, January 26, 2011

1.8 Arguments - Call by Value

  • All functions arguments are passed "by value": called function is given the value of its arguments in temporary variables rather than originals
  • Called function cannot directly alter a variable in the calling function
  • Call by reference: the called routine has access to the original argument, not a local copy
  • It is possible to arrange for a function to modify a variable in a calling routin. The caller must provide the address of the variable to be set (a pointer to the variable)
  • The called function must declare the parameter to be a pointer.
  • Call by value leads to more compact programs with fewer extraneous variables, because parameters can be treated as initialized local variables in the called routine
  • To modify a variable in a calling function, the caller must provide the address of the variable to be set (pointer to the variable), and the called function must declare the parameter to be a pointer, and access the variable indirectly through it
  • In contrast, when the name of an array is used as an argument in a calling function, the value passed to the calling function is the location or address of the beginning of the array - there is no copying of array elements
  • By subscripting this value, the function can access and alter any element of the array  

    No comments:

    Post a Comment