Jump to content
  • Advertisement
Sign in to follow this  
GemuRell

noob to c++ functions :[

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm using the tutorial at cplusplus.com. I am however stuck on grasping 'functions'. Ok so im trying to make a simple add/sub/mult/div program to get the idea of function. I just did the addition part so far, but I am stuck. I ask the user to enter 2 numbers, it should be added them but it always comes back as 0.
#include <iostream>
#include <conio.h>
#include <stdlib.h>

using namespace std;

int a;
int b;

int addition ()

{
    int r;
    r=a+b;
    cout<<"Enter first number: ";
    cin>> a;
    cout<<"\nEnter 2nd number: ";
    cin>> b;
    return (r);
}
int main()
{
    int J;
    J=addition ();
    cout<<"\nThe Result is "<< J; 
    
    getch();
    return 0;
}        
    


I have no idea what i'm doing =(

Share this post


Link to post
Share on other sites
Advertisement
Youve got the assignment of r before you ask for a and b. get the inputs for a and b before you add them together to place in r.

Share this post


Link to post
Share on other sites
I see a few problems.. first of all, int a, and int b is defined at the top and they should be defined in the addition() function (not required, but i would recommend it). The second problem is, remember, code goes from one call to the next.. so you add the two numbers before you ask the user for input...

So it should look something like:

int addition ()
{
int a;
int b;
int r;
cout<<"Enter first number: ";
cin>> a;
cout<<"\nEnter 2nd number: ";
cin>> b;
r=a+b;
return (r);
}




Good luck
~zix~

Share this post


Link to post
Share on other sites
Here, try this.

#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;
int addition(int a, int b) //a, and b are your arguments
{
int result = a + b;
cout<<"The answer is: "<<result<<".\n"
}
int main() {
int n1, n2;
cout<<"Enter first number: ";
cin>>n1;
cout<<"Enter second number: ";
cin>>n2;
addition(n1, n2); //takes the two numbers and passes them as the arguments(a, b)
return 0;
}



wow was I slow :(

Share this post


Link to post
Share on other sites
Code is a recipe. It is executed step by step. If you have a recipe for boiling rice, you have to put the water into the pot before you can boil it. Similarly, you have to obtain the values to add before you can add them.

Secondly, you don't want to have every function requesting user input independently. Functions take parameters so that you can pass data to them on which they operate. They can also return resultant values, which can feed into other functions or be employed otherwise. Some programming languages allow you to return multiple values (tuples); many, including C and C++, do not.
int addition(int a, int b)
{
return a + b;
}

Here, addition() takes two parameters, a and b. Because C employs something called static type checking, meaning that the types of all objects must be declared in advance, the type of the parameters is declared in the argument list. In this case, they're both int. The return type of the function is also required. If your function doesn't return anything, you use the void type. addition() returns an int as well.

Here's how you'd you the above-defined version of addition():

#include <iostream>

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

int main()
{
using namespace std;

cout << "Adding two numbers:\nFirst number: ";
cin >> a;
cout << "Second number: ";
cin >> b;
cout << "Result: " << addition(a, b) << endl;

return 0;
}


Hope that's helped some.

Share this post


Link to post
Share on other sites
Definite agreement here. Plus, by keeping the function independant of the request you can do something like this:


#include <iostream>
#include <conio.h>
#include <stdlib.h>
using namespace std;

int addition(int a, int b) //a, and b are your arguments
{ return a + b; }

int subtraction(int a, int b)//a and b are your arguments
{ return a - b; }

int multiplication(int a, int b)//a and b are your arguments
{ return a * b; }

int division(int a, int b)//a and b are your arguments
{ return a / b; }


int main()
{
int asmd,n1, n2, r;
while(1)
{
cout<<"Would you like to Add (1), Subtract (2), Multiply (3),"
<<"Divide (4), or Exit (0)? ";
cin>>asmd;
cout<<"Enter first number: ";
cin>>n1;
cout<<"Enter second number: ";
cin>>n2;
if(asmd==1)
{ r=addition(n1,n2); }
else if(asmd==2)
{ r=subtraction(n1,n2); }
else if(asmd==3)
{ r=multiplication(n1,n2); }
else if(asmd==4)
{ r=division(n1,n2); }
else
{ break; }
cout<<"\nThe Result is "<< r;
}

return 0;
}



You want to abstract your code so that you can reuse it in as many scenarios as is feasible. A good programmer learns early on not to redo unnecessary work.

For example, I could have put:


if(asmd==1)
{ cout<<"\nThe Result is "<< addition(n1,n2); }
else if(asmd==2)
{ cout<<"\nThe Result is "<< subtraction(n1,n2); }
else if(asmd==3)
{ cout<<"\nThe Result is "<< multiplication(n1,n2); }
else if(asmd==4)
{ cout<<"\nThe Result is "<< division(n1,n2); }
else
{ break; }



And you would be able to remove the 'r' variable, with exactly the same results. However, you do not want to make special case functions. Make practical, reusable functions from the start and you will thank yourself later.

-Greven

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!