OOP Lecture 2
OOP Lecture 2
Example Explained
•myFunction() is the name of the function
•void means that the function does not have a return value. You will learn more about return values later in the next chapter
•inside the function (the body), add code that defines what the function should do
Declared functions are not executed immediately. They are "saved
for later use", and will be executed later, when they are called.
To call a function, write the function's name followed by two
parentheses and a semicolon.
Task 1:
Write a code for calculator that can preform some basic arithmetic’s
i.e. +,-,*,/
A C++ function consist of two parts:
•Declaration: the return type, the name of the function, and
parameters (if any)
•Definition: the body of the function (code to be executed)
If a user-defined function, such as myFunction() is declared after the main() function, an error will occur:
What is the difference between call by value and call by reference?
POINTERS
Pointers are symbolic representations of addresses. They enable programs to
simulate call-by-reference as well as to create and manipulate dynamic data
structures. Iterating over elements in arrays or other data structures is one of the
main use of pointers.
The address of the variable you’re working with is assigned to the pointer variable
that points to the same data type (such as an int or string).
Syntax:
datatype *var_name; int *ptr; /
How to use a pointer?
•Define a pointer variable
•Assigning the address of a variable to a pointer using the unary operator (&) which
returns the address of that variable.
•Accessing the value stored in the address using unary operator (*) which returns the
value of the variable located at the address specified by its operand.
The reason we associate data type with a pointer is that it knows how many bytes
the data is stored in. When we increment a pointer, we increase the pointer by the
size of the data type to which it points.
#include <bits/stdc++.h>
using namespace std;
void Fun()
{
int var = 20;