Lecture 01

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 103

Object Oriented

Programming
INSTRUCTOR: JAWAD RASHEED
Why take COM252?

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 2


Defining key terms Wing 2006
Digital Promise 2017

"Computational thinking is a “Computer science is an academic


problem solving process: ‘a discipline: ‘the study of computers
way of solving problems, and algorithmic processes,
designing systems, and including their principles, their
understanding human hardware
behavior that draws on and software designs, their
concepts fundamental to applications, and
computer science… a their impact
fundamental skill for on society’”
everyone,not just
computer scientists’”

"Coding is a technical skill: the


practice of developing a set of
instructions that a computer can
understand and execute.”

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 3


Defining key terms
● Coding as a technical skill

● Computer science as an academic discipline

● Computational thinking as a problem-solving process

CS education is more than just


COMMUNICATION
+
CRITICAL
THINKING

“learning how to code”! ENGLISH


LANGUAGE
ARTS
WRITING

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 4


Phases of language development Wells 1981

1. Discovery that language is a pattern of sounds that takes on meaning and purpose

2. Participation in everyday social aspects of language that enable an understanding


of encoded cultural values and assumptions

3. Ability to self-reflect on the use of language and to see language as a “tool for thinking” and
communicating thoughts, even when not actively speaking or interacting with others

the acquisition of literacy

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 5


What COM252 is
● A logical follow-up course to a computer programming – II and data structure class

● A course that will give you practice with computational thinking skills through basic C++
coding

● A survey of abstraction, encapsulation, inheritance and polymorphism regarding classes


and objects to prepare you for future exploration in computing and to build your
understanding of technology

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 6


What COM252 is not
● To teach you how to program from scratch

● A course that will teach you the specifics of the C++ language

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 7


What is OOP?

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 8


What is OOP?

Definition
object-oriented programming
A programming paradigm based on the
concept of “objects” that contains data
(attributes/properties) and code
(procedures/methods).

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 9


OOP: Abstraction

Definition
abstraction
Design that hides the details of how
something works while still allowing the
user to access complex functionality.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 10


What is OOP?
● Another example: Programming languages are abstractions through which we communicate
with computers.

● Key idea: Through a simpler interface, users are able to take full advantage of a complex
system without needing to know how it works or how it was made.

● People are important part of defining classes/abstractions and defining the boundary between
usage and implementation (i.e. What should that simpler interface look like?)

● COM252 focuses on the design and/or use of classes and objects in computer science.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 11


What is COM252?
(THE NUTS & BOLTS)

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 12


What the abstraction looks like

client / user side Implementation side


(how the abstraction is (how the abstraction
used) works)

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 13


OOP: Classes

arrays,
Abstract data structures dynamic memory
(vectors, maps, etc.) management,
linked data structures

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 14


OOP: Classes

arrays,
Abstract data structures dynamic memory
(vectors, maps, etc.) management,
linked data structures

How to use abstractions created by


others (C++ libraries)

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 15


OOP: Classes
How to write abstractions for
others to use

arrays,
Abstract data structures dynamic memory
(vectors, maps, etc.) management,
linked data structures

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 16


OOP: Classes

arrays,
Abstract data structures dynamic memory
(vectors, maps, etc.) management,
linked data structures

How lower-level abstractions are used to


implement higher-level abstractions

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 17


Learning goals

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 18


Learning goals
● I am excited to use programming to solve real-world problems I encounter outside class.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 19


Learning goals
● I am excited to use programming to solve real-world problems I encounter outside class.
● I recognize and understand common basic class designs and implementation in computer science.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 20


Learning goals
● I am excited to use programming to solve real-world problems I encounter outside class.
● I recognize and understand common basic class designs and implementation in computer science.
● I can identify programmatic concepts present in everyday technologies because I understand how
computers process and organize information.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 21


Learning goals
● I am excited to use programming to solve real-world problems I encounter outside class.
● I recognize and understand common basic class designs and implementation in computer science.
● I can identify programmatic concepts present in everyday technologies because I understand how
computers process and organize information.
● I can break down complex problems into smaller sub problems by applying object-oriented design,
algorithmic reasoning and recursive problem-solving skills.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 22


Learning goals
● I am excited to use programming to solve real-world problems I encounter outside class.
● I recognize and understand common basic class designs and implementation in computer science.
● I can identify programmatic concepts present in everyday technologies because I understand how
computers process and organize information.
● I can break down complex problems into smaller sub problems by applying object-oriented design,
algorithmic reasoning and recursive problem-solving skills.
● I can evaluate design tradeoffs when creating objects and algorithms or utilizing them to implement
technological solutions.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 23


Learning goals
● I am excited to use programming to solve real-world problems I encounter outside class.
● I recognize and understand common basic class designs and implementation in computer science.
● I can identify programmatic concepts present in everyday technologies because I understand how
computers process and organize information.
● I can break down complex problems into smaller sub problems by applying object-oriented design,
algorithmic reasoning and recursive problem-solving skills.
● I can evaluate design tradeoffs when creating objects and algorithms or utilizing them to implement
technological solutions.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 24


Why C++?

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 25


How C++ different from other languages?

● C++ is a compiled language (vs. interpreted)


○ This means that before running a C++ program, you must first compile it to machine code.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 26


How C++ different from other languages?

● C++ is a compiled language (vs. interpreted)

● C++ is gives us access to lower-level computing resources (e.g. more direct control over
computer memory)
○ This makes it a great tool for better understanding abstractions!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 27


How C++ different from other languages?

● C++ is a compiled language (vs. interpreted)

● C++ is gives us access to lower-level computing resources (e.g. more direct control over
computer memory)

● If you’re coming from a language like Java/Python, the syntax will take some getting used to.
○ Like learning the grammar and rules of a new language, typos are expected. But
don’t let this get in the way of working toward literacy!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 28


How C++ different from other languages?

● C++ is a compiled language (vs. interpreted)

● C++ is gives us access to lower-level computing resources (e.g. more direct control over
computer memory)

● If you’re coming from a language like Java/Python, the syntax will take some getting used to.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 29


C++ ranking!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 30


TIOBE Programming Community Index
Where does C++ rank among the popular programming languages of the world?

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 31


TIOBE Programming Community Index
Where does C++ rank among the popular programming languages of the world?

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 32


C++ Overview
Inventor of C++: Bjarne Stroustrup

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 33


C++ History

● C++ is a high-performance, robust (and complex) language built on top of the C programming
language (originally named C with Classes)
○ Bjarne Stroustrup, the inventor of C++, chose to build on top of C because it was fast, powerful, and widely-used

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 34


C++ History

● C++ is a high-performance, robust (and complex) language built on top of the C programming
language (originally named C with Classes)
○ Bjarne Stroustrup, the inventor of C++, chose to build on top of C because it was fast, powerful, and widely-used
● C++ has been an object-oriented language from the beginning
○ We will spend the middle portion of this class talking about the paradigm of object-oriented programming

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 35


C++ History

● C++ is a high-performance, robust (and complex) language built on top of the C programming
language (originally named C with Classes)
○ Bjarne Stroustrup, the inventor of C++, chose to build on top of C because it was fast, powerful, and widely-used
● C++ has been an object-oriented language from the beginning
○ We will spend the middle portion of this class talking about the paradigm of object-oriented programming
● C++ is quite mature and has become complex enough that it is challenging to master the
language
○ Our goal in this class will be to help you become literate in C++ as a second programming language

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 36


C++ History

● C++ is a high-performance, robust (and complex) language built on top of the C programming
language (originally named C with Classes)
○ Bjarne Stroustrup, the inventor of C++, chose to build on top of C because it was fast, powerful, and widely-used
● C++ has been an object-oriented language from the beginning
○ We will spend the middle portion of this class talking about the paradigm of object-oriented programming
● C++ is quite mature and has become complex enough that it is challenging to master the
language
○ Our goal in this class will be to help you become literate in C++ as a second programming language

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 37


C++ Benefits and Drawbacks
Benefits

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 38


C++ Benefits and Drawbacks
Benefits
● C++ is fast
○ Get ready for the Python vs C++ speed
showdown during Assignment!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 39


C++ Benefits and Drawbacks
Benefits
● C++ is fast
○ Get ready for the Python vs C++ speed
showdown during Assignment!
● C++ is popular
○ Many companies and research projects use C++
and it is common for coding interviews to be
conducted in C++

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 40


C++ Benefits and Drawbacks
Benefits
● C++ is fast
○ Get ready for the Python vs C++ speed
showdown during Assignment!
● C++ is popular
○ Many companies and research projects use C++
and it is common for coding interviews to be
conducted in C++
● C++ is powerful
○ C++ brings you closer to the raw computing
power that your computer has to offer

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 41


C++ Benefits and Drawbacks
Benefits Drawbacks
● C++ is fast
○ Get ready for the Python vs C++ speed
showdown during Assignment!
● C++ is popular
○ Many companies and research projects use C++
and it is common for coding interviews to be
conducted in C++
● C++ is powerful
○ C++ brings you closer to the raw computing
power that your computer has to offer

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 42


C++ Benefits and Drawbacks
Benefits Drawbacks
● C++ is fast ● C++ is complex
○ Get ready for the Python vs C++ speed ○ We will rely on the basic C++ libraries to
showdown during Assignment! provide a friendlier level of abstraction
● C++ is popular ○ In the future,you may choose to explore the
○ Many companies and research projects use C++ standard libraries
and it is common for coding interviews to be
conducted in C++
● C++ is powerful
○ C++ brings you closer to the raw computing
power that your computer has to offer

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 43


C++ Benefits and Drawbacks
Benefits Drawbacks
● C++ is fast ● C++ is complex
○ Get ready for the Python vs C++ speed ○ We will rely on the basic C++ libraries to
showdown during Assignment! provide a friendlier level of abstraction
● C++ is popular ○ In the future,you may choose to explore the
○ Many companies and research projects use C++ standard libraries
and it is common for coding interviews to be
● C++ can be dangerous
conducted in C++
○ "With great power comes great responsibility"
● C++ is powerful
○ C++ brings you closer to the raw computing
power that your computer has to offer

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 44


Demo program!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 45


The structure of a program
C++ Python
#include "console.h" import sys
using namespace std;

// The C++ compiler will look for a function # This function does not need to be called “main”
// called “main” def main():
int main() { print('Hello, world!')
cout << "Hello, world!" << endl;
return 0; // must return an int to indicate
// successful program completion if name == ' main ':
} # Any function that gets placed here will get
# called when you run the program with
# `python3 helloworld.py`
main()

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 46


Core programming
fundamentals in C++

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 47


Core programming
fundamentals in C++
Get ready for a whirlwind tour !

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 48


Core
programming Comments, Includes,
fundamentals And Console Output
in C++

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 49


Comments

● Single-line comments

// Two forward slashes comment out the rest of the line

cout << "Hello, World!" << endl; // everything past the double-slash i s a comment

● Multi-line comments

/* This i s a multi-line comment.

* I t begins and ends with an asterisk-slash.

*/

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 50


Includes

● Utilizing code written by other programmers is one of the most powerful things that you can do
when writing code.
● In order to make the compiler aware of other code libraries or other code files that you want to
use, you must include a header file. There are two ways that you can do so:
○ #include <iostream>
■ Use of the angle bracket operators is usually reserved for code from the C++ library
○ #include "console.h"
■ Use of the quotes is usually reserved for code from the C++ libraries, or code in files that you have written
yourself

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 51


Console Output
● The console is the main venue that we will use in this class to communicate information from a
program to the user of the program.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 52


Console Output
● The console is the main venue that we will use in this class to communicate information from a
program to the user of the program.
● In C++, the way that you get information to the console is by using the cout
keyword and angle bracket operators (<<).

cout << "The answer to life, the universe, and everything is " << 42 << "." << endl;

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 53


Console Output
● The console is the main venue that we will use in this class to communicate information from a
program to the user of the program.
● In C++, the way that you get information to the console is by using the cout
keyword and angle bracket operators (<<).
● The endl is necessary to put the cursor on a different line. Here is an example with and without
the endl keyword.

cout << "This is some text followed by endl." << endl;


cout << "This is more text.";
cout << "We want to go to the next line here, too" << endl;
cout << "We made it to the next line." << endl;

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 54


Console Output
● The console is the main venue that we will use in this class to communicate information from a
program to the user of the program.
● In C++, the way that you get information to the console is by using the cout
keyword and angle bracket operators (<<).
● The endl is necessary to put the cursor on a different line. Here is an example with and without
the endl keyword.

cout << "This is some text followed by endl." << endl;


cout << "This is more text.";
cout << "We want to go to the next line here, too" << endl;
cout << "We made it to the next line." << endl;

Note: In C++, all programming statements must end in a semicolon.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 55


Core
programming Variable and Types
fundamentals
in C++

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 56


Variables
● A way for code to store information by associating a value with a name

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 57


Variables
● A way for code to store information by associating a value with a name

252 classCode

94.7 mondayTemp

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 58


Variables
● A way for code to store information by associating a value with a name

We will think of
a variable as a 252 classCode
named
container 94.7 mondayTemp
storing a value.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 59


Variables
● A way for code to store information by associating a value with a name

Note: C++
uses the 252 classCode
camelCase
naming 94.7 mondayTemp
convention

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 60


Variables
● A way for code to store information by associating a value with a name.
● Variables are perhaps one of the most fundamental aspects of programming! Without
variables, the expressive power of our computer programs would be severely degraded.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 61


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 62


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.
● Examples of types in C++
○ int

93
252
-5

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 63


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.
● Examples of types in C++
○ int
○ double
3.06
6.00
-88.7545545

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 64


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.
● Examples of types in C++
○ int
○ double
○ string
"Hello, World!"

"COM252"

"I student of
computer
engineering <3"

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 65


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.
● Examples of types in C++
○ int
○ double
○ string

'c' '8'
○ char

'&'

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 66


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.
● Examples of types in C++
○ int
○ double
○ string
○ char
● In C++, all types must be explicitly defined when the variable is created, and a variable
cannot change its type.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 67


Types
● As you should know from prior programming classes, all variables have a type associated with
them, where the type describes the representation of the variable.
● Examples of types in C++
○ int
○ double
○ string
○ char
● In C++, all types must be explicitly defined when the variable is created, and a variable
cannot change its type.

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 68


Typed Variables
int a; // declare a new integer variable

int
a

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 69


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

int
a

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 70


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

char
5 'x'

int
char c = 'x'; // c is a char ("character")

a c

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 71


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

char
5 'x'

int
char c = 'x'; // c is a char ("character")

double d = 1.06; // d is a double, a type


used to represent decimal numbers
a c

double
1.06

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 72


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

char
5 'x'

int
char c = 'x'; // c is a char ("character")

double d = 1.06; // d is a double, a type


used to represent decimal numbers
a c
string s = "this is a C++ string";

double

string
"this is a
1.06 C++ string"

d s

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 73


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

char
5 'x'

int
char c = 'x'; // c is a char ("character")

double d = 1.06; // d is a double, a type


used to represent decimal numbers
a c
string s = "this is a C++ string";

double a = 4.2; // ERROR! You cannot

double
redefine a variable to be another type

string
"this is a
1.06 C++ string"

d s

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 74


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

char
5 'x'

int
char c = 'x'; // c is a char ("character")

double d = 1.06; // d is a double, a type


used to represent decimal numbers
a c
string s = "this is a C++ string";

double a = 4.2; // ERROR! You cannot

double
redefine a variable to be another type

string
"this is a
int a = 12; // ERROR! You do not need the
1.06 C++ string"

s
type when re-assigning a variable
d

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 75


Typed Variables
int a; // declare a new integer variable

a = 5; // initialize the variable value

char
12 'x'

int
char c = 'x'; // c is a char ("character")

double d = 1.06; // d is a double, a type


used to represent decimal numbers
a c
string s = "this is a C++ string";

double a = 4.2; // ERROR! You cannot

double
redefine a variable to be another type

string
"this is a
int a = 12; // ERROR! You do not need the
1.06 C++ string"

s
type when re-assigning a variable

a = 12; // this is okay, updates variable value


d

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 76


Core Functions
programming &
fundamentals
in C++ Parameters

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 77


Anatomy of function

input function(input) output

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 78


Anatomy of function

input function(input) output

parameters
/ arguments

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 79


Anatomy of function

input function(input) output

Definition
parameter(s)
parameters One or more variables that your
/ arguments function expects as input

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 80


Anatomy of function

input function(input) output

Definition
argument(s)
parameters The values passed into your
/ arguments function and assigned to its
parameter variables

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 81


Anatomy of function

input function(input) output

Definition
return value
The value that your function return value
hands back to the “calling”
function

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 82


Anatomy of function

input function(input) output

parameters return value


/ arguments

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 83


Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

function
prototype

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 84


Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

function name

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 85


Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

input
expected
(parameters)

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 86


Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

Notice that these look very


similar to variable
input
declarations! You can
expected
think of parameters as a
(parameters)
special set of local variables
that belong to a function.
February 23, 2022 INSTRUCTOR: JAWAD RASHEED 87
Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

output
expected
(return type)

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 88


Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

How do you designate a


function that doesn't return
output a value? You can use the
expected special void keyword. Note
(return type) that this type is only
applicable for return types,
not parameters/variables.
February 23, 2022 INSTRUCTOR: JAWAD RASHEED 89
Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

returnType functionName(varType parameter1, varType parameter2, ...) {


returnType variable = /* Some fancy code. */
/* Some more code to actually do things. */
return variable;

function
definition

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 90


Anatomy of function
returnType functionName(varType parameter1, varType parameter2, ...);

returnType functionName(varType parameter1, varType parameter2, ...) {


returnType variable = /* Some fancy code. */
/* Some more code to actually do things. */
return variable;

returned value

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 91


Function example
double average(double a, double b){
double sum = a + b;
return sum / 2;
}

int main(){
double mid = average(10.6, 7.2);
cout << mid << endl;
return 0;
}

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 92


Function example
double average(double a, double b){

Order matters!
double sum = a + b;
return sum / 2;
} A function
must always be
int main(){ defined before it
double mid = average(10.6, 7.2);
cout << mid << endl; is called.
return 0;
}

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 93


Function example
double average(double a, double b){
double sum = a + b;
return sum / 2; callee
(called function)
}

int main(){
double mid = average(10.6, 7.2);
cout << mid << endl;
return 0;
caller
} (calling function)

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 94


Function example
double average(double a, double b){
double sum = a + b;
return sum / 2; parameters
}
return value
int main(){
double mid = average(10.6, 7.2);
cout << mid << endl;
return 0; arguments
}

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 95


Function example
double average(double a, double b){
double sum = a + b;

double
return sum / 2; 10.6 a
}

double
7.2 b
int main(){
double mid = average(10.6, 7.2);
cout << mid << endl;
sum

double
return 0; 17.8
}

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 96


Function example
double average(double a, double b){
double sum = a + b;

double
return sum / 2; 10.6 a
}

double
7.2 b
int main(){
double mid = average(10.6, 7.2);
cout << mid << endl;
sum

double
return 0; 17.8
} These variables only
exist inside average()!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 97


Function example
double average(double a, double b){
double sum = a + b;
return sum / 2;
}

double
8.9 mid
int main(){
double mid = average(10.6, 7.2);
cout << mid << endl;
return 0;
}

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 98


Function example
double average(double a, double b){
double sum = a + b;
return sum / 2;
}

double
8.9 mid
int main(){
double mid = average(10.6, 7.2);

This variable only


cout << mid << endl;
return 0;
} exists inside main()!

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 99


Pass by value
// C++: #include<iostream>
using namespace std;
int doubleValue(int x) {
x *= 2;
◦ return x;

What is the console output


◦ }

of this block of code?


int main() {
int myValue = 5;
int result = doubleValue(myValue);
cout << "myValue: " << myValue << " ";
cout << "result: " << result << endl;
}
February 23, 2022 INSTRUCTOR: JAWAD RASHEED 100
Pass by value
// C++: #include<iostream>
using namespace std;
int doubleValue(int x) {
x *= 2;
◦ return x;
◦ }

int main() {
int myValue = 5;
int result = doubleValue(myValue); myValue: 5 result: 10
cout << "myValue: " << myValue << " ";

Why is this the case?


cout << "result: " << result << endl;
}
February 23, 2022 INSTRUCTOR: JAWAD RASHEED 101
Pass by value
// C++: #include<iostream>
using namespace std;
● The reason for the output is that the parameter x
int doubleValue(int x) { was passed to the doubleValue function by value,
x *= 2; meaning that the variable x is a copy of the variable
◦ return x; passed in. Changing it inside the function does not
change the value in the calling function.
◦ }
● Pass-by-value is the default mode of operation when
it comes to parameters in C++
int main() {
● C++ also supports a different, more nuanced way of
int myValue = 5; passing parameters – we will see this in coming
int result = doubleValue(myValue); lectures!
cout << "myValue: " << myValue << " ";
cout << "result: " << result << endl;
}
February 23, 2022 INSTRUCTOR: JAWAD RASHEED 102
For queries: jawadrasheed@aydin.edu.tr

February 23, 2022 INSTRUCTOR: JAWAD RASHEED 103

You might also like