Review Part V
Local Variables
Variables that are declared within the body of a function definition are
said to be local to that function or to have that function as their scope.
Variables that are declared within the main part of the program are said to
be local to the main part of the program or to have the main part of the
program as their scope. When we say that a variable is a local variable
without any mention of a function and without any mention of the main
part of the program, we mean that the variable is local to some function
definition. If a variable is local to a function, then you can have another
variable with the same name that is declared in the main part of the
program or in another function definition, and these will be two different
variables, even though they have the same name.
Overloading a Function Name
If you have two or more function definitions for the same function name,
that is called overloading. When you overload a function name, the
function definitions must have different numbers of formal parameters or
some formal parameters of different types. When there is a function call,
the compiler uses the function definition whose number of formal
parameters and types of formal parameters match the arguments in the
function call.
Call-by-Reference
To make a formal parameter a call-by-reference parameter, append the
ampersand sign & to its type name. The corresponding argument in a call
to the function should then be a variable, not a constant or other
expression.
When the function is called, the corresponding variable argument
(not its value) will be substituted for the formal parameter. Any
change made to the formal parameter in the function body will be made
to the argument variable when the function is called. The exact details of
the substitution mechanisms are given in the text of this chapter.
EXAMPLE
(OF CALL-BY-REFERENCE PARAMETERS IN A FUNCTION
DECLARATION):
void get_data(int& first_in, double& second_in);
Both void functions and functions that return a value can have return
statements. In the case of a function that returns a value, the return statement
specifies the value returned. In the case of a void function, the return
statement simply ends the function call.
Navid Plus is a Tech blog focusing on Science material that I learn day through my College and job. Most likely, blog posts contain C++ Programming, Security, Networking, and Math. Let's have some fun.
Showing posts with label Reference. Show all posts
Showing posts with label Reference. Show all posts
Wednesday, March 22, 2017
Tuesday, March 21, 2017
C++ Part 34
The call by reference method of passing arguments to a
function copies the reference of an argument into the formal parameter.
Inside the function, the reference is used to access the actual argument
used in the call. This means that changes made to the parameter affect
the passed argument.
To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.
To pass the value by reference, argument reference is passed to the functions just like any other value. So accordingly you need to declare the function parameters as reference types as in the following function swap(), which exchanges the values of the two integer variables pointed to by its arguments.
// function definition to swap the values. void swap(int &x, int &y) { int temp; temp = x; /* save the value at address x */ x = y; /* put y into x */ y = temp; /* put x into y */ return; }For now, let us call the function swap() by passing values by reference as in the following example:
#include <iostream> using namespace std; // function declaration void swap(int &x, int &y); int main () { // local variable declaration: int a = 100; int b = 200; cout << "Before swap, value of a :" << a << endl; cout << "Before swap, value of b :" << b << endl; /* calling a function to swap the values using variable reference.*/ swap(a, b); cout << "After swap, value of a :" << a << endl; cout << "After swap, value of b :" << b << endl; return 0; }When the above code is put together in a file, compiled and executed, it produces the following result:
Before swap, value of a :100 Before swap, value of b :200 After swap, value of a :200 After swap, value of b :100
Sunday, March 19, 2017
C++ Part 33
ASCII - Binary Character Table
Letter
|
ASCII Code
|
Binary
|
Letter
|
ASCII Code
|
Binary
|
a
|
097
|
01100001
|
A
|
065
|
01000001
|
b
|
098
|
01100010
|
B
|
066
|
01000010
|
c
|
099
|
01100011
|
C
|
067
|
01000011
|
d
|
100
|
01100100
|
D
|
068
|
01000100
|
e
|
101
|
01100101
|
E
|
069
|
01000101
|
f
|
102
|
01100110
|
F
|
070
|
01000110
|
g
|
103
|
01100111
|
G
|
071
|
01000111
|
h
|
104
|
01101000
|
H
|
072
|
01001000
|
i
|
105
|
01101001
|
I
|
073
|
01001001
|
j
|
106
|
01101010
|
J
|
074
|
01001010
|
k
|
107
|
01101011
|
K
|
075
|
01001011
|
l
|
108
|
01101100
|
L
|
076
|
01001100
|
m
|
109
|
01101101
|
M
|
077
|
01001101
|
n
|
110
|
01101110
|
N
|
078
|
01001110
|
o
|
111
|
01101111
|
O
|
079
|
01001111
|
p
|
112
|
01110000
|
P
|
080
|
01010000
|
q
|
113
|
01110001
|
Q
|
081
|
01010001
|
r
|
114
|
01110010
|
R
|
082
|
01010010
|
s
|
115
|
01110011
|
S
|
083
|
01010011
|
t
|
116
|
01110100
|
T
|
084
|
01010100
|
u
|
117
|
01110101
|
U
|
085
|
01010101
|
v
|
118
|
01110110
|
V
|
086
|
01010110
|
w
|
119
|
01110111
|
W
|
087
|
01010111
|
x
|
120
|
01111000
|
X
|
088
|
01011000
|
y
|
121
|
01111001
|
Y
|
089
|
01011001
|
z
|
122
|
01111010
|
Z
|
090
|
01011010
|
Subscribe to:
Posts (Atom)
Digital Design Part 3
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...
-
4th→ assembler translates it to the machine language. 1.6 [20] <§1.6> Consider two different implementations of the same instru...
-
An array is used to process a collection of data all of which is of the same type, such as a list of numbers or a list of names. An array...