Jump to content
  • Advertisement
Sign in to follow this  
up in flames

Parameters

This topic is 4350 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 understand what parameters anf arguements are used for, however when I try to use my own I get errors. Can anyone explain the correct syntax?

Share this post


Link to post
Share on other sites
Advertisement
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
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
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
//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
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
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
On your addName function you need to specifiy a type for the paramter.

void addName(string name)

That is your problem.

theTroll

Share this post


Link to post
Share on other sites
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
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
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!