Solved

Problem with functions - Can someone check it out  for me?

Posted on 2001-06-25
20
248 Views
Last Modified: 2010-04-02
void Halve (/*inout*/ int & firstNumber,
/*inout */ int & secondNumber)

I've written the body of the function so that when it returns, the original values in firstNumber and secondNumber are halved, but I don't think it is correct. Here is what I wrote:

void Halve (/*inout*/ int & firstNumber,
/*inout */ int & secondNumber)

int main ()
{
firstNumber = firstNumber/2;{
cout<<firstNumber<<endl;
{
secondNumber = secondNumber/2;{
cout<<secondNumber<<endl;
}
return 0
}


Thanks for your help!
0
Comment
Question by:Ayanda
  • 6
  • 3
  • 2
  • +8
20 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Your code seems to be fine. Why do you think it's incorrect? You might wonder why

firstNumber = 3;

first number /= 2;

produces '1' as the output, but that's because 'int's just represent whole numbers, so you cannot expect '1.5' as the result...
0
 

Author Comment

by:Ayanda
Comment Utility
When I run the program I keep getting errors. I don't know why though.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
What errors?
Please give details.
0
 

Author Comment

by:Ayanda
Comment Utility
8 halvesprogram.cpp
 `firstNumber' undeclared (first use this function)
8 halvesprogram.cpp
 (Each undeclared identifier is reported only once
8 halvesprogram.cpp
 for each function it appears in.)

0
 
LVL 2

Expert Comment

by:pkohlmil
Comment Utility
1. Declare firstNumber and secondNumber in main.
2. Make sure there is a semicolon after the prototype declaration.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
You forgot to declare the variables:


int main ()
{

int firstNumber = 6;
int secondNumber = 3;

firstNumber = firstNumber/2;{
cout<<firstNumber<<endl;
{
secondNumber = secondNumber/2;{
cout<<secondNumber<<endl;
}
return 0
}
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
You have not declare these variables.
You need to declare them
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
You have not declare these variables.
You need to declare them
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
You don't have to declare them inside the function, IF the function receives a variable with that name.

void Halve (/*inout*/ int & firstNumber,
/*inout */ int & secondNumber)
{
//Don't need to declare it here
}
0
 
LVL 1

Accepted Solution

by:
nivel earned 50 total points
Comment Utility
I think you have mixed thing up a bit...
Right now you are halving the numbers in the main function, and I don't see any calls to (or definition of) the Halve function.

To make things clear, here is an example:

void Halve(int& firstNumber, int& secondNumber);

int main(void)
{
  int first = 19;
  int last = 16;

  Halve(first, last);
  cout << "First:  " << first << endl;
  cout << "Second: " << second << endl;

  return 0;
}

void Halve(int& firstNumber, int& secondNumber)
{
  firstNumber /= 2;
  secondNumber /= 2;
}
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 2

Expert Comment

by:smitty1276
Comment Utility
Nivel is correct.  You never even actually wrote the function.  Plus, your prototype is incorrect.  It lacks a semicolon.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
You also have to many left sided "{"
0
 
LVL 4

Expert Comment

by:havman56
Comment Utility
hi

This is  my version of using pointers to halve.

#include<stdio.h>


int halve(int *firstnumber, int *secondnumber);
void main()
{

     int firstnumber =6;
     int secondnumber =8;

     halve(&firstnumber,&secondnumber);
     printf(" first number =%d  second number =%d",firstnumber,secondnumber);
}


int halve(int *firstnumber, int *secondnumber)
{
     *firstnumber=(*firstnumber)/2;
     *secondnumber=(*secondnumber)/2;

     
     return 0;
}

0
 

Expert Comment

by:coderat
Comment Utility
hi,,

Additionally i see /*in, out*/ declaration in the comments
in the function declaration.

if you intend to use this as part of com interface,
then you for out parameters it cant be reference ( int & )
and u should go for pointers only.


0
 

Author Comment

by:Ayanda
Comment Utility
So this is not correct:


#include <iostream>

void Halve (/*inout*/ int & firstNumber,
/*inout */ int & secondNumber);

int firstNumber = 6;
int secondNumber = 3;

int main (void)
{


firstNumber = firstNumber/2;
cout<<firstNumber<<endl;

secondNumber = secondNumber/2;
cout<<secondNumber<<endl;
}




0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
That looks correct because you're using global variables.
0
 
LVL 2

Expert Comment

by:Slordak
Comment Utility
It would seem to me that the entire point of the exercise was to write a "Halve" function which used C++ pass by reference to take the reference arguments and divide them by two.  Presumably this demonstrates how an explicit pass by reference exists in C++ and differs from "pass by pointer" in C.

Thus, Ayanda, in your most recent chunk of code:

1) You have a prototype for a function that you never actually implement.
2) You have global variables which are probably supposed to be local to function main().
3) You are performing the halving in the main() function, when you likely want to do it in a Halve() function.

Above, Nivel demonstrated the correct code using C++ "pass by reference", and Havman56 did it in standard C.
0
 

Expert Comment

by:awjackin35
Comment Utility
Hello Ayanda!!! Something for future reference is that these people on Experts Exchange are light years ahead of me, since I am just a student. In the future you want to be more specific about your queations. I put queations in my code to the experts, so they can help me. I hope this comment will help you, and give me some points.


This code works!!!!!!!!!1

//You can put as many numbers into a function as you like but
//you can only return one number. So you really want two functions
//so you can return both numbers halved. Make it a double since if
//you halve a number you might get a decimal.



#include<iostream>
 

using namespace std;




double Halve(double& firstNumber);  //Function prototype for the first function.
double Halve1(double& secondNumber);//Function prototype for the second function.

int main ()
{
     double firstNumber;  //These 4 are all declaring the variables as doubles.
     double secondNumber;
     double firstNumberanswer;
     double secondNumberanswer;

cout<<"Enter first Number"<<endl;//Prompt for a number.
cin>>firstNumber;
cout<<endl;

firstNumberanswer=Halve(firstNumber); //This is your function call.

cout<<firstNumberanswer<<endl;

cout<<"Enter second Number"<<endl;//Prompt for second number.
cin>>secondNumber;
cout<<endl;

secondNumberanswer = Halve1(secondNumber);//This is your function call.
cout<<secondNumberanswer<<endl;

return 0;
}


double Halve(double& firstNumber) //Function definition

{
     double firstNumberanswer;
     firstNumberanswer=firstNumber/2;

     return firstNumberanswer;
}
double Halve1(double& secondNumber)//Function definition
{
     double secondNumberanswer;
     
     secondNumberanswer = secondNumber/2;

     return secondNumberanswer;
}


0
 

Expert Comment

by:awjackin35
Comment Utility
Oh Yeah!!!! If you want to set the numbers equal to something then initialize them and take out the input statement.
0
 
LVL 1

Expert Comment

by:Hoegje
Comment Utility
Look Ayanda.

I'm "only" a student too, but I have already some years of experience in C++. I'll try to explain this problem to you step by step.

First of all : You want to write a small function, that takes 2 integers, and halves them. Right ? So at this point you can already write the formal declaration of the function. You do not have to have the slightest idea about how to write the body of the function YET !! So we can type :

   void Halve(int& FirstNr, int& SecondNr);

This means :
* void --> we return nothing from the function because we change the given variables itself
* Halve(...); --> Name of your function
* int& FirstNr, int& SecondNr --> this function can only work with 2 integers given. the ampersant ('&') AFTER each of the int declarations tells us that we are going to use the given integers, in stead of a copy of the given integers. A good example from this is the difference between :

void add(int FirstNr){
   FirstNr++;
};

and

void add2(int& FirstNr){
   FirstNr++;
};

when we call each of these function, for example inside a main :

int main(){
  int myVar = 3;
  add(myVar);  // here myVar still has value = 3
  add2(myVar); // but here it has become 4

  return 0;
// nevermind about this last line, just to
// keep the critics away :-)
};

The reason that the add-function seems to do nothing at all, is that at the moment you call this function with your variabele myVar, the function makes a temporary copy of your variabele with the same value as yours, and increases that in his body, in stead of using your var and increasing that. That's why we add such an ampersant ('&') to it, so we can actually change the value of YOUR var, instead of some temporary value.

So far we only have "void Halve(int& FirstNr, int& SecondNr)"

If you are going to implement the body of this function immediately (why not, you have to do so anyway, so why not now) you can place the semicolon (';') after the full declaration, otherwise you have to close this statement by a semicolon.

But it's far more simple to write the body immediatly after the declaration, so :

   void Halve(int& FirstNr, int& SecondNr){
      FirstNr /= 2;
      SecondNr /= 2;
   };

Short explanation :
  {....}; --> every function body (what it does) has to be
         included between these brackets, and ended
         with a semicolon.

  FirstNr /= 2; --> This is basicly the same as
       "FirstNr = FirstNr / 2;" So we just divide your
       number by 2, and every complete statement should be
       closed by a semicolon.

  Idem for SecondNr offcourse.

So for now we have written a function that takes 2 int's as arguments, returns nothing, but DOES divide (and changes) the values of those int's by 2.

To test this function, we write a main function:

int main(){
  /* Here comes the real stuff :p */
return 0;
};

  The normal way of using main functions is to return an integer. Most of the time this is pretty useless (it seems), but I know that some compilers give you a warning or even an error if you don't do this. So make it a good habbit and always write int main() in stead of void main() or something similar. And the last line of your main would be to return some int value, because you stated so in the beginning. The normal int is 0, to note proper ending of your program, but if you really want to use your own number, I guess there's nothing realy against it.

So :

int main(){
   int MyFirst = 4;
   int MySecond = 188;

   Halve(MyFirst, MySecond);

return 0;
};

So the easiest way to test your written function is to just declare 2 variabeles (integers offcourse !!) and call your function with those 2 variabeles as parameters between its brackets. I can assure you that MyFirst and MySecond will have values 2 and 94 respectively. But offcourse you don't believe me, and you want to see proof. Because of that we add some output to the program like this :

include <iostream>

using namespace std;

int main(){
   int MyFirst = 4;
   int MySecond = 188;

   cout << "MyFirst BEFORE = " << MyFirst << endl;
   cout << "MySecond BEFORE = " << MySecond << endl;

   Halve(MyFirst, MySecond);

   cout << "MyFirst AFTER = " << MyFirst << endl;
   cout << "MySecond AFTER = " << MySecond << endl;

return 0;
};


EXPLANATION :
   include <iostream> --> make sure you can use the very good functions from C++ with regards to In and Output. (This is a very small part of the C++ STL or Standard Library as they say. The C++ STL is full of usefull functions, but you will probably learn them one by one, and with alot of troubles at the start.)

   using namespace std; --> We follow the include - statements (almost) always with this line, which states that we can use every function which should normally be prefixed by "std::" (as "std::cout") without the "std::" in front of it. so following this line, we can always use simply "cout" instead of "std::cout".


   cout << "MyFirst BEFORE = " << MyFirst << endl; --> this line actually produces the output, in a C++ like manner. (printf is C and is better avoided in C++ code). The easiest thing about cout is that it can print (again almost) any basic type from C++, and that you can define it to print even your own types the way you like it. we just follow cout by the output operator ("<<") and say what we want to appear on the screen. Here it is first a string ("MyFirst BEFORE = "), followed by an integer (MyFirst), followed by an endline (endl or '\n').

The output for this program wil then look something like :

MyFirst BEFORE = 4
MySecond BEFORE = 188
MyFirst AFTER = 2
MySecond AFTER = 94

And that's the way you want it don't you ??

Hope I've been helpfull. I tried to explain everything as accurate as possible, just to help anybody who reads this.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now