Void Functions

  1. In the previous examples, all the functions returned a value/answer (output) as shown in the right side of the black box

    Black box
  2. However, not all functions return a value. Functions that do not return a value are called void functions

    Void function black box
  3. DrawRectangle example
    1. The following function displays a rectangle based on the given rows and cols
      void DrawRectangle(int rows, int cols)
      {
      	for (int r = 1; r <= rows; r++)
      	{
      		for (int c = 1; c <= cols; c++)
      			cout << "*";
      		cout << endl;
      	}
      }
      
    2. Calling the function:
      DrawRectangle(3, 4);
      
      ****
      ****
      ****
      
    3. Note that the function begins with void and does not have a return statement
    4. The call to the function does not result in an answer, so we do not call it like functions that do return an answer
      // This is WRONG... the function doesn't return back an answer!
      int ans = DrawRectangle(3, 4);
      
    5. Modify the function so it takes a third parameter: the character to use to display the box
      void DrawRectangle(int rows, int cols, char box)
      {
      	for (int r = 1; r <= rows; r++)
      	{
      		for (int c = 1; c <= cols; c++)
      			cout << box;
      		cout << endl;
      	}
      }
      
    6. Calling the modified function:
      DrawRectangle(2, 5, '?');
      
      ?????
      ?????
      
  4. General notes on functions
    1. A program can have any number of functions
    2. A function can call any other functions
    3. A function can have any number of parameters
    4. Function parameters are just variables with values that come from the function call
    5. Function arguments must match function parameters: SNOT rule
      1. Same...
      2. Number - If a function has 3 parameters, there has to be 3 arguments
      3. Order - The 1st argument is copied into the 1st parameter, the 2nd argument is copied into the 2nd parameter, etc.
      4. Type - If the 1st parameter is a char and the 2nd is an int, the 1st argument should be a char and the 2nd argument an int
    6. Functions do not share variables
      void MyFunc(int x)
      {
      	int a;
      	
      	// etc...
      }
      
      void main()
      {
      	// main's x and a are different than MyFunc's x and a
      	int x, a;
      }
      
    7. void functions don't return an answer, they just do something like output to the console window; non-void functions always return a single answer
    8. void functions are called differently than non-void functions
      // Calling a void function
      DoSomething(5);
      
      // Calling a non-void function
      cout << GiveAnAnswer(5);
      
    9. void functions do not have a return statement but non-void functions do
    10. Every path through a non-void function must return a value
      int DoSomething(int x)
      {
      	if (x < 10)
      		return 5;
      		
      	// oh oh... what does the function return if x >= 10?
      }
      
  5. Structure charts
    1. Programmers sometimes use hierarchical structure charts to show the relationship between multiple functions
    2. A structure chart shows who calls a function, but it doesn't reveal how many times a function is called or what is passed to a function
    3. Given the following program:
      #include <iostream>
      using namespace std;
      
      void A(int x)
      {
      	cout << x;
      }
      
      void B()
      {
      	A(5);
      }
      
      void C(int x, int y)
      {
      	cout << x + y;
      }
      
      void main()
      {
      	B();
      	C(2, 3);
      }	
      

      Here is the structure chart:

      Structure chart
    4. Note that function A was defined before function B which was necessary for it to compile properly. If they were switched, the compiler would complain when it reached this line:
      A(5);			
      
      because the compiler would not yet know about function A. Therefore, functions need to be defined bottom-up from the ordering in the structure chart