Sign in to follow this  
up in flames

Parameters

Recommended Posts

rip-off    10976
Show us your code, and the errors.

It could take quite some time for us to guess the problem you are having [grin].

If it helps, arguments appear like this:

functionReturnType functionName( argument1Type name1, argument2Type name2 /* and so on */ );

So an example:


int add( int x, int y )
{
return x + y;
}

Share this post


Link to post
Share on other sites
templewulf    526
I think you'll have to be more specific. For instance, what language are you programming in? What IDE are you using? Do you have an example of code that didn't work? Do you have an example from a book that you're trying to emulate?

EDIT: Too slow!

Share this post


Link to post
Share on other sites
CTar    1134
Your learning resource can most likely, if it can't then it isn't good enough. If you want us to help you you need to specify at least three things:
1) Your language (C, C++, C#, Scheme, F#, Java or something else?)
2) The code which doesn't work
3) The error you get (does the code behave odd? does it give you a compile time error? Or does it start shouting?)

In C and C++ the correct syntax is:
return-type identifier(type1 id1,type2 id2,type3 id3 ... typen idn);


So for a function returning void and taking two int paramters you would do this:
void func(int a,int b);

Possibly with another function and parameter names.

Share this post


Link to post
Share on other sites
-JetSirus-    158
//Function to take two numbers and add them together.
int addNums(int num1, int num2) {
return (num1 + num2);
}


In that function num1, and num2 are arguments. You would use the fuction as such:
myInt = addNums(1, 2);


Then myInt would equal three. Arguments can be anything you want them to be. You have strings, ints, floats, doubles, chars, vector<*ints>, etc... The thing to keep in mind is that if you make a function that takes a integer, don't pass it a string or issues will result. If you need a solitary function to take many differant types of arguments but perform similer things on each type you can use polymorphism(I think thats the correct term). For example, to make addNums take doubles, floats and ints you would do the following.

//Function to take two numbers and add them together.
int addNums(int num1, int num2) {
return (num1 + num2);
}

float addNums(float num1, float num2) {
return (num1 + num2);
}

double addNums(double num1, double num2) {
return (num1 + num2);
}


The complier will then use the correct function based off of how you call it. For example.

myInt = addNums(1, 2); Calls the first function I made.
myFlt = addNums(1.0f, 2.0f); Calls the second function.
myDbl = addNums(1.00, 2.00); Calls the third function.

I hope this has helped.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
I am using C++ and the compiler is the G++ compiler that comes with the Fedora O/S. The error I am trying to fix is a compile time error say in:

#include

using namespace std;

string getName(string);
void addName(string);

int main()
{
string name;

getName();
addName(name);
return 0;
}

string getName()
{
string name;

cout > name;

return name;
}

void addName(name)
{
cout <

Share this post


Link to post
Share on other sites
TFS_Scorpion    128
[quote]Original post by Anonymous Poster
I am using C++ and the compiler is the G++ compiler that comes with the Fedora O/S. The error I am trying to fix is a compile time error say in:


#include //Include what, exactly here?

using namespace std;

string getName(string);
void addName(string);

int main()
{
string name;

//This call to getName doesn't assign what is returned to anything at all
//When a function returns something that you want, you need to actually do
//something with the results - however, make sure you read the notes in the
//function itself, below
getName();

//Perhaps the line should have been... :
name = getName(); //assigns the returned value (string) to name

//Otherwise, if you didn't assign the value above, when you call *this*
//function, you're sending it a string variable that has not been
//initialized to anything at all. - error
addName(name);

return 0;
}

string getName()
{
string name;

//This isn't the correct syntax for the cout statement...
cout > name;

//The line should be:
cout << name;

//However, as in the main function, you're attempting to cout a value
//from a variable that has not been initialized yet. In other words,
//name needs to have something assigned to it before you can cout it or
//return it. What is this function's purpose? Have the user type it in?
//If so, get rid of cout and use cin:

cout << "Please enter a name: ";
cin >> name; //this will allow the user to type in a name

return name; //this would now have something to return. Otherwise, you're
//trying to return something that doesn't have a value yet
}

void addName(name)
{
//Not sure what happened to the rest of the code here, so I have nothing here
cout <




[/quote]

HTH :)
Scorp

EDIT - GRRRRRRRR At getting source posts to quit turning my < into & lt !

Share this post


Link to post
Share on other sites
up in flames    122
Sorry about that, I hit the wrong button and didn't notice until I was able to get back online today.

I ended up fixing the problem, I had tp make a global declaration of my string variable name. Also in any other function I wanted to use a param with I had to make the varaible that I was passing global. Is this something that has to be done at all times?


//now code looks like this

#include <iostream>
#include <string>

using namespace std;

string name; // here is the global declaration of my string variable

void addName(string);
string getName();

int main()
{
getName();
addName(name);
}
string getName()
{
cout << "Enter Name:" << endl;
cin >> name;

return name;
}

void addName(string)
{
cout << name << " is the name that I typed in" << endl;
}




This works when I compile and run it, but I didn't think that I would have to make variables to be used as parameters global...

Thanks Everyone

Share this post


Link to post
Share on other sites
thallish    202
Quote:
Original post by up in flames
Sorry about that, I hit the wrong button and didn't notice until I was able to get back online today.

I ended up fixing the problem, I had tp make a global declaration of my string variable name. Also in any other function I wanted to use a param with I had to make the varaible that I was passing global. Is this something that has to be done at all times?

*** Source Snippet Removed ***

This works when I compile and run it, but I didn't think that I would have to make variables to be used as parameters global...

Thanks Everyone


OK you shouldn't have to use a global. In your addName function declaration you are providing a string parameter, which is allright, but in you function definition you have left out a parameter identifier.

Your function should be addName(string name) and thereby you'll use the string given in main. Here is the altered code:


//now code looks like this

#include <iostream>
#include <string>

using namespace std;

// prototypes
void addName(string);
string getName();

int main()
{

string name;

name = getName();
addName(name);

return 0;
}
string getName()
{
cout << "Enter Name: " << endl;
cin >> name;

return name;
}

void addName(string name) // now name will be used in this function scope
{
cout << name << " is the name that I typed in" << endl;
}





EDIT: Oh yeah remember to add a return value to your main

[Edited by - thallish on August 20, 2006 4:48:41 PM]

Share this post


Link to post
Share on other sites
stenny    142
You don't have to define arguments globally...You certainly don't have too. The problem is that you you're functions in an other way. You do:

string name;

Function(string)
{
cout << name;
}



But it should be:

Function(string name)
{
cout << name;
}



You declare the variable and it's type inside the function like:

Function(string name);

And then the function uses that string you just defined:

{
cout << name;
}

Remember the variable name will be deletet/removed/destroyed when the function has ended.

Hope that helped

-Stenny

---P.S.---
And for the others out there: I might have swapped the words declared and defined but I just can't remember their meaning[grin]. Feel free to correct this if you want.
---/P.S.---

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this