Hello, this is Juhwan Kim
This is the second lecture of Unreal C++ basics
In this lecture, we will learn C++ grammar
We will get to know of C++'s data type, variables, and functions' meanings
and I will explain them
I will let you know how to interpret and use
conditionals, iterations, and expressions
Meaning of function in C++
First, data type and variables
Data type
is the form for storing data
Data is not refined
The difference between data and information is
whether it has a meaning or not
Data type differs in size or expression method
according to the type needed to put data in computer memory
For example, there are types that can store integers
floating points
like decimals or fractions
characters or strings
boolean, enumerated, and void types
Let me tell you about variables that can store various types like these
As you can see in the table, there are various data types
but the basic one is 'int'
which stands for integer
The content can vary
but usually int has the size of 4 bytes
It can express from -2.1 billion to 2.1 billion
Unsigned int means int without a sign
Since it does not have a sign, we ignore the negative numbers
and it can express up to 4.2 billion
Like this, there are short, unsigend short, long, unsinged long, float
double, character, unsigned character data types
Unlike int type that can only express integers
float and double, which are real number types, are engineering expressions
and it can express from 3.4*10^-38
to 3.4*10^38
When we talk about data type and variables
you can consider variables as the bowl
to put values expressed as the forementioned data types
To put it easier, when you put a value into a bowl
whether that be an integer, real number
or a string
it can become the integer 3
real number 3.0
or the string of 3
the way that is desired
So it is declared and defined based on the data type
Now I'll tell you about declaration and definition of variables
It is about grammars
first the data type comes
and then the variable name
then an equal sign, and finally the value
You also have to put a semicolon at the end
For example, when you say int a = 3
It means that in the variable named a with the data type of int
the integer 3 is contained
The next one says float fValue = 1.0f
and this means
it is in the form of a floating point
In the variable named fValue
with the data type of float, floating point
the constant value 1.0f is contained
The reason why there's an f at the end
is to show that it's in a float type
that is, a floating point type
The next one says char c
This means that the variable c in the form of a string
is declared but not yet defined
There is no value substituted so it is not defined
and it is just declared that the variable character c will be used
I explained briefly about data type and variables
and next I'll talk about functions
Functions are
fundamentally the same with functions in maths
In mathematics, they must be one-to-one
and if there's an input there must be an output
but in functions in programming, there does not need to be outputs
So I'll tell you about return value which is the output, reference, and the mechanism
To put it simply, function in programming is function in mathematics
expanded in various forms
In another way
it is a defined element that can perform a function independently
and a group of functions are called module
And this function unit is at the core of structured programming
Depending on programming languages, function names differ
but function is the most basic form
and sometimes it is called sub-routine
Sub-routine means the same thing as function
but it stresses that it diverges from the normal routine
and comes back after performing
It is also called method
Method is used in languages like Java or C# that emphasize object orientation very much
From the word function
and expanding on the meaning of performing a function
they use the word method meaning that it is about the methodology of it
It is also called procedure
Procedure is used to emphasize the server or the process
saying that it performs something orderly
Now we'll talk about the shape of the definition of functions
There is int which is similar to variables
and that is the type int
which is a return type that I'll explain later
int, space
Add indicates the name of the function
It is the name, and then parenthesis
int A and int B
These are the parameters of the function
It is also called parameter(in Korean)
which is the input of the function
int on the left
is the output data type
and int a and int b on the right
are parameters, which are the inputs
Under that, there are curly brackets
which indicate the body of the function
The two slashes and the part that says function implementation
is called the annotation which in programming
is complied and does not have a meaning but that explains
Then it says return a plus b
To give you a brief explanation about this int a Add function
it is a function receiving integers A and B
and returning a plus b, the sum
Let me give you an example of function definition order
On the left, it says int main
and void function 1, void function 2, void function 3
3 functions are there
It is a diagram showing the name of the function and its body
Starting from int main
it defines functions 1, 2, and 3
Function definition
puts a value into the parameter of a function and gets a return value after performing
The int main in the example
usually means the main function
that performs in a console project
and the main function is called the entry point
When you run a program
it will perform one by one starting from
under the curly bracket under int main
So it meets function 1
meeting function 1 parenthesis
it defines the first function, function 1
Then the implementation flows
to the void function 1
on the top middle source
Performs the function
and when it meets return
it comes back to function 1 on the left
The process goes on
and upon meeting function 2
it goes to the bottom middle
where void function 2 body is
and again performs
void function 2
and the next step
is where it meets the void function 3 definition
Then void function 2
defines function 3
moving to void function 3
on the right
performing the function
and coming back
to void function 2 upon meeting the return
Then again it meets return
going back to the main function
and performing what comes after the void function 3 parentheses
The process can be shown with arrows
Let me give you a bit more explanation
on parameter that I mentioned before
It is about
ind Add function that I showed you
Parameter is the part that indicates
input in functions of mathematics
When you have a function that adds two integers
since you have to put two integers in
you need to know how
the two integers will be portrayed
What's red here, that is
int a and int b are the parameters
If parameters are there like that
from the part that uses the function
we can put values in
which are called arguments
So, where it says int a and int b
3 and 6 are put in
3 is int a
and 6 is int b
So upon return
We saw that it would return a plus b
So on return
we have the variable c
c becomes return type
or return value
When I was first explaining the function
int Add
The int type at the very front of the function's name
is the return type of the function
The calculation will be done
and it is returned in that int type
In this case, it is 3 plus 6
so 9 will be returned
Return type and parameter type are together
called function signature
These are what have the function's
form as signatures
We didn't learn it yet
but when you learn Lambda function or Delegate
you will get to know about function signature
To give a brief explanation, function signature is
a grammar that expressed what function it is
What you should note is that
parameter, argument, signature and such
are translated to
formal parameter, actual parameter
formal argument, actual argument, function signature
and such
and it would be great to organize these according to their meanings
and learn them
The reason why we use functions
is to avoid having overlapping codes
We can gather several duplicate codes
and turn them into one
That way, reading the code becomes easier
and when you need to change the code
only the body of the function needs to be edited
and the part that defines doesn't need to be
Functions are already used in console main
and Unreal has countless functions
They are used in application programming interface
To put it easily
in Unreal, to perform or run
a function
there is a function for that
and there are many of them
I gave a brief explanation about functions
but apart from this
there are many other topics on functions
Call by value and call by reference
determine whether a value
or a pointer or reference will be input
There is also recursion
which is a function that defines itself
You will also learn class' member function later on
There is also function pointer
which takes the defining part as the value
so that the defining happens later
meaning that the caller and callee are switched
Statement and expression
Next is statements
Statements
contain
conditional statement, iteration statement, and break statement
Statements, put easily
are what controls the flow of a program
The most basic example
is an if statement
if statements, according to certain conditions
the divergence differ
MSDN, Microsoft Development Network's
website's URL on the bottom
will give you an explanation on statements
I will give you a brief explanation
about how the code if works
with a diagram
On the left you can see a black circle
which is one line of a code
and at that point it meets a condition
if a is true
The triangle is called the diagram of the flowchart
Depending on whether a is true or false
the flow is divided
If it's true, to the left
and if false, to the right
That was an example of an if statement
and looking at the third diagram
it uses continuous ifs
where if then is used when the content of if is true
and else is for when it's not
When one condition is true
it reaches end
and if not
it goes back to if
which is shown in this diagram
There are many other conditionals
other than if conditional statements
Switch-case has one condition
and diverges multiple ways according to the value
For example, depending on variable 2's value which can be 1, 2, or 3
it diverges to the right like this
Iteration means repeating a certain statement
and for statements
is a statement that gives a variable a beginning
and end point and which repeats according to conditions
According to the condition in the middle
it can also break
While statement is also an iteration
In case the condition inside while is true
it repeats endlessly
Do-while is a while with a condition
that first performs
and then keeps editing according to the conditions that follow
Among interations
it can break from the loop
or keep performing
This is called break statements
Having break or continue
in the conditions
it can break from the cyclical loop
or ignore it and continue
Now let's move on to expressions
Expressions
are elements like
conditionals, values, variables, operator and function definition's
combinations
Put simply, it is an equation that can be evaluated as value
Parts of the if statement like a is true or false
or equal to 3 are expressions
There are formula and discriminant in expressions
What must be mentioned along with expression is operator
which is
literally what operates
Arithmetic it mathematics can be a good example
There are many types of operators
which can also be seen on MSDN
I made a table of operators
There are operators with signs
showing negative or positive
and arithmetic operator
for arithmetics
and relational operators like not equal to, bigger or equal to, bigger, and smaller
logical operators
and operator and or operator
cast operator that changes types
increment and decrement operators, assignment operator, comma operator, and sizeof operator
There are various operators in C++
Let me explain the most basic operator
On the screen it says a=1
In mathematics, this means that you substitute a with 1
and in C++ as well, the constant 1 is substituted with the variable a
The two equations under that
are statements that increase the value of the variable a
but the expression is a bit different
The one on the left is substituting variable a with the value of a+1
If the value of the variable a was 3
3+1 is 4 so 4 is put in
making a increase by 1
The one on the right is a simplified version of the one on the left
which means
substituting a that is increased by 1
Increment and decrement operator can also be used to express a increasing by 1
It can be simplified even more to a++ or ++a
Prefix and postfix expressions' difference is the order of
substitution and reflection
Relational operator is similar to mathematics
and depends on the value of true or false
Equality operators are similar to math as well
and truth or falsity depends on whether it is equal or not
Conditional operators diverge according to certain conditions
which is similar to if statements
According to the truth value of the
statement inside the parentheses
D or E next to the question mark is performed
Next is cast operator
Cast operator changes types
It can change an int value
to a float value that can have a real number
It's not as simple as just changing types
because integer 3.3 and real number 3.0 are different values
So there needs to be a process of changing the value
and a specific explanation can be found on the MSN page
There are orders in operators
You would have learned in mathematics
but when there are several operators
orders determine what is performed first
Operators have orders and combination directions and rules
For example, in mathematics, multiplication
comes before addition
and it is similar in operators
When you're unsure of the order
you can use parentheses that force orders
Now we'll move on to C++ operators' orders and associativity
On the top, there are square brackets, parentheses, period, arrow, ++ and --
which are increment and decrement operators that use equations
When these operators are put in equations
the calculation is done from left to right
Simply, ++ operator
in a++ means that a increases
Second, sizeof, ampersand, asterisk
which are and and multiplication
+, -, ~, !, and ++ and -- for prefix
These are unary operator with one operand
These are operators that work on one subject
and sizeof can figure out the size of a variable
so it looks like sizeof(variable)
& and * are put before a variable
to bring value from the address value of that variable
+ and - operator that are not addition and subtraction
represent positive or negative numbers
and it lets us know the sign of a variable
These are unary operators
and in this case, the operator works when it is put from right to left
You could think of -a or +a
Casting operator is also unary
and it casts one variable or type
and changes it
Next we have multiplication, division, and remainder operator
which are related to multiplication
and have two operands
like multiplying A and B or dividing C by D
They are operators that calculate the remainder of two values
It goes from left to right
like multiplying 3 by 2
There are also shift operator and relation operator
Shift operator shifts bit
which moves binary numbers
and also makes the result of multiplication by 2
Relation operator includes larger than or smaller than
like A is smaller than B
or A is larger than B
On the second page, there is equality operator
In mathematics, only one = is used
but in programming, two are used
== means A is equal to B
and != means A is not equal to B
This also goes from left to right
so it is read as A is equal to B
A==B means
A equals to B, not B is equal to A
Bit operators are operators for bits
logically like AND and OR
and this also has the same
order as equality operator
There are also conditionals, simple and compound assignment, and sequential operators
which also, like the ones mentioned earlier
paying attention to the order must be noted
Meaning of functions in C++ Data type The form to store data in computer memory
int(integer), float(floating point), char(character), std::string(string), bool(boolean), void
Variable The bowl for values, declared and defined based on data type
Function Element that can independently perform a function Avoids duplicate codes Only edits function body when changing code
Statements and expressions Statements What controls the flow of a program
Conditionals: if, switch-case Iterations: for, while, do-while Break: break, continue
Expressions Combination of elements like value, variable, operator and function definition Operator: sign operator, arithmetic operator, relational operator, logical operator, cast operator, increment and decrement operator
substitution operator, conditional operator, comma operator, sizeof operator Must pay attention to the orders and associativity
The End