Hi @Tomas_Andersson, as it’s your first post I would like to welcome you to the community and also assure you that you are not breaking any rules - if you have questions, please do ask ![:slight_smile: :slight_smile:](https://sjc3.discourse-cdn.com/business5/images/emoji/twitter/slight_smile.png?v=5)
Responding to each of your points in turn;
The curly brackets { } were only briefly touched on as to their exact function. I felt like they were going to be explained more as we went on, and that I was gaining an understanding slowly, but as of the questionnaire had not understood them fully (even when rewatching).
The curly braces in C# are used to indicate blocks of code. To give you an example of how they may be used;
if (myName == "Rob")
{
Debug.Log("Hi Rob!");
}
else
{
Debug.Log("Erm... you're not Rob?");
}
The above is referred to as an if
statement, used for testing some logic. The curly braces used above indicate a block of code for each condition, e.g. whether the myName
variable equals Rob
or not. Anything within one of these code blocks will be run under the appropriate condition.
The void keyword was barely mentioned at all so far and I definitely did not gain an understanding of it or when to use it. It doesn’t return something. What IS return? Return what, where? I’m completely new to programming and do not understand these terms.
This may be a little soon for my more detailed explanation, but lets go for it! ![:slight_smile: :slight_smile:](https://sjc3.discourse-cdn.com/business5/images/emoji/twitter/slight_smile.png?v=5)
You code is placed into classes, these are used as templates or, cookie cutters, for creating objects (instances of classes). These classes may well have behaviours, we would refer to these as methods.
If you think of yourself as an instance of the Human class, you have specific behaviours. For example, you can Sit, Stand, Run, Greet a friend.
We may create methods to represent these behaviours. Some of these methods would just need to do something, but not necessarily provide a value or response back to whatever called them. For example;
public void Run() {
// code for running goes here
}
The method Run()
is an example of a void
being used. void is a Type, however it indicates that the method doesn’t return a value to whatever call it.
So, what about something that does return a value and use the return word that keeps getting mentioned… lets take your Greet a friend behaviour;
public string GreetFriend() {
string greeting = "Hello friend, long time no see!";
return greeting;
}
In the above example, rather than void
I have used a string
(as we will be returning some words).
Within this method I create a variable called greeting
which is also of Type string. I also populate it with a value, that is the part in the quotation marks.
Below this you can see the return
keyword, followed by the variable. So, when this method is called, it will return the value of the greeting
variable to the calling code. In a real world example, perhaps this is a message that is displayed to the user when they first start the game.
In the question about taking in float and returning int the code was formated in a way that, again, felt just one step above what I’d done so far. A bit like learning addition and subtraction and having multiplication thrown into the quiz.
I don’t recall this specific item, but I suspect it may have been something like this;
(int)maximumSpeed;
Assuming so, the (int)
in front of the variable name maximumSpeed
casts the maximumSpeed
variable from being of Type float to Type int - it has changed it’s type (it’s class). In this specific example you would be taking a number with a floating point and converting it to a whole number (and integer).
Up until now I thought I was going at a beginners pace and doing well since I was keeping up, could experiment a little in the code, when suddenly this curve ball comes along and says I should understand these things at the end of section 2 when I do not.
The fact that you have been feeling that way is fantastic, and I would really recommend to not lose sight of that. Everyone will have varying strengths/weaknesses/experiences and so on, things that are perhaps more straight forward to one person will not necessarily be to the next person. You only have to search on the forum to see the number of questions that come up for all sorts of things and at all sorts of levels.
Now I’m wondering if, when continuing, I’m just a dummy and this is obvious to everyone else?
Absolutely not a dummy! You have proven this by giving things a go yourself, stepping out of the comfort zone, you can’t expect to understand everything straight away, I don’t believe anyone can, I certainly never have. Give yourself some credit for what you have achieved so far and, the fact that you felt comfortable enough to ask some questions on the forum - not everyone does, yet everyone’s questions are so valuable. For all you know there may have been another half a dozen people wondering the same thing but didn’t have the courage to ask, your question will have now also helped them and hopefully also given them a little more confidence to ask questions.
If there is anything in my examples or explanations above which doesn’t make sense, please do ask and I will do what I can to help you
Once again, welcome to the community ![:slight_smile: :slight_smile:](https://sjc3.discourse-cdn.com/business5/images/emoji/twitter/slight_smile.png?v=5)
See also;