473,395 Members | 1,539 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

How to pass parameter or call a function?

9
How do I call a function outside of main or use a parameter to pass a reference?

If my function is:
Expand|Select|Wrap|Line Numbers
  1.  
  2. void getData(int& diameter, int& price)
  3.  { 
  4. cout << "Enter in the diameter of the pizza in: " << endl; 
  5. cin >> diameter; cout << "Enter in the the price of the pizza in: " << endl; 
  6. cin >> price; 
  7.  }
How would I call it in main()?

Would it be getData(diameter, price) ?
or some variable = getData(diameter, price) ? I can't figure it out.
or getData(int&, int&) ?
Yes, it was declared at the first part of the program. I get an error saying it expected a primary expression before int?
Oct 17 '06 #1
9 2737
dtimes6
73
#include <iostream>
using namespace std;

void getData(int& diameter, int& price)
{
cout << "Enter in the diameter of the pizza in: " << endl;
cin >> diameter; cout << "Enter in the the price of the pizza in: " << endl;
cin >> price;
}

int main () {
int diameter;
int price;
getData( diameter, price);
cout << "diameter of the pizza is: " diameter << " price is: " << price << endl;
return 0;
}
Oct 17 '06 #2
matrim
9
Hmm for some reason I put that, and I get:

pizza.cpp
pizza.cpp: In function `int main()':
pizza.cpp:27: warning: unused variable 'diameter'
pizza.cpp:29: error: `diameter' undeclared (first use this function)
pizza.cpp:29: error: (Each undeclared identifier is reported on

And I have:

Expand|Select|Wrap|Line Numbers
  1. int diameter;
  2. int price;
  3. getData(diameter, price);
  4.  
What's wrong?
Oct 17 '06 #3
dtimes6
73
Its hard to find where the problem is, can you show me your file?
Oct 17 '06 #4
vermarajeev
180 100+
On my system I get error in this line
Expand|Select|Wrap|Line Numbers
  1. cout << "diameter of the pizza is: " diameter << " price is: " << price << endl;
so I have used this
Expand|Select|Wrap|Line Numbers
  1. cout << "diameter of the pizza is: " <<diameter << " price is: " << price << endl;
Oct 17 '06 #5
dtimes6
73
right that's my fault!
Oct 17 '06 #6
matrim
9
Price is part of an equation, so I don't think that will work. Here's the whole file:

Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. char pizzaType;
  6. void getData(int length, int width, int price);
  7. void getData(int diameter, int price);
  8. double computeUnitCost(int diameter, int price);
  9. double computeUnitCost(int length, int width, int price);
  10.  
  11. int main()
  12. {
  13.         bool okay = true;
  14.         do {
  15.         cout << "Please enter in the type of pizza: " << endl;
  16.         cout << "r - a round pizza" << endl;
  17.         cout << "s - a square pizza" << endl;
  18.         cin >> pizzaType;
  19.  
  20.         cin.ignore(80, '\n');
  21.  
  22.         okay = (pizzaType == 'r' || pizzaType == 's');
  23.  
  24.             cout << "\nYou entered an invalid pizza type!" << endl;
  25.         }while(!okay);
  26.         if (pizzaType == 'r')
  27.         int diameter;
  28.         int price;
  29.         void getData();
  30.         cout << "The price per square inch for this pizza is " << getData(diameter, price) << endl;
  31.     //return 0;
  32.         if (pizzaType == 's')
  33.  
  34.         int squarePrice = getData(int& length, int& width, int& price);
  35.         cout << "The price per square inch for this pizza is " << getData << endl;
  36. }
  37.  
  38. void getData(int& length, int& width, int& price)
  39.     {
  40.  
  41.         cout << "Enter in the length of the pizza: " << endl;
  42.         cin >> length;
  43.         cout << "Enter in the width of the pizza: " << endl;
  44.         cin >> width;
  45.         cout << "Enter in the price of the pizza: " << endl;
  46.         cin >> price;
  47.         double computeUnitCost(int&, int&);
  48.     }
  49. void getData(int& diameter, int& price)
  50.     {
  51.         cout << "Enter in the diameter of the pizza in: " << endl;
  52.         cin >> diameter;
  53.         cout << "Enter in the the price of the pizza in: " << endl;
  54.         cin >> price;
  55.     }
  56.  
  57. double computeUnitCost(int diameter, int price)
  58.     {
  59.         int roundCost = diameter / price;
  60.     cout << "The price per square inch for this pizza is: " << roundCost << endl;
  61.     return roundCost; // not sure if i need to return this or if its the right way
  62. }
  63.  
  64. double computeUnitCost(int length, int width, int price)
  65.     {
  66.         int squareCost = ((length * width))/ price;
  67.     cout << "The price per square inch for this pizza is: " << squareCost << endl;
  68.         return squareCost; // not sure if this is the right way to return it
  69.  
  70.     }
Oct 17 '06 #7
dtimes6
73
remember to pass by reference if you want a return value be put into the referenced object. use const reference if you want a value that is never changed in your function scope.

you have to change:

void getData(int length, int width, int price);
void getData(int diameter, int price);

void getData(int& length, int& width, int& price);
void getData(int& diameter, int& price);
Oct 18 '06 #8
tyreld
144 100+
Your function prototypes have to match your function declarations. In the code you posted you have neglected to include the reference operator "&" in your parameter lists.
Oct 18 '06 #9
matrim
9
woops. I had them there originally, then took them out for some reason. I think i messing around with using string instead of void.

Here's what I need to accomplish:

Your program should include

1. A function named getData( )that gets all of the user input for a round pizza. For a round pizza it will ask for the diameter of the pizza and its price. Each piece of data entered by the user must be validated to ensure that it is a non-zero, positive value. If a value is zero or negative, tell the user that the input is invalid and go back and ask for that data to be input again. Somehow, this function must store the values it gets from the user where they can be accessed by other parts of the program. You cannot use global variables! The return type of the function is void.
2. Another function named getData( )that gets all of the user input for a square pizza. For a square pizza it will ask for the length and width of the pizza and its price. Each piece of data entered by the user must be validated to ensure that it is a non-zero, positive value. If a value is zero or negative, tell the user that the input is invalid and go back and ask for that data to be input again. Somehow, this function must store the values it gets from the user where they can be accessed by other parts of the program. You cannot use global data. The return type of the function is void.
3. A function named computeUnitCost( ) that takes the diameter of a round pizza and its price as parameters, and returns the cost of 1 square inch of that pizza.
4. Another function named computeUnitCost( ) that takes the length and width of a rectangular pizza and its price, and returns the cost of 1 square inch of that pizza. Note that the function computeUnitCost( ) is overloaded.
Oct 18 '06 #10

Sign in to post your reply or Sign up for a free account.

Similar topics

46
by: J.R. | last post by:
Hi folks, The python can only support passing value in function call (right?), I'm wondering how to effectively pass a large parameter, such as a large list or dictionary? It could achieved...
110
by: Mr A | last post by:
Hi! I've been thinking about passing parameteras using references instead of pointers in order to emphasize that the parameter must be an object. Exemple: void func(Objec& object); //object...
0
by: Zlatko Matić | last post by:
Hi everybody! Recently I was struggling with client/server issues in MS Access/PostgreSQL combination. Although Access is intuitive and easy to use desktop database solution, many problems...
1
by: NorrYtt | last post by:
I am having a problem with passing a function as a parameter to my DLL so it can call it back with updates. The DLL is written in LabWindows CVI with some #ifdef 'C'-style wrappings. The...
5
by: David++ | last post by:
Hi folks, I would be interested to hear peoples views on whether or not 'pass by reference' is allowed when using a Web Service method. The thing that troubles me about pass-by-reference into...
14
by: Abhi | last post by:
I wrote a function foo(int arr) and its prototype is declared as foo(int arr); I modify the values of the array in the function and the values are getting modified in the main array which is...
3
by: giloosh | last post by:
can i pass a hash as a function parameter. ive seen it been used before but i can't figure out how to do it. i would like to call a function like this for example ...
12
by: raylopez99 | last post by:
Keywords: scope resolution, passing classes between parent and child forms, parameter constructor method, normal constructor, default constructor, forward reference, sharing classes between forms....
3
by: Davy | last post by:
Hi all, Sometimes I need to pass same parameter in recursive function. From my point of view, the style is redundant, and I don't what to use some global style like self.A, self.B, Is there any...
4
by: soni2926 | last post by:
hi, is it possible to pass a function into another function as a parameter? Say i have these: function SaveMe(text) {...} function SaveMeNow(text) {...}
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.