path error code, why?

V

vinnie

I get this error:

" Error 1 'calcolo_percentuale.Program.CalcolaPercentuale(double,
double, out double, out double)': not all code paths return a value C:
\Documents and Settings\Robert\Desktop\prova VS8\calcolo percentuale
\calcolo percentuale\Program.cs"

Why? I don't understand where is the error: the two parameters ar
returning the value of primo and secondo. So what the error?


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace calcolo_percentuale
{
class Program
{

static double valore, percentuale;
static double primo, secondo;
static void Main(string[] args)
{
Console.WriteLine("questa procedura calcola la percentuale
di un numero "
+ "tramite la chiamata a out..");
Console.WriteLine();
Console.WriteLine(" Inserisci la percentuale espressa in
unita': ");
double percentuale = Convert.ToDouble(Console.ReadLine());
Console.WriteLine(" Inserisci ora il valore su cui
calcolerai la percentuale: ");
double valore = Convert.ToDouble(Console.ReadLine());
CalcolaPercentuale(percentuale, valore, out primo, out
secondo);
Console.WriteLine(" Il calcolo, tramite la chiamata alla
funzione "
+ " Calcola percentuale e': \n"
+ " \n Prima percentuale (variabile): {0} \n"
+ "\n Seconda percentuale (fissa al 6.75%) e': {1}",
primo, secondo);
}

static double CalcolaPercentuale(double a, double b, out
double primo, out double secondo)
{
primo = 0;
primo = (valore * (percentuale / 100));
secondo = 0;
secondo = (valore * (6.750 / 100));

}
}
}
 
L

Lit

your static double CalcolaPercentuale does not have a return
statement.

may need static void CalcolaPercentuale and not return a value since you
have out parameters.

Lit
 
V

vinnie

your static double CalcolaPercentuale does not have a return
statement.

may need static void CalcolaPercentuale and not return a value since you
have out parameters.

Lit
Hi Lit, it has parameters.

Or probably i don't truly understand how it should be.
 
V

vinnie

Hi Lit, it has parameters.

Or probably i don't truly understand how it should be.

I'm sorry, i got you.

But if i'm doing a math operation, and those values are then passed to
the Main(), why it has to be void and not double? Doesn't the Main(),
call a method through which it gets 2 new values? (In this case the
percentage)

thanks
 
L

Lit

A function must pass back a value like in return xyz; can only pass back
one Object ( the object can have one or many values in it, int, Array or
values or a collection )
or as you did when you want two results returned you can call a method
with Out Parameters

You can call two or multiple functions

or

You can pass back a class or a struct that has all the Values you want.

You can return null

you can put void to make it a method.

VB.NET does not complain but C# does you must return some value if you have
a function.

Lit
 
V

vinnie

A function must pass back a value like in return xyz; can only pass back
one Object ( the object can have one or many values in it, int, Array or
values or a collection )
or as you did when you want two results returned you can call a method
with Out Parameters

You can call two or multiple functions

or

You can pass back a class or a struct that has all the Values you want.

You can return null

you can put void to make it a method.

VB.NET does not complain but C# does you must return some value if you have
a function.

Lit

Oh, ok, let's see if i understood right:

1) a function can only pass back one value (such as using return );
2) i was trying to make one function to return more values, which is
wrong;
3) to make that function to pass back more values, i need to make it a
method, that i achieve with the using of VOID.

Is that ok, or i'm missing something?

Btw, thanks a lot for your help
i really appreciate :)
 
L

Lit

Hi,

Yes you can make it a method and move on.

static void CalcolaPercentuale(double a, double b, out
double primo, out double secondo)
{
primo = 0;
primo = (valore * (percentuale / 100));
secondo = 0;
secondo = (valore * (6.750 / 100));

}


or you can keep it as a function, remove the Out Parameters and Make your
return an Array, a struct or an class


static double[] CalcolaPercentuale(double a, double b)
{
double[] ps = new double[2];
ps[0]= valore * (percentuale / 100)
ps[1] = valore * (6.750 / 100));
return ps;
}


public struct primoSecondo{
public double primo,secondo

public primoSecondo(double Primo,double Secondo)
{
primo= Primo;
secondo = Secondo;

}
}

static primoSecondo CalcolaPercentuale(double a, double b)
{
primoSecondo ps = new primoSecondo(valore * (percentuale /
100), valore * (6.750 / 100));
return ps;
}


Or ETC....

Warning: I did not test any of the code above may contain Syntax errors
etc. etc. also



Lit
 
P

Peter Duniho

vinnie said:
Oh, ok, let's see if i understood right:

Doesn't look like it.
1) a function can only pass back one value (such as using return );

A function can only have at most one _return_ value. This is the value
that, when the function name itself is used in an expression, is used in
the expression in which the function name is used.

However, a function can pass back arbitrarily many values, via a variety
of mechanisms, including the return value (which itself could
encapsulate multiple values, if it's a struct or class instance),
passing variables by reference, and passing reference types by value,
all of which can be used simultaneously.
2) i was trying to make one function to return more values, which is
wrong;

It's not wrong as long as there's a good reason to do so. If you have a
good reason for having two "out" parameters, that's fine.
3) to make that function to pass back more values, i need to make it a
method, that i achieve with the using of VOID.

In C#, everything is a function. Or everything is a method. Or both.
It just depends on how you use the words.

But regardless of how you use the words, not all functions (or methods)
return a value. Some have "void" as the return type, meaning they don't
return anything.

Is this required if you intend to pass back multiple values? No, not at
all. You can have a return value _and_ you can have parameters passed
by reference, for example.

But, the example of code you posted does not include a line of code that
returns anything, so it sure seems as though declaring the function as
returning a "double" is an error.

_In your specific example_ it appears that the correct thing to do is
simply change the function's return type from "double" to "void". But
this is not the required solution. You could instead actually return a
value from within the function, and that would also make the error go away.

Now, if you finally understand all that...

In addition to having mis-matched return type as it relates to the
actual code, your CalcolaPercentuale() function has some other oddities:

* You initialize the "out" parameters to 0 and then immediately
after assign new values to them. There's no point in initializing them
to 0 in this case; it's useless code (hopefully removed by an optimizer
somewhere along the line, but still...you should work on not writing
useless code).

* You have two other parameters that don't appear to be used at
all: "a" and "b". They are never referenced in the code you posted, so
why do they even exist?

* In the Main() function, you've declared local variables with the
same name as the static members "percentuale" and "valore". I'm
surprised you don't get a compiler warning about that, but they hide the
static members from code in the Main() method. This may be related to
the previous issue, since the CalcolaPercentuale() function uses the
static members; perhaps you meant to use the parameters "a" and "b"
instead of the static members.

On this last point: if you meant to use the parameters "a" and "b", that
begs the question: why have the static members at all? Conversely, if
you meant to have the static members, why bother with the local
variables? Possibly this would also lead to the question: why bother
passing the values into the CalcolaPercentuale() function? Since they
are already accessible, if you always intend for the function to use
those specific values, they need not be passed in every time. Just use
the static members directly (as the function does now) and remove the
"a" and "b" parameters (which aren't being used now anyway).

Pete
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top