I've tried but I don't understand how to use void, can someone please try to explain how it works in a simple way.

#include<iostream>
#include<conio.h>
#include<iomanip>
using namespace std; 
float  areacalc( float rad);
float  circumcalc( float rad);
void  output(float A, float C);
void  input ( void  );
float radius;                   // Deklaration av en global variabel 
int main( )
{
    input( );  
   float circum, area;  
   area= areacalc( radius);
   circum = circumcalc( radius);
   output( area, circum);
   return 0;
}
float  areacalc( float rad)
 {
   float circlearea;
   circlearea = rad*rad*3.14;
   return circlearea;
}
float  circumcalc ( float rad)
 {
   return  2*rad*3.14;
 }
void  output(float A, float C)
{
  cout.setf(ios::fixed);
  cout<<setprecision(2); 
  cout<<"area = "<< A<<" centimetre^2"<<endl;                                                        
  cout<<"Circumference = "<< C<<" Centimetre"<<endl;
}
void  input ( void  )
{
 cout<<"Input the circle radius: ";                
  cin>> radius;
}

This is an example in my book but I don't understand where you get the value for "A", "C" or "rad" when it's not given a value anywhere in the code. I feel really stupid, that I don't get it.


Also can someone please show me what this program should look like:
Write a program that uses a function with following prototype: float add(float a, float b); to calculate the sum of two numbers.
Should look something like this:
Input two numbers: 10 30
Sum = 40

well in the ouptut function a and c are being passed to the function and in the input function radius is a global variable and doesnt need to be passed

void : is a type that acts as a place-holder for "nothing".

>>I don't understand where you get the value for "A", "C" or "rad" when it's not given a value anywhere in the code.

"A" "C" and "rad" are function parameters. A declaration like float add(float a, float b); declares a function which returns a float , the function is called "add", and it takes two parameters "a" and "b" which are each of type float . When you call the function "add", you pass a value for each parameter, then the execution enters the body of the function and, at that point, the variables "a" and "b" will have the values that were passed by the "caller". When the function is finished it returns a value using a statement like return some_value; (where "some_value" is either a variable holding a value or the result of some expression). In your sample code, the lines 14 to 16 are calling the various functions, giving them the values for the parameters "A", "C" and "rad".

This is an example in my book but I don't understand where you get the value for "A", "C" or "rad" when it's not given a value anywhere in the code. I feel really stupid, that I don't get it.

Let's use a simpler example and focus on how functions, arguments, and parameters interact:

#include <iostream>

using namespace std;

int add(int a, int b)
{
    return a + b;
}

int main()
{
    int c = add(11, 22);

    cout << c << '\n';
}

Your question amounts to "how do a and b work in the add function?". When you declare a function, you specify parameters. Parameters are variables that are initialized with the corresponding arguments of a call to that function. In the first example, add() is a function that takes two parameters int a and int b . These are variables local to the add function just the same as if you declared them in the function body:

int add()
{
    int a = <magic>;
    int b = <magic>;

    return a + b;
}

A function definition doesn't run any code, it just defines a blueprint for code that will be run later. To actually run the code in a function you have to call the function and pass in arguments to give the parameters values. The function call add(11, 22) initializes a with 11, b with 22, and runs the code in the body. The result of a + b (the return value) conceptually is passed off to a hidden temporary variable which is usable back in the calling function.

With all of this in mind, the interaction between add() and main() is more like this:

#include <iostream>

using namespace std;

int main()
{
    int __temp;
    int c;

    {
        int a = 11;
        int b = 22;

        __temp = a + b;
    }

    c = __temp;

    cout << c << '\n';
}

Okey, I think I kinda get it now..

I tried "copying" the sample code with the float add(float a, float b); prototype and came up with this:

float add(float a, float b);
float output(float sum);
void input(void);
float num1;
float num2;

int _tmain(int argc, _TCHAR* argv[])
{
	input();
	float total;
	total=add(num1, num2);
	output(total);
	return 0;
}
float add(float a, float b)
{
	float sumtot;
	sumtot=a+b;
	return sumtot;
}
void output(float sum)
{
	cout<<"Sum = "<<sum<<endl;
}
void input(float a, float b)
{
	cout<<"Input two numbers: ";
	cin>>a>>b;
}

I run it and gets the error messages:
'void output(float)' : overloaded function differs only by return type from 'float output(float)'
see declaration of 'output'
redefinition; different basic types
see declaration of 'output'

So what am I doing wrong? Am I making the code too complicated?

Type void shouldn't return any value. If you want a return value you should declare the function of the type you are returning. In line 3 you declared void input which would be correct, but the parameters are floats and there are two of them so you can't declare it as (void) it should be (float a, float b.).

There are a few problems with your code. Let's go through them in order of importance.

First, lines 1 to 3 are called "function prototypes". These are announcing to the rest of the program that there will be three functions named "add", "output" and "input", respectively. And, it announces what parameters (how many and their types) they will take and what type of result value they yield. Later, you have the implementation (often called "definition") of the functions (with the actual code that they execute when called). It is necessary that the prototype and the definition match exactly. So, in this case, you need the following prototypes (instead of lines 1 to 3):

float add(float a, float b);
void output(float sum);        //notice the 'void' here.
void input(float a, float b);  //notice the parameters.

The error message you are getting is due to the fact that when you call the functions (or when the compiler sees the calls to your functions), the compiler only knows about the prototypes that you declared at the start, because it hasn't reached the definitions yet. So, it expects different functions from what the definitions are (different parameters and/or return type), so, the compiler gets confused and throws a compilation error.

Second, in your function "input", you pass two parameters "a" and "b" to the function. The problem is that you need to understand that these parameters are "passed-by-value", which is a one-way street. By passing parameters, you can only give values to them for the function to use. Whatever modifications the function makes on those variables will not be reflected back in the caller's variables. This is what I mean:

#include <iostream>

void setValue(int a) {
  a = 42;
};

int main() {
  int some_var = 69;
  std::cout << some_var << std::endl;  //output is: 69
  setValue(some_var); //call the function with parameter some_var.
  std::cout << some_var << std::endl;  //output is again: 69
  return 0;
};

This is because the parameter that is passed is copied into a variable which is local to the function, so any modifications to that variable are also local to the function only. To solve this, you can use "pass-by-reference" which passes a reference to a variable (or "alias" of the variable) to the function, not a copy of its value. To signify a pass-by-reference, you use the & sign after the type of the parameter:

#include <iostream>

void setValue(int& a) {  //notice the & here.
  a = 42;
};

int main() {
  int some_var = 69;
  std::cout << some_var << std::endl;  //output is: 69
  setValue(some_var); //call the function with parameter some_var.
  std::cout << some_var << std::endl;  //output is now: 42
  return 0;
};

So, with this modification, a more appropriate definition for the "input" function would be:

void input(float& a, float& b)  //notice the two & here.
{
	cout<<"Input two numbers: ";
	cin>>a>>b;
}

**Remember to change the prototype of "input" to match that of the definition.

Third, when you call a function, parameters are not optional, you have to have the correct number of parameters and they need to be of the correct type. In this case, when you call the "input" function, you need to provide the correct parameters, so, instead of line 9, you need:

float num1, num2;
  input(num1,num2); //will take num1 and num2 by reference and assign the user-provided values to them.

Fourth, you have noticed in the above last snippet of code that I have put the declarations of num1 and num2 inside the main() function. This is because you should avoid global variables when they can be avoided, which is the case here. So, you should remove the lines 4 and 5 (and replace by a declaration inside the main function, as I did above).

Finally, generally you should assign values to variables on the site of their declaration (on the same line). This is a good habit to develop (it is less error-prone), so better soon than later. So, lines 10 and 11 should be merged into:

float total = add(num1,num2);  //declare "total" and immediately assign a value to it.

And lines 17 and 18 should be:

float sumtot = a + b;

Actually, in your "add" function, keeping things short and simple is usually better in C++ (i.e. don't make a simple piece of code unnecessarily long, but don't over-compress complex code into the shortest possible amount of lines either, keep it balanced). So, as with Narue's version, your add function doesn't need to be more than this:

float add(float a, float b)
{
	return a + b;
}

So, all-in-all, your "revised" code is this:

#include <iostream>  //this is needed for cout/cin
using namespace std; //this is needed to be able to use cout and cin without std:: in front of them.

float add(float a, float b); 
void output(float sum);         //notice the matching prototype.
void input(float& a, float& b); //notice the matching prototype.

int _tmain(int argc, _TCHAR* argv[])
{
  float num1, num2; //declare input vars, as local variables (not global).
  input(num1, num2);
  float total = add(num1, num2);
  output(total);
  return 0;
}
float add(float a, float b)
{
  return a + b;
}
void output(float sum)
{
  cout << "Sum = " << sum << endl; //space things out a bit, for readability.
}
void input(float& a, float& b) //notice the two & here.
{
  cout << "Input two numbers: ";
  cin >> a >> b;
}

Thank you, this really helped me understand void and prototypes better =D

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.