Void Function
The structures of void functions and value-returning functions are similar. Both have a heading and a statement section. User-defined void functions can be placed before or after the main function. If user-defined void functions are added after the main function. -The function prototype must come before the main function.
There is no return type for a void function.
-return statement with no value is commonly used to exit a function early. Formal parameters are not required. A call to a void function is an independent statement.
Void Function
Function definition syntax: void functionName (formal parameter list) { statement } Formal parameter list syntax: dataType& variable, dataType& variable, ...
Void Function
Function call syntax: functionName (formal parameter list); Actual parameter list syntax: expression or variable, expression or variable, ...
Void Function
void funexp(int a, double b, char c, int x) { • • • } The function funexp has four parameter
Void Function
Value parameter: a formal parameter that receives a copy of the content of corresponding actual parameter
Reference parameter: a formal parameter that receives the location (memory address) of the corresponding actual parameter
Void Function
void expfun (int one, int& two, char three, double& four) { • • • } The function expfun has four parameter: (1)one, a value parameter of type int; (2)two, a reference parameter of type int; (3)three, a value parameter of type char; and (4)four, a reference parameter of type double.
Value Parameters
If a formal parameter is a value parameter
(The value of the corresponding actual parameter is copied into it)
. The value parameter has its own copy of the data. During program execution(The value parameter manipulates the data stored in its own memory space).
Reference Variable as Parameters
If a formal parameter is a reference parameter(It receives the memory address of the corresponding actual parameter). A reference parameter stores the address of the corresponding actual parameter. During program execution to manipulate data(The address stored in the reference parameter directs it to the memory space of the corresponding actual parameter).
Reference Variable as Parameters
Reference parameters can: -Pass one or more values from a function -Change the value of the actual parameter Reference parameters are useful in three situations: -Returning more than one value -Changing the actual parameter -When passing the address would save memory space and time
Reference Variable as Parameters
value and reference parameters
When a function is called: -Memory for its formal parameters and variables declared in the body of the function (called local variables) is allocated in the function data area. In the case of a value parameter: -The value of the actual parameter is copied into the memory cell of its corresponding formal parameter.
In the case of a reference parameter: -The address of the actual parameter passes to the formal parameter. >Content of formal parameter is an address. >During execution, changes made by the formal parameter permanently change the value of the actual parameter. >Stream variables (e.g., ifstream) should be passed by reference to a function.
value and reference parameters