+ 1

In C/C++, how to pass a string as a function parameter? And how to pass any , random data type as a function parameter?

I want to define python's print() function in C++ but I don't know.

1st Dec 2017, 10:19 AM
Digitalboy
7 Answers
+ 8
Just chiming in because I saw variadics. :> https://code.sololearn.com/cu6pxYa9577z/?ref=app
2nd Dec 2017, 9:58 AM
Hatsy Rei
Hatsy Rei - avatar
+ 7
You may use templates when you don't know the type of some variable or wish to overload the function for all the types available. Eg - template<typename T> void print(T arg) { cout<<arg<<endl; } int main() { print(4); print("\n"); print("Hello\n"); }
1st Dec 2017, 11:13 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 7
A simpler version : template <class T, class ...U> void print(T a, U... args){ cout << a << " "; print(args...); } template <class T> void print(T a){ cout << a << endl; } void print(){ cout << endl; }
2nd Dec 2017, 8:52 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 6
Something that looks like python's print but with a formatting and no parameters like sep and end Edit : forgot the link šŸ˜‚ https://code.sololearn.com/cI4Qf6kN8a3Z/?ref=app
2nd Dec 2017, 7:03 AM
Baptiste E. Prunier
Baptiste E. Prunier - avatar
+ 2
@Digitalboy Yes for just a single input, you may use the function above. And for n inputs like the true python's print function, use Baptiste's Code for reference...
2nd Dec 2017, 8:44 AM
Solo Wanderer 4315
Solo Wanderer 4315 - avatar
+ 1
"..function parameter.." - like, what?...
1st Dec 2017, 10:36 AM
stKhaDgar
stKhaDgar - avatar
0
function parameters like strings , variables , characters , etc
2nd Dec 2017, 7:46 AM
Digitalboy