In my book it talks about using function parameters. My question is what is the main purpose for a function parameter? Does it just make it where the current function can use a local variable that is located in a different function?
In my book it talks about using function parameters. My question is what is the main purpose for a function parameter? Does it just make it where the current function can use a local variable that is located in a different function?
a function parameter is a variable passed to a function so it can do what it needs to do. if you want to calculate the area of a square in math class, you must provide the width and height, the same is true for a function that calculates the area, you must pass the width and height parameters
It's purpose is to be able to pass variables to the function
from wich it was called
Code:void function1() { int a=5; //function 1 has a var noone else can acces function2(a); // However whe can pass it to function 2 } void function2(int anumber) { cout << anumber; }
So I could do something like-
?Code:int main() { long p cout << "Enter the number: "; cin >> p; function(p); return 0; } int function(p) { cout << "The value of p is" << p; return 0; }
yes, but you should put a semicolon after 'long p' and change 'function's return type to 'void' (because it doesn't return anything).
If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?
>>change 'function's return type to 'void' (because it doesn't return anything).
Wrong, it returns 0.
>>int function(p)
This is incorrect, you need to specify the variable type, like this:
>>int function(long p)
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
I saw the semicolon I missed, just a quick example. But why change it to void? It has a return statement at the end of it.
EDIT: Thanks for clearing that up hammer.
>>change 'function's return type to 'void' (because it doesn't return anything).
>Wrong, it returns 0.
sorry.. i missed that. it should be changed to void and the 'return 0' taken away, since it serves no purpose.. shouldn't it?
If I had a world of my own, everything would be nonsense. Nothing would be what it is, because everything would be what it isn't. And contrariwise, what it is, it wouldn't be, and what it wouldn't be, it would. You see?
I thought every function should return a value based on ANSI compliancy. Isn't this true?
no. You can do as MadHatter suggests if you want.Originally posted by Munkey01
I thought every function should return a value based on ANSI compliancy. Isn't this true?
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]
No, main is a special case. Do a board seach if you want to know more, it's been discussed far too many times already
When all else fails, read the instructions.
If you're posting code, use code tags: [code] /* insert code here */ [/code]