Please explain the void function to me.

So i just recently learned about functions (very new to programming) and after reading the section in the tutorial on this site about void functions i got a bit confused.

From what i understand a void function is just there to seperate your code from the main function since the function wont really return anything to you.

I made a simple program a few days ago that is supposed to separate numbers from two files (A and B) and then put them in the correct order in a new file (C).

I was thinking about adding a function into a separate .cpp file but i did not know where to start. Last time i did this i had a function return true or false and therefore i could easily come to the conclusion that i should use a bool function.

But looking a my code right now i did not really know what kind function to use but i am going to guess it would be some sort of void function?

Also as a separate question, i made my bool function without any parameters and it worked perfectly, do i need any parameters and if so what would they be?

This is my code for my bool function. As you can see i have no parameters.
Please ignore the swedish comments. ;)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <fstream>
#include "function1.h" // Länkar till headerfilen function1.h

using namespace std;

// Deklarerar variabler
ifstream numbers;
int newnr;
int oldnr;

//Bool funktion som ska returna (true) eller (false)
bool IsFileSorted()
{

  // Läser in värdena ur vald fil.
  numbers.open("B");

  // Kollar att filen öppnades ordentligt, annars meddelas vi.
  if (!numbers.is_open())
    cout << "The file did not open correctly" << endl;

  // Lägger in värdet i variabeln numbers i variabeln oldnr.
  numbers >> oldnr;

  // While-sats som läser in värden tills de är slut.
  while (!numbers.eof())
  {
    numbers >> newnr;

    // If-satsen kollar att de tidigare nummret inte är större än det nya.
    // Om detta sker returnas (false) och funktionen avbryts.
    if (oldnr > newnr)
      return false;

  oldnr = newnr;
  }

  // Stänger filen som en extra säkerhetsåtgärd.
  numbers.close();
  
  // Ifall funktionen aldrig avbryts i if-satsen ovanför gör den de här
  // och returnerar (true).
  return true;
}


And this is my code that put the numbers in the correct order. As you can see i have it all in the main function. Is possible to use a void function here to separate my code from main function, and how would that look like?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
include <iostream>
#include <fstream>

using namespace std;

int main ()
{

  // Deklarerar variabler.
  ifstream firstn1;
  ifstream firstn2;
  ofstream correctorder;
  double value1;
  double value2;

  // Öppnar filerna
  firstn1.open("A");
  firstn2.open("B");
  correctorder.open ("C");

  // If-satser som dubbelkollar att filerna öppnades korrekt.
  if (!firstn1.is_open())
  {
    cout << "Fil 1 öppnades ej korrekt." << endl;
  }

  if (!firstn2.is_open())
  {
    cout << "Fil 2 öppnades ej korrekt." << endl;
  }

  // Läser in de första numrerna i variablerna.
  firstn1 >> value1;
  firstn2 >> value2;

  // while-sats med nästlad if-sats som lägger in numrerna i rätt ordning
  // och sedan lägger in ett nytt värde i variabeln.
  while (!firstn1.eof() && !firstn2.eof())
  {
    if (value1 < value2)
    {
      correctorder << value1 << endl;
      firstn1 >> value1;
    }
    else
    {
      correctorder << value2 << endl;
      firstn2 >> value2;
    }
    }

    // while-satser som lägger in resterande värden ifall ena filens
    // nummer tagit slut.
    while (!firstn1.eof())
    {
      correctorder << value1 << endl;
      firstn1 >> value1;
    }

    while (!firstn2.eof())
    {
      correctorder << value2 << endl;
      firstn2 >> value2;
    }

    // Stänger filerna som en extra säkerhetsåtgärd.
    firstn1.close();
    firstn2.close();
    correctorder.close();

    return 0;

}


Thanks
- Zorac
Last edited on
Void functions always return true irrespective of their continence. A return statement inside of a void function simply stops the execution of the function and prevents any of the code after it from running. It is useful not only to make your code look neater and cleaner, but also to allow certain elements to be reused multiple times without needing to be written multiple times.

It is rare that anyone would ever actually return the value of a void function. Usually they would either be used to modify a variable in a higher scope or have a variable passed to them by reference that they could then modify.
Last edited on
Thanks for the answer.

I managed to make my code in two separate void functions so it looks a bit neater now. :)

However i am still a bit confused about the parameters in functions and why i dont need to have them and when i am supposed to have them.
However i am still a bit confused about the parameters in functions and why i dont need to have them and when i am supposed to have them.

Are you still using global variables? Please post your current code.
jlb

I am not really sure what mean by "global variables".
But if you look at the first code i posted in i dont have any variables in my bool function.

My current code with void functions look like this. And as you can see here as well i dont have any variabels in the void functions either.

function2.h
1
2
3
4
5
6
7
#ifndef _FUNC2_
#define _FUNC2_

void AddOrder();
void AddRest();

#endif 


function2.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <fstream>
#include "function2.h" // Länkar till headerfilen function2.h

using namespace std;

// Deklarerar variabler.
ifstream firstn1;
ifstream firstn2;
ofstream correctorder;
double value1;
double value2;

void AddOrder () // Namnger void-funktion.
{
  // Öppnar filerna
  firstn1.open("A");
  firstn2.open("B");
  correctorder.open ("C");

  // If-satser som dubbelkollar att filerna öppnades korrekt.
  if (!firstn1.is_open())
  {
    cout << "Fil 1 öppnades ej korrekt." << endl;
  }

  if (!firstn2.is_open())
  {
    cout << "Fil 2 öppnades ej korrekt." << endl;
  }

  // Läser in de första numrerna i variablerna.
  firstn1 >> value1;
  firstn2 >> value2;

  // while-sats med nästlad if-sats som lägger in numrerna i rätt ordning
  // och sedan lägger in ett nytt värde i variabeln.
  while (!firstn1.eof() && !firstn2.eof())
  {
    if (value1 < value2)
    {
      correctorder << value1 << endl;
      firstn1 >> value1;
    }
    else
    {
      correctorder << value2 << endl;
      firstn2 >> value2;
    }
    }
}

void AddRest () // Namnger void-funktion.
{
  // while-satser som lägger in resterande värden ifall ena filens
  // nummer tagit slut.
  while (!firstn1.eof())
  {
    correctorder << value1 << endl;
    firstn1 >> value1;
  }

  while (!firstn2.eof())
  {
    correctorder << value2 << endl;
    firstn2 >> value2;
  }

  // Stänger filerna som en extra säkerhetsåtgärd.
  firstn1.close();
  firstn2.close();
  correctorder.close();
}


main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include "function2.h" // Länkar till headerfilen function2.h

using namespace std;

int main ()
{
  // Anropar funktionerna.
  AddOrder ();
  AddRest ();

  return 0;
}
This is what I mean by global variables:
function2.cpp
1
2
3
4
5
6
7
...
// Deklarerar variabler.
ifstream firstn1;
ifstream firstn2;
ofstream correctorder;
double value1;
double value2;


Global variables are variable created in the global scope (not inside any functions).
Last edited on
So today i got help from one of my teachers explaining parameters for me. That they are basically local variables. This was something i did not know yesterday. ;)
Topic archived. No new replies allowed.