Functions, and arguements question

Say a function takes a number of arguments. Do those arguments also have to be declared in the function, and if they are with the same name, I suppose they would take on the value of the passed in arguments from within the parameter eh?

like

1
2
3
4
5
6
function(int a, int b)
{
int a, b;
int sum;
sum = a + b;
}
Last edited on
closed account (D80DSL3A)
No, don't declare variables with the same name in the function. They will shadow (or hide) the variables being passed. You can use the a and b passed to the function.
Example:
1
2
3
4
5
6
int Add(int a, int b)
{
int sum;
sum = a + b;
return sum;
}


In main()
 
int x = Add(3,4);// x will be assigned the value 7 here. 
okay, thanks for the info :)
Topic archived. No new replies allowed.