Parameters

Started by
9 comments, last by stenny 17 years, 8 months ago
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?
Kenjutsu maybe a lost art, but tell me I'm stupid for wanting to take a knife toa gunfight after watching this http://www.comegetyousome.com/misc/videos/bulletvskatana.wmv
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;}
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!
XBox 360 gamertag: templewulf feel free to add me!
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.
//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.
------------------This is so stupid!
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 <
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 <



HTH :)
Scorp

EDIT - GRRRRRRRR At getting source posts to quit turning my < into & lt !
On your addName function you need to specifiy a type for the paramter.

void addName(string name)

That is your problem.

theTroll
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 variablevoid 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
Kenjutsu maybe a lost art, but tell me I'm stupid for wanting to take a knife toa gunfight after watching this http://www.comegetyousome.com/misc/videos/bulletvskatana.wmv
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;// prototypesvoid 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]
regards/thallishI don't care if I'm known, I'd rather people know me

This topic is closed to new replies.

Advertisement