Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Writing functions with optional arguments

bvbbvb Member Posts: 2
I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
transform("John Gunther");
transform("John Gunther",upperflag);
transform("John Gunther",upperflag,nospaceflag);

The problem is determining which arguments were supplied in a particular call, and which not.

The GNU C Library manual says,

"Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."

This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.

I must be missing something obvious, but what?

John Gunther
Bucks vs Bytes Inc

Comments

  • HK_MP5KPDWHK_MP5KPDW Member Posts: 770 ✭✭✭
    : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : transform("John Gunther");
    : transform("John Gunther",upperflag);
    : transform("John Gunther",upperflag,nospaceflag);
    :
    : The problem is determining which arguments were supplied in a particular call, and which not.
    :
    : The GNU C Library manual says,
    :
    : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    :
    : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    :
    : I must be missing something obvious, but what?
    :
    : John Gunther
    : Bucks vs Bytes Inc
    :
    :

    Don't know about C, you'd probably have to use the [b]varargs.h[/b] header along with the [b]va_list[/b], [b]va_arg[/b], [b]va_start[/b], and [b]va_end[/b] functions/data types.

    For C++ you could easily just create three versions of the function you mention, each one having a different number of function arguments but the same function name (function overloading). Or, you could create a single function where the last two arguments had default values:

    [code]
    int transform(char* cpName, int iUpperFlag = 0, int iNoSpaceFlag = 0 );
    [/code]
  • bvbbvb Member Posts: 2
    : : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : : transform("John Gunther");
    : : transform("John Gunther",upperflag);
    : : transform("John Gunther",upperflag,nospaceflag);
    : :
    : : The problem is determining which arguments were supplied in a particular call, and which not.
    : :
    : : The GNU C Library manual says,
    : :
    : : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    : :
    : : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    : :
    : : I must be missing something obvious, but what?
    : :
    : : John Gunther
    : : Bucks vs Bytes Inc
    : :
    : :
    :
    : Don't know about C, you'd probably have to use the [b]varargs.h[/b] header along with the [b]va_list[/b], [b]va_arg[/b], [b]va_start[/b], and [b]va_end[/b] functions/data types.
    :
    : For C++ you could easily just create three versions of the function you mention, each one having a different number of function arguments but the same function name (function overloading). Or, you could create a single function where the last two arguments had default values:
    :
    : [code]
    : int transform(char* cpName, int iUpperFlag = 0, int iNoSpaceFlag = 0 );
    : [/code]
    :
    Thanks, I wasn't aware that c++ had the arg default feature. I recompiled my test in c++, but when main() calls the function with missing arguments, the program won't compile with a "too few arguments to function" error. I guess I'll have to learn more about c++ to get it working.


  • tsagldtsagld Member Posts: 621
    When overloading in C++, you must declare the functions as members of a class.

    : : : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : : : transform("John Gunther");
    : : : transform("John Gunther",upperflag);
    : : : transform("John Gunther",upperflag,nospaceflag);
    : : :
    : : : The problem is determining which arguments were supplied in a particular call, and which not.
    : : :
    : : : The GNU C Library manual says,
    : : :
    : : : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    : : :
    : : : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    : : :
    : : : I must be missing something obvious, but what?
    : : :
    : : : John Gunther
    : : : Bucks vs Bytes Inc
    : : :
    : : :
    : :
    : : Don't know about C, you'd probably have to use the [b]varargs.h[/b] header along with the [b]va_list[/b], [b]va_arg[/b], [b]va_start[/b], and [b]va_end[/b] functions/data types.
    : :
    : : For C++ you could easily just create three versions of the function you mention, each one having a different number of function arguments but the same function name (function overloading). Or, you could create a single function where the last two arguments had default values:
    : :
    : : [code]
    : : int transform(char* cpName, int iUpperFlag = 0, int iNoSpaceFlag = 0 );
    : : [/code]
    : :
    : Thanks, I wasn't aware that c++ had the arg default feature. I recompiled my test in c++, but when main() calls the function with missing arguments, the program won't compile with a "too few arguments to function" error. I guess I'll have to learn more about c++ to get it working.
    :
    :
    :


    Greets,
    Eric Goldstein
    www.gvh-maatwerk.nl

  • HK_MP5KPDWHK_MP5KPDW Member Posts: 770 ✭✭✭
    : When overloading in C++, you must declare the functions as members of a class.

    Not using my compiler! MSVC++ 6.0.



    :
    : : : : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : : : : transform("John Gunther");
    : : : : transform("John Gunther",upperflag);
    : : : : transform("John Gunther",upperflag,nospaceflag);
    : : : :
    : : : : The problem is determining which arguments were supplied in a particular call, and which not.
    : : : :
    : : : : The GNU C Library manual says,
    : : : :
    : : : : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    : : : :
    : : : : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    : : : :
    : : : : I must be missing something obvious, but what?
    : : : :
    : : : : John Gunther
    : : : : Bucks vs Bytes Inc
    : : : :
    : : : :
    : : :
    : : : Don't know about C, you'd probably have to use the [b]varargs.h[/b] header along with the [b]va_list[/b], [b]va_arg[/b], [b]va_start[/b], and [b]va_end[/b] functions/data types.
    : : :
    : : : For C++ you could easily just create three versions of the function you mention, each one having a different number of function arguments but the same function name (function overloading). Or, you could create a single function where the last two arguments had default values:
    : : :
    : : : [code]
    : : : int transform(char* cpName, int iUpperFlag = 0, int iNoSpaceFlag = 0 );
    : : : [/code]
    : : :
    : : Thanks, I wasn't aware that c++ had the arg default feature. I recompiled my test in c++, but when main() calls the function with missing arguments, the program won't compile with a "too few arguments to function" error. I guess I'll have to learn more about c++ to get it working.
    : :
    : :
    : :
    :
    :
    : Greets,
    : Eric Goldstein
    : www.gvh-maatwerk.nl
    :
    :

  • HK_MP5KPDWHK_MP5KPDW Member Posts: 770 ✭✭✭
    : : : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : : : transform("John Gunther");
    : : : transform("John Gunther",upperflag);
    : : : transform("John Gunther",upperflag,nospaceflag);
    : : :
    : : : The problem is determining which arguments were supplied in a particular call, and which not.
    : : :
    : : : The GNU C Library manual says,
    : : :
    : : : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    : : :
    : : : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    : : :
    : : : I must be missing something obvious, but what?
    : : :
    : : : John Gunther
    : : : Bucks vs Bytes Inc
    : : :
    : : :
    : :
    : : Don't know about C, you'd probably have to use the [b]varargs.h[/b] header along with the [b]va_list[/b], [b]va_arg[/b], [b]va_start[/b], and [b]va_end[/b] functions/data types.
    : :
    : : For C++ you could easily just create three versions of the function you mention, each one having a different number of function arguments but the same function name (function overloading). Or, you could create a single function where the last two arguments had default values:
    : :
    : : [code]
    : : int transform(char* cpName, int iUpperFlag = 0, int iNoSpaceFlag = 0 );
    : : [/code]
    : :
    : Thanks, I wasn't aware that c++ had the arg default feature. I recompiled my test in c++, but when main() calls the function with missing arguments, the program won't compile with a "too few arguments to function" error. I guess I'll have to learn more about c++ to get it working.
    :
    :
    :

    What do mean when you say "I recompiled my test in c++"? Have you saved your source file with a ".cpp" extension? If not, then make it so.
  • fluttyflutty Member Posts: 50
    : When overloading in C++, you must declare the functions as members of a class.
    :
    : Greets,
    : Eric Goldstein

    Since when?

    Flut

  • PeterTheMasterPeterTheMaster Member Posts: 636
    : : When overloading in C++, you must declare the functions as members of a class.
    :
    : Not using my compiler! MSVC++ 6.0.
    :


    never! this isnt even worth a try.


    :
    :
    : :
    : : : : : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : : : : : transform("John Gunther");
    : : : : : transform("John Gunther",upperflag);
    : : : : : transform("John Gunther",upperflag,nospaceflag);
    : : : : :
    : : : : : The problem is determining which arguments were supplied in a particular call, and which not.
    : : : : :
    : : : : : The GNU C Library manual says,
    : : : : :
    : : : : : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    : : : : :
    : : : : : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call. I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    : : : : :
    : : : : : I must be missing something obvious, but what?
    : : : : :
    : : : : : John Gunther
    : : : : : Bucks vs Bytes Inc
    : : : : :
    : : : : :
    : : : :
    : : : : Don't know about C, you'd probably have to use the [b]varargs.h[/b] header along with the [b]va_list[/b], [b]va_arg[/b], [b]va_start[/b], and [b]va_end[/b] functions/data types.
    : : : :
    : : : : For C++ you could easily just create three versions of the function you mention, each one having a different number of function arguments but the same function name (function overloading). Or, you could create a single function where the last two arguments had default values:
    : : : :
    : : : : [code]
    : : : : int transform(char* cpName, int iUpperFlag = 0, int iNoSpaceFlag = 0 );
    : : : : [/code]
    : : : :
    : : : Thanks, I wasn't aware that c++ had the arg default feature. I recompiled my test in c++, but when main() calls the function with missing arguments, the program won't compile with a "too few arguments to function" error. I guess I'll have to learn more about c++ to get it working.
    : : :
    : : :
    : : :
    : :
    : :
    : : Greets,
    : : Eric Goldstein
    : : www.gvh-maatwerk.nl
    : :
    : :
    :
    :

  • whoiewhoie Member Posts: 672
    : I'm trying to write functions with optional arguments, e.g. a function that could be called 3 ways:
    : transform("John Gunther");
    : transform("John Gunther",upperflag);
    : transform("John Gunther",upperflag,nospaceflag);
    :
    : The problem is determining which arguments were supplied in a particular call, and which not.
    :
    : The GNU C Library manual says,
    :
    : "Some functions such as |open| take a fixed set of arguments, but occasionally ignore the last few. Strict adherence to ISO C requires these functions to be defined as variadic; in practice, however, the GNU C compiler and most other C compilers let you define such a function to take a fixed set of arguments--the most it can ever use--and then only /declare/ the function as variadic (or not declare its arguments at all!)."
    :
    : This does indeed work, but I can't figure out how to determine how many arguments were actually supplied in a particular call.

    You have to provide this mechanism in your interface. Either a format string (like printf and scanf do it), or an argument that denotes how many were passed, or a sentinel value.

    It is true, about the empty parameter list. This same subject came up in a thread a little bit ago. Empty parentheses in a function declaration do not mean void in C (as opposed to C++). They simply mean "unspecified paramters". This opens an entirely new world of undefined behavior. One of the hurdles involved, is that default promotions take place. If you had your function definition like this:
    [code=ffffff]
    char *transform(char *s, char upper, char space)
    {
    /* ... */
    }
    [/code]
    You would be walking the fine line, because the compiler would pass those last two arguments as integers (after promotion). If chars and ints don't have the same sizes and alignments on your machine, the next thing you know, you will be saying; "What are these ghastly looking demons, and why are they sitting on my nose?"


    : I would have thought the missing arguments would show up in the function as NULL pointers or something similar, but it doesn't seem to be so. Each argument, supplied or not, has an address. The unsupplied arguments seem to differ only in that their values are unpredictable.
    :
    : I must be missing something obvious, but what?

    The above mostly, and it's just not a great idea. It can prove useful at times, but I'll let you figure those out for yourself ;).

    The C++ feature of function overloading could serve you well here, but I don't think it is necessary. I would simply make your function take a pointer to the string you want to transform (not a string literal like your example!), and a second argument of unsigned int that contains bit flags denoting the operations you want to do. This keeps you from having to write 3 different functions, and expecting a user programmer to remember what 3 ones do what, i.e.:
    [code=ffffff]

    #define TRANS_UPPER 1u
    #define TRANS_MIXED 2u
    #define TRANS_NOSPACE 4u
    /* more here */

    char *transform(char *s, unsigned ops)
    {
    /* ... */
    }
    [/code]

    Then, the interface is simple for the caller:
    [code=ffffff]
    char me[] = "John Gunther";
    char me_too[] = "Jennifer Lopez";

    puts( transform(me, TRANS_UPPER) );
    puts( transform(me_too, TRANS_UPPER | TRANS_NOSPACE) );
    [/code]
    Less work, and cleaner, clearer code. Although, you may have a different opinion about it. I still don't know why you would pass a string to a function like 'transform' without any arguments. That seems a little "iffy" to me, would you care to explain what that is supposed to do? It isn't obvious from the context.


    HTH,
    Will
    --
    http://www.tuxedo.org/~esr/faqs/smart-questions.html
    http://www.eskimo.com/~scs/C-faq/top.html
    http://www.parashift.com/c++-faq-lite/
    http://www.accu.org/


  • Chris BrownChris Brown USAMember Posts: 4,624 ✭✭

    _________ // http://forcoder.org // free ebooks and video tutorials about | C# JavaScript Java Delphi PHP Assembly Python C Swift R Visual Basic Scratch Visual Basic .NET Go Ruby PL/SQL Perl Objective-C MATLAB C++ D Bash Dart LabVIEW Julia Scheme Scala FoxPro Ada Erlang ML SAS Apex COBOL Clojure ABAP F# Rust Transact-SQL Prolog Logo Hack VBScript Fortran Lisp Awk Crystal Kotlin Alice Lua | ___

Sign In or Register to comment.