Solved

Question re: functions

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
AVI wait icons for CAnimateCtrl in Visual Studio 2008 MFC 1 138
gdb doesn't stop on breakpoint 2 68
max float value 3 41
Why isn't object file created? 6 59
Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
  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 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 goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

773 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