Sin Gularity
Sin Gularity

Reputation: 23

multiplication function giving a constant random number

I am a beginner in C++ and I was just learning about functions. My program runs, however, it gives a constant number that doesn't really make any sense to me. Anyway here is my code, thanks.

#include <iostream>
using namespace std;


       int multiplication (int x , int s)
       {
            int v;
            s=x*s;
             return v;
      }

           int main (){
        int u,k,l;
          cout<<"enter two numbers"<<endl;
           cin>>k;
            cin>>l;

          u =    multiplication ( k , l);

          cout <<"the result is "<<u<<endl;

           return 0 ;




        }

Upvotes: 2

Views: 233

Answers (1)

int multiplication (int x , int s)
{
    int v;
    s=x*s;
    return v;
}
  1. Create a variable v. Currently v holds an uninitialized (unpredictable) value.
  2. Set s to the result of the multiplication. Now s holds the result of the muliplication, and v still holds an unpredictable value.
  3. Return the value of v (which is unpredictable).

Did you mean to write return s;, or maybe v=x*s;?

Upvotes: 3

Related Questions