Function Arguments in C and C++
Function Arguments
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.
Calling Functions with Arrays
When an array is used as a function argument, its address is passed to a function. This is an exception to the call-by-value parameter passing convention. In this case, the code inside the function is operating on, and potentially altering, the actual contents of the array used to call the function. For example, consider the function print_upper(), which prints its string argument in uppercase:
#include
#include
void print_upper(char *string);
int main(void)
{
char s[80];
gets(s);
print_upper(s);
printf(”\ns is now uppercase: %s”, s);
return 0;
}
/* Print a string in uppercase. */
void print_upper(char *string)
{
register int t;
for(t=0; string[t]; ++t) {
string[t] = toupper(string[t]);
putchar(string[t]);
}
}
After the call to print_upper(), the contents of arrays in main() will change to uppercase. If this is not what you want, you could write the program like this:
#include
#include
void print_upper(char *string);
int main(void)
{
char s[80];
gets(s);
print_upper(s);
printf(”\ns is unchanged: %s”, s);
return 0;
}
void print_upper(char *string)
{
register int t;
for(t=0; string[t]; ++t)
putchar(toupper(string[t]));
}
In this version, the contents of arrays remain unchanged because its values are not altered inside print_upper().
The standard library function gets() is a classic example of passing arrays into functions. Although the gets() in your standard library is more sophisticated, the following simpler version, called xgets(), will give you an idea of how it works.
/* A simple version of the standard
gets() library function. */
char *xgets(char *s)
{
char ch, *p;
int t;
p = s; /* gets() returns a pointer to s */
for(t=0; t<80; ++t){ ch = getchar(); switch(ch) { case ‘\n’: s[t] = ‘\0′; /* terminate the string */ return p; case ‘\b’: if(t>0) t–;
break;
default:
s[t] = ch;
}
}
s[79] = ‘\0′;
return p;
}
The xgets() function must be called with a character pointer. This, of course, can be the name of a character array, which by definition is a character pointer. Upon entry, xgets() establishes a for loop from 0 to 79. This prevents larger strings from being entered at the keyboard. If more than 80 characters are entered, the function returns. (The real gets() function does not have this restriction.) Because C/C++ has no built-in bounds checking, you should make sure that any array used to call xgets() can accept at least 80 characters. As you type characters on the keyboard, they are placed in the string. If you type a backspace, the counter t is reduced by 1, effectively removing the previous character from the array. When you press, a null is placed at the end ENTER of the string, signaling its termination (Null Terminated String). Because the actual array used to call xgets() is modified, upon return it contains the characters that you type.
Popularity: 1% [?]
Posted on December 13, 2009 at 6:59 am
good article on Function Arguments.