Solved

Question re: functions

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

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…
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now