Wednesday, September 29, 2010

Function Arguments in C,C++

If a function is to use arguments, it must declare variables that accept the values
of the arguments. These variables are called the formal parameters of the function.
They behave like other local variables inside the function and are created upon entry
into the function and destroyed upon exit. As shown in the following function, the
parameter declarations occur after the function name:
/* Return 1 if c is part of string s; 0 otherwise. */
int is_in(char *s, char c)
{
while(*s)
if(*s==c) return 1;
else s++;
return 0;
}
The function is_in() has two parameters: s and c. This function returns 1 if the
character c is part of the string s; otherwise, it returns 0.
As with local variables, you may make assignments to a function's formal
parameters or use them in an expression. Even though these variables perform
the special task of receiving the value of the arguments passed to the function,
you can use them as you do any other local variable.

No comments:

Post a Comment