Home > Enterprise >  Returning value from method and "The Value Assigned Is Never Used"
Returning value from method and "The Value Assigned Is Never Used"

Time:11-16

I'm following Princeton's introductory computer science course (I'm not a student, just teaching myself). I working on this assignment.

Main is calling two methods: amplify and reverse, both of which return an array. Amplify multiplies all values in the array by a constant alpha. Reverse returns an array that lists the original array values in reverse order, ex. {1,2,3} -> {3,2,1}.

Amplify works fine, but nothing happens when I call reverse and I get a bug that states: The Value Assigned Is Never Used

public class audiocollage {
    // Returns a new array that rescales a[] by a factor of alpha.
    public static double[] amplify(double[] a, double alpha) {
        for (int i = 0; i < a.length; i  ) {
            a[i] = a[i] * alpha;
        }
        return a;
    }

    // Returns a new array that is the reverse of a[].
    public static double[] reverse(double[] a) {
        double[] b = new double[a.length];
        for (int i = a.length - 1, j = 0; i >= 0; i--, j  ) {
            b[j] = a[i];
        }
        return b;
    }

    // Creates an audio collage and plays it on standard audio.
    public static void main(String[] args) {
        double[] samples = StdAudio.read("cow.wav");
        double alpha = 2.0;

        samples = amplify(samples, alpha);
        samples = reverse(samples);
    }
}

CodePudding user response:

It sounds like you have two questions:

  1. Why doesn't anything happen when I call reverse(samples)?

The code you're showing does nothing with the result of reverse(samples) other than store it in the variable samples (overwriting its previous value). You will need to do something with samples after that to observe the new array (like printing samples, which should now appear to be reversed).

Which leads into the next question:

  1. Why do I get a warning about "the value assigned to samples is never used"?

This is a warning saying that the code you wrote doesn't do anything. Dead store to local variable is the first line of the warning, which describes what's happening: the value stored to samples is "dead" -- it is never used again, and so we may as well have skipped that line altogether. That causes your compiler (or extension) to give us a warning because it's almost certain that the code you wrote is not doing what you intended, so in many cases that warning can be helpful for spotting mistakes.

This warning can be resolved by using samples somehow, such as by printing it, calling another function with it, etc.

The previous line

 samples = amplify(samples, alpha);

doesn't generate that warning because it's output is used in the following call to reverse():

samples = reverse(samples);
//                  ^ usage of `samples` variable!

This is made even clearer by using different variables for all your arrays:

    public static void main(String[] args) {
        // No warning; samplesRaw used later
        double[] samplesRaw = StdAudio.read("cow.wav");
        double alpha = 2.0;

        // No warning; samplesAmplified used later
        samplesAmplified = amplify(samplesRaw, alpha);

        // WARNING! samplesReversed is never used!
        samplesReversed = reverse(samplesAmplified);
    }

CodePudding user response:

If the hint is related to the last line, it means you have a local variables samples which has not used ( you assigned a value but never read it)

  • Related