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

Passing A Value To A Function

Going back to the main program, and the "for" loop specifically, we find the new construct from the end of the last lesson used in the last part of the for loop, namely the "index++". You should get used to seeing this, as you will see it a lot in C programs.
In the call to the function "square", we have an added feature, namely the variable "index" within the parentheses. This is an indication to the compiler that when you go to the function, you wish to take along the value of index to use in the execution of the function. Looking ahead at the function "square", we find that another variable name is enclosed in its parentheses, namely the variable "number". This is the name we prefer to call the variable passed to the function when we are in the function. We can call it anything we wish as long as it follows the rules of naming an identifier. Since the function must know what type the variable is, it is defined
following the function name but before the opening brace of the function itself. Thus, the line containing "int number;" tells the function that the value passed to it will be an integer type variable. With all of that out of the way, we now have the value of index from the main program passed to the function "square", but renamed "number", and available for use within the function. Following the opening brace of the function, we define another variable "numsq" for use only within the function itself, (more about that later) and proceed with the required calculations. We set "numsq" equal to the square of number, then add numsq to the current total stored in "sum". Remember that "sum += numsq" is the same as "sum = sum + numsq" from the last lesson. We print the number and its square, and return to the main program.