d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Quick Function Question
Add Reply New Topic New Poll
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 21 2012 12:09am
If I do something like


sum = total(x,y,z)

where sum is some variable local to main and total is a function I have created and x,y,z are all variables local to the main function

I am passing x,y,z to total

When I pass them, can I just go ahead and use them in the function? Or do I have to define them


IE

When declaring prototypes (what my prof calls functions), should I write double total(double x, double y, double z);

or is it okay if I do

double total();

And just use the x,y,z that are passed without creating new local variables to the function.
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Jul 21 2012 12:26am
you need to define the type of variables you are passing (the variables inside the brackets) as well the return type (the type before the function name)

once you define the variables inside the brackets you do not have to declared them inside your function.

ex:

Code
int total(int x, int y, int z)
{
  return x + y + z;
}


takes the integers passed to it and returns an integer values based upon them.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jul 21 2012 12:27am
would help if you specified the language, bud

Quote

double total();

And just use the x,y,z that are passed without creating new local variables to the function.


I'm guessing that won't even compile. when you pass them as arguments, your function treats them as local variables. you probably have to specify the quantity and types/names, depending on what language you use
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 21 2012 12:32am
c++ sorry
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Jul 21 2012 12:33am
Quote (AbDuCt @ Jul 21 2012 01:26am)
you need to define the type of variables you are passing (the variables inside the brackets) as well the return type (the type before the function name)

once you define the variables inside the brackets you do not have to declared them inside your function.

ex:

Code
int total(int x, int y, int z)
{
  return x + y + z;
}


takes the integers passed to it and returns an integer values based upon them.


Quote (carteblanche @ Jul 21 2012 01:27am)
would help if you specified the language, bud



I'm guessing that won't even compile. when you pass them as arguments, your function treats them as local variables. you probably have to specify the quantity and types/names, depending on what language you use




Ahh, I see. Thanks guys, prof didn't really explain this.

This post was edited by Eep on Jul 21 2012 12:33am
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll