Argument definition
An argument is a way for you to provide more information to a function. The function can then use that information as it runs, like a variable. Said differently, when you create a function, you can pass in data in the form of an argument, also called a parameter.
- Arguments are variables used only in that specific function.
- You specify the value of an argument when you call the function.
- Function arguments allow your programs to utilize more information.
C++ argument example
Say you want to create a function that describes how much fun you're having; you'd want to be able to use different words to be as descriptive as possible.
Before now, the function may have looked like:
string howMuchFun() {
return "so much fun";
}
But then you'd always have the same amount of fun, never any more!
You can use arguments to change how much fun your function will say you're having.
Using arguments
1. Modify the function prototype and implementation to take a string argument:
string howMuchFun(string amount);
2. Change your return statement to:
return amount + " fun";
3. Add a string to the parentheses where you call the function:
howMuchFun("tons of")
Adding arguments can really expand how much they can do! You can add even more arguments by using commas between each argument (string amount, string noun).