Parameters
Parameters are the way program parts communicate with each other. One
program part will call a subprogram to do some task. In order for the
subprogram to perform the task some information may need to be given to it.
Once the subprogram is done, it may need to give the calling program part
the results of its task. Parameters have 3 communication methods:
- In only - information is sent IN to the subprogram from the calling
program part. The called subprogram gets a COPY of the value sent
from the calling program part. This is called "pass by value".
(Read only)
- In out - information is both sent IN to the subprogram and comes OUT of
the subprogram through the same parameter. For example, I may want to
send in a value and have the subprogram change that value to something
different. Both the calling program part and the subprogram have to
have access to the same memory location so that the subprogram can
change the value. This is called "pass by reference", or "pass by
value- result". C does not have a pass by reference method (C++ does)
but it can simulate the action by passing addresses by value. To
allow both the calling program part and the called subprogram to have
access to the same memory location, C uses the address of the
parameter (pointers). The calling program part sends the address of
the parameter whose memory needs to be shared to the subprogram. The
subprogram receives an address. For example: get_int(&amount) would
be used in the calling program part and void get_int(int *num) would be
what the subprogram received. (Read-write)
- Out only - information comes OUT of the subprogram into a designated
memory location but no information comes in. In C, this is accomplished
in the same way the in-out is accomplished;however, C and C++ do NOT
enforce the idea that no information is passed in. (Write only)
Actual Parameters
Actual parameters are the values that are actually sent to the subprogram.
Actual parameters are the way that the calling program part sends
information to the called subprogram. In the case of procedures, they
can also bring information back from the called subprogram. With functions,
all information coming back from the called subprogram is sent back using
a function return statement. In C, "return (value_of_type_return_type);".
Formal Parameters
Formal parameters are those defined in the subprogram. They must match the
actual parameters in number, type, and order. If you send 3 integers, you
must receive 3 integers. If you send an integer, a float, and a character,
you must receive an integer, a float, and a character IN THAT ORDER.