EEB 334 - Chapter 5 Final 2022 - Void Functions
EEB 334 - Chapter 5 Final 2022 - Void Functions
Sheikh
Senior Lecturer
Department of Electrical Engineering
University of Botswana
•
• There are two differences between a function definition for
void-function and the function definitions discussed before.
• One difference is the use of keyword void. The word void is
used as a way of saying “ no value is returned by this
functions.”
• The second difference is that the return-statement does not
contain an expression for a value to be returned.
• A void-function call is an executable statement. E.g.
Show_results(32.5, 0.3);
Void_function Prototype
Void Function_Name (Parameter_List);
Prototype_Comment
Void-Function Definition
Void Function_Name(Parameter_List)
{
Declaration_1
…
Declaration_last
Executable_Statement_1
….
Executable_Statement_Last
Return
}
Call-by-Reference Parameters
• When a function is called, its arguments are substituted for the formal
parameters in the function definition.
• There are different mechanism used for this substitution process.
• The mechanism we have been using so far is known as call-by-value mechanism.
• The second main mechanism for substituting arguments is known as the call-by-
reference mechanism.
A First View of call-by-reference
• A function for obtaining input should set the values of one or more variables as
arguments and should change the values of these arguments.
• With the call-by-value formal parameters, a corresponding argument in a
function call can be a variable, but the function takes only the value of the
variable and does not change the variable in any way.
• With a call-by-value formal parameter only the value of the argument is
substituted for formal parameter.
• With a call-by-reference formal parameter, the corresponding argument in a
function call must be a variable and this argument variable is substituted for the
formal parameter. It is as if the argument variable were literally copied into the
body of the function definition in place of the formal parameter.
• After the argument is substituted in, the code in the function body is executed
and this code can change the value of the argument variable.
• The way you indicate a call-by-reference parameter is to attach the ampersand
sign & to the end of the type name in the formal parameter list in both the
function prototype and header of the function definition.
• E.g
void get_input(double f_variable)
{
cout << “ I will convert a Fahrenheit temperature”
<< “ to Celsius \n”
<< “Enter a temperature in Fahrenheit: “;
cin >> f_variable;
}
• In a program that contains this function definition, the following function call will
set the variable f_temperature equal to a value read from the keyboard.
Using Procedural Abstraction
get_input(first_num, second_num);
order(first_num, second_num);
give_results(first_num, second_num);
return 0;
}
//Uses iostream.h:
void get_input(int& input1, int& input2)
{
cout << “Enter two integers: “;
cin >> input1 >> input2;
}
void swap_values(int& variable1, int& variable2)
{
int temp;
temp = variable1;
variable1 = variable2;
variable2 = temp;
}
//Uses iostream.h
void give_results(int output1, int output2)
{
cout << “In increasing order the numbers are: “
<< output1 << “ “ << output2 << endl;
}