twitter
    Find out what I'm doing, Follow Me :)

More About Passing A Value To A Function

When we passed the value of "index" to the function, a little more happened than meets the eye.We did not actually pass the value of index to the function, we actually passed a copy of the value. In this way the original value is protected from accidental corruption by a called function. We could have modified the variable "number" in any way we wished in the function "square", and when we returned to the main program, "index" would not have been modified. We thus protect the value of a variable in the main program from being accidentally corrupted, but we cannot return a value to the main program from a function using this technique. We will find a well defined method of returning values to the main program or to any calling function when
we get to arrays and another method when we get to pointers. Until then the only way you will be able to communicate back to the calling function will be with global variables. We have already hinted at global variables above, and will discuss them in detail later in this chapter. Continuing in the main program, we come to the last function call, the call to "ending". This call simply calls the last function which has no local variables defined. It prints out a message with the value of "sum" contained in it to end the program. The program ends by returning to the main program and finding nothing else to do. Compile and run this program and observe the output.