Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 132,613 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 929 people online right now. Registration is fast and FREE... Join Now!




Need help on Functions? (New to Functions)

 
Reply to this topicStart new topic

Need help on Functions? (New to Functions), I dont understand why this function will not work?

xAlanxEnglandx
post 15 Jul, 2008 - 01:15 PM
Post #1


New D.I.C Head

*
Joined: 6 Jul, 2008
Posts: 31

Hey guys, Ime beginning C++ language and learning from the book quite well, its introduced me to Functions and understand them in a certain way, so I started to write my very own function too see if it would work.

But it don't work when I try to multiply the two numbers, I grab them out of the global scope and multiply them but it doesent seem to work, If someone could redo my code to tell me how to Multiply the two numbers it would be a pleasure and a great help! smile.gif

(the first two numbers show, but the multiply calculation seems to produce a compiler error)

CODE
#include <iostream>
using namespace std;

int number1();
int number2();
int Multiply();


int number1()
{
    int num = 3;
    return num;
}

int number2()
{
    int num = 6;
    return num;
}

int Multiply()
{
    return (number1 * number2); // Is this correct to Multiply?
}

int main()
{
    cout << "Number 1: " << number1() << endl;
    cout << "Number 2: " << number2() << endl;
    cout << "Multiplied: " << Multiply() << endl;
    
    system("pause");
    return 0;
}


Thanks for anyone who can redo the code to make the numbers multiply, Ime only a beginner so don't be mean if theres a obvous answer to the code, the compiler keeps telling me that the * is a illegal operator or something... Ime going by the book, but I just like to experiment with my own code, and thought this would work but it doesent?

Thanks smile.gif

This post has been edited by xAlanxEnglandx: 15 Jul, 2008 - 01:17 PM
User is offlineProfile CardPM

Go to the top of the page

polymath
post 15 Jul, 2008 - 01:24 PM
Post #2


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


Well, you could do one of two things. One, change your function declaration to take two arguments of type int, like this: int multiply(int num1, int num2) { OR you could make your function read like this:

CODE

int Multiply()
{
    return (number1() * number2());
}


You need parenthases to call a function. If you want to pass two args:

CODE

int multiply(int num1, int num2) {
   int returnval=num1*num2;
   return (returnval);
}


OR you could use constants to make you not need to call it like a function:
CODE

#include <something>
#define number1 3
#define number2 6
//your code


hope this helps smile.gif

This post has been edited by polymath: 15 Jul, 2008 - 01:25 PM
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 15 Jul, 2008 - 01:25 PM
Post #3


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Dammit poly, you beat me to it sleep.gif

The problem is that you need to call your functions:
return (number1() * number2());
Note the () after each number?

Here is your code, pretty short:
cpp
#include <iostream>
using namespace std;

int number1(){return 3;}

int number2(){return 6;}

int Multiply(){return (number1() * number2());}

int main()
{
cout << "Number 1: " << number1() << endl;
cout << "Number 2: " << number2() << endl;
cout << "Multiplied: " << Multiply() << endl;

system("pause");
return 0;
}
User is online!Profile CardPM

Go to the top of the page

xAlanxEnglandx
post 15 Jul, 2008 - 09:23 PM
Post #4


New D.I.C Head

*
Joined: 6 Jul, 2008
Posts: 31

I just added the (), now I understand functions abit better, thanks guys!


Oh by the way, could someone just Breifly describe what a "argument" is to a function?

Thanks if so smile.gif

This post has been edited by xAlanxEnglandx: 15 Jul, 2008 - 10:02 PM
User is offlineProfile CardPM

Go to the top of the page

no2pencil
post 16 Jul, 2008 - 12:16 AM
Post #5


My fridge be runnin OH NOEZ!

Group Icon
Joined: 10 May, 2007
Posts: 6,351



Thanked 58 times

Dream Kudos: 2375

Expert In: Goofing Off

My Contributions


QUOTE(xAlanxEnglandx @ 16 Jul, 2008 - 01:23 AM) *

Oh by the way, could someone just Breifly describe what a "argument" is to a function?


An argument is used by the function. For example

cpp

printf("%s",variable);


The function printf takes two arguments. The formatter, & the variable. The variable is not required however, it's only supplied it needed by the 1st argument. However, the 1st argument is required by printf.

cpp

printf("This text will print, & is the argument");


Sorry if this doesn't help, at quarter after 4 am, it's the best I have to offer tongue.gif
User is online!Profile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 04:24 AM
Post #6


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


Arguments (aka parameters) are basically variables that you pass to your function. Let's make a simple multiplication function:
cpp
double multiply (double x, double y)
{
return x * y;
}

Now, x and y (in the brackets) are the parameters. Basically, you can pass any two numbers to this function, and x and y will take their values. Let's make an example call to the function:
cpp
multiply (4,5);
Here, we are passing 4 and 5 to the function as parameters.

Now, x will be equal to 4, and y will be equal to 5. Then we simply return the two multiplied together, so effectively it is return 4 * 5; which we all know is 20.

Now, how would we implement a return value? Let's take a look:
cpp
double myVar = multiply (4,5);
myVar will be equal to the return value, which is 20.

Hope this helps smile.gif

You know, I haven't been thanked in a while wink2.gif
User is online!Profile CardPM

Go to the top of the page

polymath
post 16 Jul, 2008 - 07:25 AM
Post #7


D.I.C Regular

Group Icon
Joined: 4 Apr, 2008
Posts: 407



Thanked 4 times

Dream Kudos: 500
My Contributions


Oh, come on gabehabe...

This is a pretty good reference:

http://www.cplusplus.com/doc/tutorial/functions.html
User is offlineProfile CardPM

Go to the top of the page

gabehabe
post 16 Jul, 2008 - 07:38 AM
Post #8


Working Girl.

Group Icon
Joined: 6 Feb, 2008
Posts: 5,439



Thanked 94 times

Dream Kudos: 2625

Expert In: ruling the world.

My Contributions


QUOTE(polymath @ 16 Jul, 2008 - 04:25 PM) *
Oh, come on gabehabe...

What? You just don't like it that I've got more kudos and thanks than you tongue.gif

feuds are fun.
User is online!Profile CardPM

Go to the top of the page

Reply to this topicStart new topic
Time is now: 11/23/08 02:41AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month