+ 1

Why the code is giving me garbage vaue + error at the last !!!

#include<iostream> using namespace std; int myname(int a,int b=2){ cout<<"Ashutosh is a Good Boy"<<endl; cout<<a<<endl; cout<<b<<endl; } int main(){ int a; cin>>a; int c=myname(a); cout<<c<<endl; return 0; }

23rd Feb 2022, 7:11 AM
Sabnis Ashutosh
Sabnis Ashutosh - avatar
2 Réponses
+ 4
You're not returning a value, that's why it's resulting in a garbage value. I don't know what that random number is, but returning something will fix it. Below's your code fixed https://code.sololearn.com/cdC80P5Preq8/?ref=app
23rd Feb 2022, 7:30 AM
Rishi
Rishi - avatar
+ 3
The function myname says it returns an integer value, which you then try and print out. If you don't return a value then it will create an integer with a garbage value and that will be what's printed out
23rd Feb 2022, 9:36 AM
Daniel Huggins
Daniel Huggins - avatar