Results 1 to 5 of 5

Thread: [functions] - opcionals parameters

  1. #1

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    [functions] - opcionals parameters

    i know that in C\C++ we can create opcionals parameters, like the printf() function:
    printf(char *string, ...); // i have seen these header in stdio.h
    how can i do that?
    i need build 1 function that accpet several string parameters, but i don't know work with them
    any advices?
    VB6 2D Sprite control

    To live is difficult, but we do it.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [functions] - opcionals parameters

    You do it just like that. It's not like optional parameters in VB or C# but more like a ParamArray, i.e. it specifies that the method will accept zero, one or more arguments. You can't specify the number and type of the parameters that way.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [functions] - opcionals parameters

    Quote Originally Posted by jmcilhinney View Post
    You do it just like that. It's not like optional parameters in VB or C# but more like a ParamArray, i.e. it specifies that the method will accept zero, one or more arguments. You can't specify the number and type of the parameters that way.
    i can do with va_list. but i don't understand how can i show the strings
    Code:
    #include <stdio.h>
    #include <conio.h>
    #include <stdarg.h>
    
    int Menu( char *sentence, ... )
    {
    	va_list listPointer;
    	va_start( listPointer, sentence );
    	
        for( int i = 0 ; i < numargs; i++ )
        {
            char *arg = va_arg(listPointer, char );
            printf( "%s", i, arg );        
        }
    	va_end( listPointer );
    
    	return 0;
    }
    
    
    void main()
    {
    	Menu("hello", "hi", "back", "Down");
    	getch();
    }
    the 'for' isn't correct but what i read was for 'int' and not 'char' - string's
    VB6 2D Sprite control

    To live is difficult, but we do it.

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: [functions] - opcionals parameters

    This is a worthwhile read:

    http://www.informit.com/guides/conte...lus&seqNum=138

    It also mentions default arguments as an alternative, so that's basically optional parameters.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    PowerPoster joaquim's Avatar
    Join Date
    Apr 2007
    Posts
    3,961

    Re: [functions] - opcionals parameters

    Quote Originally Posted by jmcilhinney View Post
    This is a worthwhile read:

    http://www.informit.com/guides/conte...lus&seqNum=138

    It also mentions default arguments as an alternative, so that's basically optional parameters.
    thanks
    VB6 2D Sprite control

    To live is difficult, but we do it.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width