The safe way to pass information in to a function is by using "normal" parameters. You can use any expression that evaluates to a value of the required type when you call a function.
For example, if you define
int (double n) { return n+n; }You may call the double function using any of the following invocations:
int i = 24; double(i); double(i*42-47); double(101*32); double(double(3));
In the first case double(i), where you pass a variable name you may be certain that when control returns from the function, your variable tt i will have the same value it did before you called the function.
This is the "normal" parameter passing convention in C: Values of actual arguments are copied to the stack in a call frame. The called function has access to those copies in the stack frame, not to the originals in the calling function.
It is gratifying to know that by calling a function you are not going to inadverantly change a value you are relying on.
Sometimes, though, you will want the called function to change a value for you. C lets you do that by passing, not a value, but the address of a variable. Because it has access to the address of the variable, the called function can change the value stored at that address.
Here is a very oversimplified example. Look carefully at teh difference between square and squarify. The first simply computes the square of the value passed in to the parameter. The second is passed the address of a variable where main is storing a value. squarify can, and does, change a value declared in main.
This is powerful and dangerous.
It is possible in C to pass some parameters by value and others by address. Similar to the square and squarify functions in ex3.c we want an add(int, int) and an addify(*int, int) function. add will not change the value of any of the caller's variables. But addify can change the value of the first, but not the second, parameter. Of course, the first parameter will be an address and the second a value.
Here is a main method for you to test your add and addify functions in Exercise 3. Submit it in a file ex3sol.c with your functions add and addify.
..a.out add(42,23) is 65 n is still 42 and m is still 23 addify(42,23) is 65 n is now 65 and m is still 23Submit your file as ex3sol.c.
START7