Download presentation
Presentation is loading. Please wait.
Published byBeryl Morris Modified over 6 years ago
1
Variables A piece of memory set aside to store data
When declared, the memory is given a name by using the name, we can access the data that sits in memory we can retrieve the value, we can change the value
2
Variables a variable must be declared before it can be used type name;
type name = value; type - the type of data to be stored integers (int, long) real numbers(float, double) characters (char) logic (bool) name - a legal identifier value - a literal value
3
Variables int x; int y = 0; double avgSpeed = 5.9;
boot isReady = false;
4
Variables Once declared, you just use the name to access the data
double avgSpeed = 3.5; double time = 100.0; double distTravelled = 0.0; cout << avgSpeed; avgSpeed += 1; distTravelled = avgSpeed * time;
5
Functions Functions are blocks of code that perform a specific task
Functions are assigned a name and can be called many times Functions can accept input and provide an output Functions MUST be declared before they can be used Functions that are declared MUST also be defined
6
Functions Declaring return name(type param, type param, ...);
done before use, so let's put it before our main return name(type param, type param, ...); return - the type of output we expect void means no output, or int, float, double, long, bool mean return of that type name - a legal identifier param - a variable to represent the input
7
Functions void printMessage(); void printNumber(int num);
bool isRobotWorking(); double areaOfCircle(double radius); The declaration is also called the prototype
8
Functions If we declare a function, we must also define it
give it some instructions to complete its task We usually define it near the end of out code, after the main the definition starts with the same prototype as before has a block of code if an output is needed, must call return
9
Functions int myFunction(int param1, int param2); int main(){ }
return param1 + param2;
10
Functions Inside the definition we can have any legal code
variable declarations operations calls to other functions The parameter variables exist as if they were declared inside the function A function that has an output MUST call return once return is called, the function is over
11
Functions Using functions we use functions to wrap up a block of code
this block of code is then simply called by its name the function can be called once, or multiple times when a function is called, we jump to its definition and execute its code, once finished we return to where the call originated and continue from there
12
Functions Parameters parameters are variables that hold the input to the function when you call a function with parameters you MUST provide values to those variables int getAvg(int num1, int num2); to call getAvg(3,4); int y = 2; int x = 3; getAvg(y,x);
13
Functions Returns (output) cout << getAvg(3,4);
functions that return a value are used in the same manner as variables the value can be output to the screen, put into a variable, sent into another function, used in an expression cout << getAvg(3,4); double y = areaOfCircle(r); int minTemp = calcTemp(getPressure());
14
Functions Pass by Reference
if we include the & symbol before a parameter, we pass it in by reference. Thus, any changes made to that variable are reflected in the variable that was passed into the function int fun(int& a);
15
Functions void fun(int& a); main(){ int x = 0; fun(x);
cout << a; //what will this output? } void fun(int& a){ a = 1
16
If statements Put a condition on a block of code
IF the condition is TRUE, the code will be executed if(x > 3){ cout << “Success!” << endl; } allows us to program decisions and choice
17
If Statements We can also include other options
The computer will evaluate each options condition until it finds one that is true once a successful condition is met, the IF statement is finished options are written else if a “catch-all” option is written else
18
if (grade < = 100 && grade > 79){ } else if (grade > 69) {
cout << “you got an A”; } else if (grade > 69) { cout << “you got a B”; else if (grade > 59){ cout << “you got a C”; else if (grade > 49) cout << “you got a D”; else{ cout << “you FAILED”; cout << “endl”;
19
While Loops Allows us to attach a condition to a block of code
The code will be executed over and over again, until the condition becomes false if the condition never becomes false, the code never stops if the condition is never true, the code will never be executed
20
While Loops //stores data, return false when bank is full
bool storeData(double in); … bool readData = true; while(readData == true){ cout << “enter your number: “ ; cin >> inputData; readData = storeData(inputData); }
21
Logical Expressions Evaluate to either true or false operators
>,>=, <, <=, !, == expressions can be combined && means both conditions are true || means either condition is true REMEMBER OUR TRUTH TABLE!?!?
22
Terms Keyword Parameter Identifier Expression literal Logic Expression
Types Syntax Comments Loop Condition Prototype Declaration Parameter Expression Logic Expression Variable Function Compiler Control Statement Operator Definition
23
Operators << >> > < >= <= == = && ! != ++ – +=
-= ( ) { } ||
24
Rules Identifiers Semi-colons Declaration Brackets
- letters, numbers, _ - can't start with number, or _ - should make sense Semi-colons - all expressions end with a ; Declaration - Functions and variables must be declared, before they are used Brackets - if you start a bracket, finish a bracket
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.