+ 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.
7 Answers
+ 8
Just chiming in because I saw variadics. :>
https://code.sololearn.com/cu6pxYa9577z/?ref=app
+ 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");
}
+ 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;
}
+ 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
+ 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...
+ 1
"..function parameter.." - like, what?...
0
function parameters like strings , variables , characters , etc