Be the first to post for 'Functions With Variable Parameters'!

If you’re reading this, there probably aren’t very many posts yet. But don’t worry, you can be the first! Either create a new post or just reply to this one to say ‘hi’.

There’s a quiz question about this video, I’d like to ask you opinion about it

-How might you DECLARE a function that finds the square root of a whole number (integer)?
-the correct answer is the function definition, even when there’s a valid function declaration among the choices. video from 02:02 to 02:08 also uses what I’d consider the correct terminology. a fix would be to use DEFINE in the question, and remove the declaration answer option to avoid confusion

1 Like

I’ve done this in my first attempt and it worked well:
section 2 lecture 15 parameter

1 Like

I’m a bit off on this question, I felt there were something that I didn’t pay enough attention to, so I was unable to understand the question. So I went back to the video and Im still comfused. Can I go back to see the questions and answers again somehow?

Update: I could retake questions, I didn’t see the link in the bottom left corner. But I really first understand it when I had written the script to do it. I get it now. Thanks for your post, that made me do a lot more research.

using UnityEngine;
using System;

class DeclareFunktionSquare : MonoBehaviour
{
    void Start()
    {
        FindSquareRootInt(25);
    }

    // Function that finds the square root of a whole number
    void FindSquareRootInt(int squareint)
    {
        Debug.Log("Question: DECLARE a function that finds the square root of a whole number(integer) The square root of '" + squareint + "' is " + Math.Sqrt(squareint));
    }
}


Anyone know how to make all the choices more closer together? I can’t seem to make choice #3 bunch up with the rest

My guess is that you’ve gotten too close to the right side of the monitor screen with the text for question number 2 and it’s actually causing a little wrap around, even if that wraparound is blank space.

Since in this case, the monitor is an asset given to us rather than one you own and could adjust its behavior more easily, I’d probably just recommend being more succinct with your phrases. Maybe remove the redundancy in starting every line with “Press”.

There are a few ways to do so, but a quick one that should address your problem would be to replace “Press 2 for …” and “Press 3 for …” to just “Or 2 for …” and “Or 3 for …” and I think it will still carry a clear enough meaning in this case, whilst also giving you the screen real estate to fit the text more easily.

1 Like

Thank you! that worked like a charm :smiley:

1 Like

Continuing the discussion from Be the first to post for 'Functions With Variable Parameters'!:

Hi,
I was thinking the same way as you, about to create a var(string) call it greeting and with the “Hello Ben”, so why in the lesson, we have Terminal.WriteLine (greeting) and in the void ShowMainMenu (string greeting), and finally in the ShowMainMenu (“Hello Ben”). I didn’t create a greeting variable, but still when play the unity it works . How is Hello Ben called when there is not a string/variable name greeting??? Ahhhhh . I need help. I have sent questions to udemy and nobody answer me.
thx
Isabel

The short answer is that “Hello Ben” is what is known as a “string literal (constant)”.

These can be used in most places that a “string variable” could have been used. The only differences being that it has no name you can reference, you are just using the literal value, and it can’t be changed, because you don’t have a named variable to hold it.

Once you pass it to the ShowMainMenu() method, which does have a string variable as its first argument, and happens to be called greeting, you can then treat it like a variable that has that content (“Hello Ben”) assigned to it.

Even if you had declared a variable earlier, such as: string greeting = “Hello Ben”;

Notice you are still using a “string literal (constant)” on the right side of that assignment. So wherever you could pass along the value held in greeting, you could instead just pass “Hello Ben” directly too.

Privacy & Terms