Solved

Question re: functions

Posted on 2004-10-17
2
185 Views
Last Modified: 2010-04-01
If you were to have a case statement in which a function is called for each of the 8 cases, however, a file

case 1:
   runProgram(vector1, filename, filename2);

case 2:
   runProgram(vector2, filename, filename2);

case 3:
   runProgram(vector3, filename, NULL):

However, with case3, a second filename is not needed.  Is it sufficient to pass in NULL????  I would rather not create 2 separate functions since it would be rather redundant.

0
Comment
Question by:jewee
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 500 total points
ID: 12335818
Yes, you are right, don't have to make a new function, just have to evaluate if NULL inside the function, but this will work only if filename2 is a pointer (by example a pointer to a char array or a pointer to a string object)
Also you can exploit the "default value" feature of C++. By example:

yourFunc(int firstArg, char *secondArg=NULL);

you can invoke it these ways:

yourFunc(15,"a string");
yourFunc(15,NULL);
yourFunc(15);   // has the same effect of previous
0
 
LVL 4

Expert Comment

by:pankajtiwary
ID: 12335842
Hi jewee,
You can use the default argument technique of C++. The only problem is you can specify the default arguments only starting from the right hand side.

void runProgram ( some_type vector , string filename1, string filename2 = "" ) ;

case 1:
   runProgram(vector1, filename, filename2);

case 2:
   runProgram(vector2, filename, filename2);

case 3:
   runProgram(vector3, filename):
 
will solve your purpose.

Cheers!
0

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

735 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question