0% found this document useful (0 votes)
46 views18 pages

EEB 334 - Chapter 5 Final 2022 - Void Functions

This document discusses functions in C++, specifically void functions that do not return a value. It provides examples of defining and calling void functions, including functions that take parameters by reference rather than by value. Functions can call other functions, and an example program is given that demonstrates one function calling multiple other functions to complete a task.

Uploaded by

Letso Barongwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views18 pages

EEB 334 - Chapter 5 Final 2022 - Void Functions

This document discusses functions in C++, specifically void functions that do not return a value. It provides examples of defining and calling void functions, including functions that take parameters by reference rather than by value. Functions can call other functions, and an example program is given that demonstrates one function calling multiple other functions to complete a task.

Uploaded by

Letso Barongwi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Dr. Sajid M.

Sheikh
Senior Lecturer
Department of Electrical Engineering
University of Botswana

Office No: 248/106


Ext: 4956
Email: [email protected]

EEB 334: Computer Programming


CHAPTER 5
Functions for all subtasks
Functions for all subtasks
void – functions

• Subtasks are implemented as functions in C++.


• Subtasks might produce several values or it might produce no values at all.
• In C++ a function must either return a single value or return no values at all.
• A function that returns no value is called a void-function.
Definitions of void – functions

• In C++ a void-function is defines in a way similar to the way


that functions that return a value are defined.
• The example below is a void-function that outputs the result of
a calculation that converts a temperature expressed n
Fahrenheit degrees to a temperature expressed in Celsius
degrees.
• The actual calculations would be done elsewhere in the
program.
• This void-function implements only the subtasks for outputting
the results of the calculation.
void show_results(double f_degress, double c_degrees)
{
cout.set(ios::fixed);
cout.setf(ios::showpoint);
cout.precision(1);
cout << f_degrees
<< “ degrees Fahrenheit is equivalent to \n”
<< c_degrees << “ degrees Celsius. \n”;
return 0;
}


• 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);

32.5 degrees Fahrenheit is equivalent to


0.3 degrees Celsius
• Syntax for a void-Function definition

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

Function calling functions


• A function body may contain a call to another function.
• The situation for these sorts of function calls is exactly the same as it would be if the
function call had occurred in the main function of the program; the only restriction is
that the prototype should appear before the function is used.
Function calling another function

//Program to demonstrate a functin calling another function.


#include <iostream.h>

void get_input(int& input1, int& input2);


//Reads two integers from the keyboard.

void swap_values(int& variable1, int& variable2);


//Interchanges the values of variable1 and variable2.

void order(int& n1, int& n2);


//Orders the numbers in the variables n1 and n2
//So that after the function call n1 <= n2.

void give_results(int output1, int output2);


//Outputs the values in output1 and output2.
//Assumes that output1 <= output2.
Int main()
{
int first_num, second_num;

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;
}

void order(int& n1, int& n2)


{
If (n1 > n2)
Swap_values(n1, n2)
}

//Uses iostream.h
void give_results(int output1, int output2)
{
cout << “In increasing order the numbers are: “
<< output1 << “ “ << output2 << endl;
}

You might also like