Practice Method Problem

Hi ,
I’m learning C# and I have an error. :slight_smile:

Assets/Script/MethodeReturn.cs(11,9): error CS0127: `MethodeReturn.LongHypo(double, double)': A return keyword must not be followed by any expression when method returns void

I found documentation about this error but it doesn’t help me.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MethodeReturn : MonoBehaviour {

    // Ma methode
    static void LongHypo(double a, double b)
    {
        double SommeCar = a * a + b * b;
        return SommeCar;
    }

    // Use this for initialization
    void Start () {
        var = LongHypo(3,4);
        Debug.Log(var);

    }
   
    // Update is called once per frame
    void Update () {
       
    }
}

Change return type to double @ 8;
Also, assign a variable name @ 16;

Error basically tells you to either change the return type, or don’t return anything if method return type is void.

Also, Unity scripts most of the time use float type instead of double, there’s precision reasons for that. You might want to use float in the future.

1 Like

Hi, thank you for your comment.
Excuse me , I didn’t understand "Change return type to double " at the ligne 8.
I make the correction @16 Thanks :slight_smile:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MethodeReturn : MonoBehaviour
{

    // Ma methode
    static void LongHypo(float a, float b)
    {
        float SommeCar = a * a + b * b;
        return SommeCar;
    }

    // Use this for initialization
    void Start()
    {
        float result = LongHypo(3, 4);
        Debug.Log(result);

    }

    // Update is called once per frame
    void Update()
    {

    }
}

Change this:

static void LongHypo(float a, float b)
    {
        float SommeCar = a * a + b * b;
        return SommeCar;
    }

To this:

static float LongHypo(float a, float b)
    {
        float SommeCar = a * a + b * b;
        return SommeCar;
    }

Even better way of doing it:

static float LongHypo(float a, float b)
    {
        return a * a + b * b;
    }
1 Like

aaaaaaaaaaaaah OK thank you very much !