Solved

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

Posted on 2001-06-25
248 Views
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
}

0
Question by:Ayanda
• 6
• 3
• 2
• +8

LVL 86

Expert Comment

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

When I run the program I keep getting errors. I don't know why though.
0

LVL 30

Expert Comment

What errors?
0

Author Comment

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

1. Declare firstNumber and secondNumber in main.
2. Make sure there is a semicolon after the prototype declaration.
0

LVL 86

Expert Comment

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

You have not declare these variables.
You need to declare them
0

LVL 30

Expert Comment

You have not declare these variables.
You need to declare them
0

LVL 30

Expert Comment

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

nivel earned 50 total points
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

LVL 2

Expert Comment

Nivel is correct.  You never even actually wrote the function.  Plus, your prototype is incorrect.  It lacks a semicolon.
0

LVL 30

Expert Comment

You also have to many left sided "{"
0

LVL 4

Expert Comment

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

hi,,

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

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

That looks correct because you're using global variables.
0

LVL 2

Expert Comment

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

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;

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

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

return 0;
}

double Halve(double& firstNumber) //Function definition

{

}
double Halve1(double& secondNumber)//Function definition
{

}

0

Expert Comment

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

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 :

FirstNr++;
};

and

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;
// 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

### Suggested Solutions

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++.