
Patrick B. answered 05/27/19
Math and computer tutor/teacher
The short answer is NO.
You must mangle the function name if only in C.
In C++, yes, you can declare default arguments.
Note that in the C++ code snippets below, the same function is used whereas in the
C code, the function has to be declared (with the name mangled) multiple times.
The only problem with the default arguments in C++ is you cannot omit the first argument
but supply the second. Once you default the argument, any parameters after that must have
default values.
Here is some code:
//======================= printChar.c ===================================
#include <stdio.h>
/* master parent function */
printChar( char ch, int n)
{
int i;
for (i=0; i<n; i++)
{
printf("%c",ch);
}
printf("\n");
}
/* defaults the char to star asterisk */
printStar( int n)
{
printChar('*',n);
}
/* defaults the count to 10 */
print10( char ch)
{
printChar(ch,10);
}
int main()
{
printChar('!',5); // prints 5 exclamation points
printChar('$',7); //prints 7 dollar signs
printChar('#',6); //prints 6 pound signs
printStar(10); //prints 10 star asterisks: note that the function name is mangled
print10('@'); // prints 10 AT symbols using a technically different function that calls the parent
}
================ printChar.cpp ==========================
#include <stdio.h>
#define DEFAULT_CHAR ('*')
/* master parent function */
printChar( char ch='*', int n=10)
{
int i;
for (i=0; i<n; i++)
{
printf("%c",ch);
}
printf("\n");
}
int main()
{
printChar('!',5); //prints 5 exclamation points
printChar('$'); //prints 10 dollar signs
printChar(DEFAULT_CHAR,7); //prints 7 star asterisks: note the first parameter must be passed
printChar('#',2); // prints 2 pound signs
printChar(); //prints 10 star asterisks
}