C++ course(eighth section)
eighth section(Function with parameters)
Function definition syntax:
void functionName(dataType& variable, dataType& variable, …)
{
statements
}
Value Parameter - a formal parameter that receives a copy of the content of the corresponding actual parameter
Reference Parameter - a formal parameter that receives the location (memory address) of the corresponding actual parameter.
Formal Parameters
Reference Parameter
They can pass one or more values from a function and can change the value of the actual parameter
Reference parameters are useful in three situations:
Returning more than one value
Changing the actual parameter
When passing the address would save memory space and time.
using namespace std;
void funOne(int a, int& b, char v);
void funTwo(int& x, int y, char& w);
int main()
{
int num1, num2;
Parameters & Memory Allocation
During execution, changes made by the formal parameter permanently change the value of the actual parameter
Stream variables (for example, ifstream and ofstream) should be passed by reference to a function.
Scope of an Identifier
the identifier is declared before the function definition (block)
the function name is different from the identifier
all parameters of the function have names different than the name of the identifier
all local identifiers (such as local variables) have names different than the name of the identifier.
Global VariablesSome compilers initialize global variables to default values
The operator :: is called the scope resolution operator
By using the scope resolution operator, a global variable declared before the definition of a function (block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the variable.
Global Variable Side Effects
Static and Automatic Variables
The statement
static int x;
declares x to be a static variable of the type int
Static variables declared within a block are local to the block
Their scope is the same as any other local identifier of that block.
Function Overloading
Default Parameters
All the default parameters must be the rightmost parameters of the function
In a function call where the function has more than one default parameter and a value to a default parameter is not specified, then you must omit all of the arguments to its right.
eighth section(Function with parameters)
Function definition syntax:
void functionName(dataType& variable, dataType& variable, …)
{
statements
}
Value Parameter - a formal parameter that receives a copy of the content of the corresponding actual parameter
Reference Parameter - a formal parameter that receives the location (memory address) of the corresponding actual parameter.
If a formal parameter is a value parameter, the value of the corresponding actual parameter is copied into it
The value parameter has its own copy of the data
During program execution, the value parameter manipulates the data stored in its own memory space.
The value parameter has its own copy of the data
During program execution, the value parameter manipulates the data stored in its own memory space.
If a formal parameter is a reference parameter, it receives the address of the corresponding actual parameter
A reference parameter stores the address of the corresponding actual parameter
During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter.
A reference parameter receives the address of the actual parameter A reference parameter stores the address of the corresponding actual parameter
During program execution to manipulate the data, the address stored in the reference parameter directs it to the memory space of the corresponding actual parameter.
They can pass one or more values from a function and can change the value of the actual parameter
Reference parameters are useful in three situations:
Returning more than one value
Changing the actual parameter
When passing the address would save memory space and time.
Programming example:
#include <iostream>using namespace std;
void funOne(int a, int& b, char v);
void funTwo(int& x, int y, char& w);
int main()
{
int num1, num2;
char ch;
num1 = 10; //Line 1
num2 = 15; //Line 2
ch = 'A'; //Line 3
cout<<"Line 4: Inside main: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 4
funOne(num1, num2, ch); //Line 5
cout<<"Line 6: After funOne: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 6
funTwo(num2, 25, ch); //Line 7
cout<<"Line 8: After funTwo: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 8
return 0;
num1 = 10; //Line 1
num2 = 15; //Line 2
ch = 'A'; //Line 3
cout<<"Line 4: Inside main: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 4
funOne(num1, num2, ch); //Line 5
cout<<"Line 6: After funOne: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 6
funTwo(num2, 25, ch); //Line 7
cout<<"Line 8: After funTwo: num1 = "<<num1
<<", num2 = "<<num2<<", and ch = "<<ch<<endl; //Line 8
return 0;
}
void funOne(int a, int& b, char v)
{ int one;
one = a; //Line 9
a++; //Line 10
b = b * 2; //Line 11
v = 'B'; //Line 12
cout<<"Line 13: Inside funOne: a = "<<a<<", b = "<<b
<<", v = "<<v<<", and one = "<<one<<endl; //Line 13
}
void funTwo(int& x, int y, char& w)
{ x++; //Line 14
y = y * 2; //Line 15
w = 'G'; //Line 16
cout<<"Line 17: Inside funTwo: x = "<<x
<<", y = "<<y <<", and w = "<<w<<endl; //Line 17
}
void funOne(int a, int& b, char v)
{ int one;
one = a; //Line 9
a++; //Line 10
b = b * 2; //Line 11
v = 'B'; //Line 12
cout<<"Line 13: Inside funOne: a = "<<a<<", b = "<<b
<<", v = "<<v<<", and one = "<<one<<endl; //Line 13
}
void funTwo(int& x, int y, char& w)
{ x++; //Line 14
y = y * 2; //Line 15
w = 'G'; //Line 16
cout<<"Line 17: Inside funTwo: x = "<<x
<<", y = "<<y <<", and w = "<<w<<endl; //Line 17
}
Output is:
Line 4: Inside main: num1=10, num2=15, and ch=A
Line 13: Inside funOne: a=11, b=30, v=B, and one=10
Line 6: After funOne: num1=10, num2=30, and ch=A
Line 17: Inside funTwo: x=31, y=50, and w = G
Line 8: After funTwo: num1=10, num2=31, and ch= G
When a function is called, memory for its formal parameters and variables declared in the body of the function (called local variables) is allocated in the function data area
In the case of a value parameter, the value of the actual parameter is copied into the memory cell of its corresponding formal parameter
In the case of a reference parameter, the address of the actual parameter passes to the formal parameter.
Content of the formal parameter is an address In the case of a value parameter, the value of the actual parameter is copied into the memory cell of its corresponding formal parameter
In the case of a reference parameter, the address of the actual parameter passes to the formal parameter.
During execution, changes made by the formal parameter permanently change the value of the actual parameter
Stream variables (for example, ifstream and ofstream) should be passed by reference to a function.
The scope of an identifier refers to where in the program an identifier is accessible (that is visible)
Local identifier - identifiers declared within a function (or block)
Global identifier – identifiers declared outside of every function definition
C++ does not allow the nesting of functions, this means that the definition of one function cannot be included in the body of another function.
Global identifiers (such as variables) are accessible by a function or a block if, Local identifier - identifiers declared within a function (or block)
Global identifier – identifiers declared outside of every function definition
C++ does not allow the nesting of functions, this means that the definition of one function cannot be included in the body of another function.
the identifier is declared before the function definition (block)
the function name is different from the identifier
all parameters of the function have names different than the name of the identifier
all local identifiers (such as local variables) have names different than the name of the identifier.
An identifier declared within a block (Nested Block) is accessible
only within the block from the point it is declared until the end of the block and
by those blocks that are nested within that block if the nested block does not have an identifier with the same name as that of the outside block (the block that encloses the nested block)
The scope of a function name is similar to the scope of an identifier declared outside of any block.
only within the block from the point it is declared until the end of the block and
by those blocks that are nested within that block if the nested block does not have an identifier with the same name as that of the outside block (the block that encloses the nested block)
The scope of a function name is similar to the scope of an identifier declared outside of any block.
The operator :: is called the scope resolution operator
By using the scope resolution operator, a global variable declared before the definition of a function (block) can be accessed by the function (or block) even if the function (or block) has an identifier with the same name as the variable.
C++ provides a way to access a global variable declared after the definition of a function
In this case, the function must not contain any identifier with the same name as the global variable.
In this case, the function must not contain any identifier with the same name as the global variable.
Using global variables has side effects
Any function that uses global variables is not independent and usually it cannot be used in more than one program
If more than one function uses the same global variable and something goes wrong, it is difficult to find what went wrong and where
Problems caused by global variables in one area of a program might be misunderstood as problems caused in another area.
Any function that uses global variables is not independent and usually it cannot be used in more than one program
If more than one function uses the same global variable and something goes wrong, it is difficult to find what went wrong and where
Problems caused by global variables in one area of a program might be misunderstood as problems caused in another area.
Automatic variable - a variable for which memory is allocated at block entry and deallocated at block exit
Static variable - a variable for which memory remains allocated as long as the program executes
Variables declared outside of any block are static variables
By default variables declared within a block are automatic variables
We can declare a static variable within a block by using the reserved word static.
Static variable - a variable for which memory remains allocated as long as the program executes
Variables declared outside of any block are static variables
By default variables declared within a block are automatic variables
We can declare a static variable within a block by using the reserved word static.
The syntax for declaring a static variable is:
static dataType identifier;The statement
static int x;
declares x to be a static variable of the type int
Static variables declared within a block are local to the block
Their scope is the same as any other local identifier of that block.
You can have several functions with the same name
Every function must have a different set of parameters
This is overloading a function name
The types of parameters determine which function to be executed
It is used when we have the same action for different sets of data
For it to work, the definition of each of the functions must be given.
Every function must have a different set of parameters
This is overloading a function name
The types of parameters determine which function to be executed
It is used when we have the same action for different sets of data
For it to work, the definition of each of the functions must be given.
When a function is called, the number of actual and formal parameters must be the same
C++ relaxes this condition for functions with default parameters
You specify the value of a default parameter when the function name appears for the first time, such as in the prototype.
If you do not specify the value of a default parameter, the default value is used C++ relaxes this condition for functions with default parameters
You specify the value of a default parameter when the function name appears for the first time, such as in the prototype.
All the default parameters must be the rightmost parameters of the function
In a function call where the function has more than one default parameter and a value to a default parameter is not specified, then you must omit all of the arguments to its right.
Default values can be constants, global variables, or function calls
The caller has the option of specifying a value other than the default for any default parameter
You cannot assign a constant value as a default value to a reference parameter.
The caller has the option of specifying a value other than the default for any default parameter
You cannot assign a constant value as a default value to a reference parameter.
0 comments:
Post a Comment