Solved

Question re: functions

Posted on 2004-10-17
2
186 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
simple way to manipulate chinese characters in non unicode mfc c++ application. 9 249
Best book to learn C++ 4 96
keep track of class structure 1 50
visual C++ 1 37
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
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.

738 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