6 Replies - 433 Views - Last Post: 06 March 2012 - 03:47 AM Rate Topic: -----

#1 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

void function

Posted 05 March 2012 - 10:51 PM

Is problems with trying to get this void function to multiple x? Can someone please help me?


#include<iostream>

using namespace std;

void Square (int& x);

int main()
{
	int x;
cout<<" Enter an amount to be squared" <<endl;
	cin>>x;
	return 0;
}

void Square (int& x)
{
	x = x * x;

	cout<<"Value of x is:" << x <<endl;

	return;
	
}







Is This A Good Question/Topic? 0
  • +

Replies To: void function

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: void function

Posted 05 March 2012 - 11:08 PM

Looks like your on the right path, you just need to call the function from main.

Read this : http://www.cplusplus...rial/functions/
Was This Post Helpful? 0
  • +
  • -

#3 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: void function

Posted 05 March 2012 - 11:09 PM

you don't need the return statement on line 21.

Jack
Was This Post Helpful? 0
  • +
  • -

#4 taaliyah_2000  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 15-November 09

Re: void function

Posted 05 March 2012 - 11:31 PM

When I run the program I get a prompt screen to enter the number amd the product doesn't show up after the number is entered?
Was This Post Helpful? 0
  • +
  • -

#5 jdavi134  Icon User is offline

  • D.I.C Head

Reputation: 42
  • View blog
  • Posts: 225
  • Joined: 26-October 11

Re: void function

Posted 05 March 2012 - 11:43 PM

You need to call the function like jjl said.

something like this

function(name of variable);

Was This Post Helpful? 0
  • +
  • -

#6 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1270
  • View blog
  • Posts: 4,998
  • Joined: 09-June 09

Re: void function

Posted 05 March 2012 - 11:43 PM

The entry point of any program starts at the top of main and ends at the bottom of main. You do not call your Square function anywhere in main, therefore the Square function is never executed. Read up on functions on the link I posted in my earlier post.
Was This Post Helpful? 0
  • +
  • -

#7 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6246
  • View blog
  • Posts: 24,014
  • Joined: 23-August 08

Re: void function

Posted 06 March 2012 - 03:47 AM

Read your book/notes and/or the links to the function tutorials in my signature. Hell, the second one has this exact problem explained.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1