Thread: Optional Function Parameters in Prototypes

  1. #1
    Registered User
    Join Date
    Sep 2006
    Posts
    24

    Optional Function Parameters in Prototypes

    The name pretty much says it all. How do you do it, specifically in header files?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    use = after the parameter with the default value, this way if the parameter is missing when called this default value will be used, example:
    Code:
    #include<iostream>
    
    int stuff(int a=3);
    
    int main()
    {
    	std::cout<< stuff();
    }
    
    
    int stuff(int a)
    {
    	return a;
    }
    prints 3. hope this helps
    Last edited by nadroj; 10-20-2006 at 02:25 PM.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Posts
    24
    Well, the problem that I'm having is in my header file... If I declare a function that takes an optional argument of NULL, it says NULL was not declared in this scope. What exactly does that mean?

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I believe you want null not NULL

    EDIT: Just tested it, hmm odd.
    Last edited by Wraithan; 10-20-2006 at 03:08 PM.

  5. #5
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    do you have the proper code to reference the defined NULL value? try to include<iostream>. or instead of NULL just use 0 (zero) if need be. or post the relevant code if you can

  6. #6
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    NULL is defined in windef.h (which you get if you include windows.h) I agree though, 0 is what the MinGW implementation of Win32 API defines it as...

    Code:
    #ifndef NULL
    #ifdef __cplusplus
    #define NULL 0
    #else
    #define NULL ((void*)0)
    #endif
    #endif
    I don't have Win SDK from MS installed on this computer so I can't check what MS defines it as, but I will assume it is simular.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    i get error if i try and reference NULL, however when iostream is included it works..since this is a popular header its not a bad idea to include this if it solves the problem.

  8. #8
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    windef??? NULL is defined in cstddef and cstdlib. That it works when including iostream is just coincidence.

    Or just use the number 0. It works the same in C++.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Ahh, well it may be defined there too, I just know it was defined in the windows headers. I used Code::Blocks find declaration feature (I <3 it)

  10. #10
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    well it looks like theres afew CORRECT solutions here. he needs to include the most appropriate header which has a reference to the NULL definition.

  11. #11
    Registered User
    Join Date
    Sep 2006
    Posts
    24
    Thanks! iostream, huh? A little odd.

  12. #12
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The most appropriate if you need NULL is <cstddef>. <iostream> is inappropriate.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    why is inappropriate? it worked in my case, and ive read on university notes that it is defined in iostream.
    im not trying to start an argue im trying to see why you say its better than what i have said. thanks

  15. #15
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    Quote Originally Posted by nadroj
    i get error if i try and reference NULL, however when iostream is included it works..since this is a popular header its not a bad idea to include this if it solves the problem.
    The easiest solution is to #include <cstddef> in the header file.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  2. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  3. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 08:55 AM
  4. Replies: 5
    Last Post: 02-08-2003, 07:42 PM