C++ Function Arguments
3 posters
:: C Languages :: C++ :: Help!
Page 1 of 1
C++ Function Arguments
Can someone please explain C++ Function Arguments to me? I read the chapter in my Dummies book 5 times over, but to no avail. Please provide as much detail as possible.
Thanks in advance!
Thanks in advance!
~ David B
Re: C++ Function Arguments
What do you want to know about them?
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Function Arguments
Literally everything. I want to learn what they are, what purpose they serve, how to use them, syntax for them, and would like an example program with as many comments related to arguments as possible.legolizard wrote:What do you want to know about them?
I am trying to write a program where a variable that is declared within a function is passed back to the main function, but nothing seems to work, and without the use of arguments, I know the compiler will get mad and say that the variable does not exist.
Re: C++ Function Arguments
LAWL, all right then. Parameters or arguments are in short local variables to a specific function, this meaning that they are variables that will only be used in one specific function and then they are taken off the stack. So because they are variables they act like them too in that they require a data type, and a name. The interesting thing about parameters however is that their value comes from the value of already created variables.
Let us look at a simple example.
However note how if you try to call int x in main you will get an error because the variable does not exist within main and only in void function().
Furthermore, parameters can be of ANY data type, keep in mind they are ordinary variables.
Now on the passing of variables to a function...Variables are passed to a function exactly as you see them and MUST MUST MUST correspond with the correct data type otherwise you will get a warning and even an error if you are that dumb enough to do so.
For example...
This would be a correct and acceptable way to pass arguments to a function. Now if I called the function like so...
I would get a bajillion errors because a string != to a int, a int != to a double, and a double != to a string.
One thing I would also like to note is that you technically don't even need to pass in variables when calling a function and can technically use constants like the following...
And you would get the EXACT SAME RESULT. However it is very bad programming practice to do so(look up magic numbers) and I reccommend you never ever ever do so.
To answer your question on their use view the following. -> -__________________-
Functions are the basis of programming and an extenstion of them is the parameter. Therefore their use is that they allow the functions to well do more complex tasks and they also keep scope local, rather than global.
This is also where pointers get to have some fun. See parameters are merely COPIES of the original variable so if I did something like :
Pointers on the other hand actually use the variable you pass and can change that variables even from a local scope. O_O
Rewriting the above program we will get a different result :
Note how the value of x in main is also different. This is the magic of pointers my friend. : )
In summation paramters are local variabeles of a specific function and thus act like and are declared in a similar fashion. To continue paremters get their value from other variables and are actually simply copies of them, unless of course they are pointers, which are actually refereces to a variable in memory.
If you still can not fix your program I will fix it for you and then explain what I did.
Hope this helps; sincerley,
The lizard made of legos.
Let us look at a simple example.
- Code:
#include <iostream>
//Note how the parameters name and data type are within the ().
void function(int x){
std::cout<<"The value of parameter x is : "<<x;//6 will be ouputed
}
int main(){
int var = 6;
function(var);
return 0;
}
However note how if you try to call int x in main you will get an error because the variable does not exist within main and only in void function().
Furthermore, parameters can be of ANY data type, keep in mind they are ordinary variables.
Now on the passing of variables to a function...Variables are passed to a function exactly as you see them and MUST MUST MUST correspond with the correct data type otherwise you will get a warning and even an error if you are that dumb enough to do so.
For example...
- Code:
#include <iostream>
void printData(int x, double y , std::string z){
std::cout<<"The value of x is : "<<x<<"\n";//Prints out 20.
std::cout<<"The value of y is : "<<y<<"\n";//Prints out 15.5.
std::cout<<"The valie f z is : "<<z<<"\n";//Prints out "wow these are really easy"
}
int main(){
int var_int = 20;
double var_double = 15.45;
std::string a_string = "wow these are really easy";
printData(var_int , var_double , a_string);
return 0;
}
This would be a correct and acceptable way to pass arguments to a function. Now if I called the function like so...
- Code:
printData(a_string , var_int , var_double);
I would get a bajillion errors because a string != to a int, a int != to a double, and a double != to a string.
One thing I would also like to note is that you technically don't even need to pass in variables when calling a function and can technically use constants like the following...
- Code:
printData(20 , 15.5 , "wow these are really easy");
And you would get the EXACT SAME RESULT. However it is very bad programming practice to do so(look up magic numbers) and I reccommend you never ever ever do so.
To answer your question on their use view the following. -> -__________________-
Functions are the basis of programming and an extenstion of them is the parameter. Therefore their use is that they allow the functions to well do more complex tasks and they also keep scope local, rather than global.
This is also where pointers get to have some fun. See parameters are merely COPIES of the original variable so if I did something like :
- Code:
#include <iostream>
void add(int number1 , int number2){
number1 += number2;//Number 1 set equal to number1 + number 2
std::cout<<number1;//Will display 3 even though number1 corresponds with x.
}
int main(){
int x = 1 , y = 2;
add(x , y);
std::cout<<x;//Will display 1.
return 0;
}
Pointers on the other hand actually use the variable you pass and can change that variables even from a local scope. O_O
Rewriting the above program we will get a different result :
- Code:
#include <iostream>
//Now it takes two pointers to a variable.
void add(int& number1 , int& number2){
number1 += number2;//Number 1 set equal to number1 + number 2
std::cout<<number1<<"\n";//Will display 3.
}
int main(){
int x = 1 , y = 2;
add(x , y);
std::cout<<x;//Will display 3 as well. O__O
return 0;
}
Note how the value of x in main is also different. This is the magic of pointers my friend. : )
In summation paramters are local variabeles of a specific function and thus act like and are declared in a similar fashion. To continue paremters get their value from other variables and are actually simply copies of them, unless of course they are pointers, which are actually refereces to a variable in memory.
If you still can not fix your program I will fix it for you and then explain what I did.
Hope this helps; sincerley,
The lizard made of legos.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Function Arguments
Function Arguments. That's a fancy term for parameters right?
Function Arguments are used in pretty much every programming language ever in the whole wide world. Supposed you want to make a pointless program. That program must have pointlessness in it right? Well about a replacement for the print statement? (if you don't mind I'll use Java code, you should be able to follow. Just note the comments, and note that 'method' in java is basically a function. I'm kind of rusty at C++)
But supposed we want to use this same function more then once in our code. Well then we just pass in a 'parameter', or function argument, which acts as a temporary variable that we can use in our function.
I found this aspeect confusing at first, but soon it just clicked inside my head Now I don't struggle with this. I personally think C++ is rather complicated for a first language, but hey it's up to you. (btw, any way to make it so that the post editor acts more like notepad. Mainly with the double spacing when I press Enter. Makes programming impossible.
Function Arguments are used in pretty much every programming language ever in the whole wide world. Supposed you want to make a pointless program. That program must have pointlessness in it right? Well about a replacement for the print statement? (if you don't mind I'll use Java code, you should be able to follow. Just note the comments, and note that 'method' in java is basically a function. I'm kind of rusty at C++)
- Code:
public class MyProgram{
public static void main(String[] args){
printText(); //MAIN METHOD CALLS ON PRINT TEXT METHOD
}
public void printText(){ //IN CASE YOU DON'T KNOW, VOID MEANS THE FUNCTION RETURNS NO VALUE
System.out.println("Text"); //THIS IS A JAVA PRINT STATEMENT
}
}
But supposed we want to use this same function more then once in our code. Well then we just pass in a 'parameter', or function argument, which acts as a temporary variable that we can use in our function.
- Code:
public class MyProgram{
public static void main(String[] args){
printText("Hello World");
printText("TADA!");
}
public void printText(String s){ //USING THE FORMULA 'VarialeType variableName' AND SEPERATING EACH NEW PARAMETER BY A COMMA
//(YOU CAN HAVE AS MANY PARAMETERS AS YOU WANT) YOU CAN THEN HAVE YOUR OWN PARAMETERS FOR THE FUNCTION
System.out.println(s); THIS CALLS ON OUR S VARIABLE
}
}
I found this aspeect confusing at first, but soon it just clicked inside my head Now I don't struggle with this. I personally think C++ is rather complicated for a first language, but hey it's up to you. (btw, any way to make it so that the post editor acts more like notepad. Mainly with the double spacing when I press Enter. Makes programming impossible.
Nighthawk0973- Moderator
- Posts : 307
Join date : 2011-07-20
Age : 25
Location : In Front of My Computer
Re: C++ Function Arguments
\Nighthawk0973 wrote:Function Arguments. That's a fancy term for parameters right?
Just note the comments, and note that 'method' in java is basically a function.
For your first question Nighthawk yes it is.
Also to clarify your second statement. A method = a function however a method is a function bound to a class and since everything in Java is totally OO well...
And lastly in case neither of the above posts clarified anything which they should have since I read over both what I as well as Nighthawk stated and pretty much nothing is left out. However I may be a little biased since I never really found these all to complicated.(:/)
I do however think I know the core of your confusion in that you don't understand how these "arguments" or parameters(synonyms ftw) get their value, which is understandable.
So to simplify please view the following image.(This is a lot easier on paper too. :///)
- Spoiler:
Last edited by David B on Thu Jan 05, 2012 9:59 pm; edited 1 time in total (Reason for editing : Image fixed http://postimage.org/image/9pbmj1unl/)
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Function Arguments
David B wrote:Uh oh! Legolizard is getting philosophical on us!
What are you talking about? At least address the question you asked. .__.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
Re: C++ Function Arguments
Thank you to both of you, while Nighthawk left me with no questions, legolizard did.
So let me get this straight: functions are variables that exist only within a function, and I need pointers to allow the access of variables in the main function to other functions. Right?
legolizard wrote:...This is also where pointers get to have some fun. See parameters are merely COPIES of the original variable so if I did something like :
- Code:
#include
void add(int number1 , int number2){
number1 += number2;//Number 1 set equal to number1 + number 2
std::cout<
}
int main(){
int x = 1 , y = 2;
add(x , y);
std::cout<
return 0;
}
Pointers on the other hand actually use the variable you pass and can change that variables even from a local scope. O_O
Rewriting the above program we will get a different result :
- Code:
#include
//Now it takes two pointers to a variable.
void add(int& number1 , int& number2){
number1 += number2;//Number 1 set equal to number1 + number 2
std::cout<<<"\n"; will="" display="" 3.
}
int main(){
int x = 1 , y = 2;
add(x , y);
std::cout<
return 0;
}
Note how the value of x in main is also different. This is the magic of pointers my friend. : )...
So let me get this straight: functions are variables that exist only within a function, and I need pointers to allow the access of variables in the main function to other functions. Right?
Re: C++ Function Arguments
If by the fist "function" you mean parameters than yes. Parameters are variables that can only be used within that function because that is their scope.
Once, the function is complete the parameters and all other local variables in that function are taken off the stack.
Pointers, again, allow you to reference, a variable within one function with another completely different function.
If you look at my two examples, in the first one note how, x and number1 are different this is because number1 is a copy of , while in the second example x and number1 are the same value because in example 2 number1 is simply a reference rather than a copy of the variable x.
Does that help?
Also, lawl Nighthawk.
Once, the function is complete the parameters and all other local variables in that function are taken off the stack.
Pointers, again, allow you to reference, a variable within one function with another completely different function.
If you look at my two examples, in the first one note how, x and number1 are different this is because number1 is a copy of , while in the second example x and number1 are the same value because in example 2 number1 is simply a reference rather than a copy of the variable x.
Does that help?
Also, lawl Nighthawk.
legolizard- Posts : 137
Join date : 2011-08-01
Location : On planet Char.
:: C Languages :: C++ :: Help!
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|