728x90
반응형
- funciton signature
- function overloading
- pass by value
- pass by reference
- pass by pointer
- function signature
- funciton name
- number of parameters
- type of parameters
- function overloading
- same name but not same number of parameters or type of parameters
- return type and pas-by-ref are not part of Function signature
- function
- Define a function
- function name
- number of parameters
- type of parameters
- Invoke a function
- functionname(parameters)
- Define a function
#include <iostream>
using namespace std;
//void max(int a, int b, int m);
void max(int a, int b, int& m);
void mulplus(int x, int y, int& mul, int& sum);
int main()
{
int num = 0;
max(5, 7, num); //argument num ha sassociationwith reference var mn
cout << "max : " << num << endl;
int target = 20;
int& ref = target; // Declaration and init must be in one statement
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
cout << "address of ref is " << &ref << endl;
cout << "address of target is " << &target << endl;
ref = 100;
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
int a = 5, b = 55, m = 0, s = 0;
mulplus(5, 7, m, s);
cout << "multiplication value : " << m << endl;
cout << "sum value : " << s << endl;
return 0;
}
void max(int a, int b, int& m)
{
if (a > b)
{
m = a;
}
else
{
m = b;
}
}
void mulplus(int x, int y, int& mul, int& sum)
{
mul = x * y;
sum = x + y;
cout << "a*b= " << mul << " a+b= " << sum << endl;
}
- Scope of Variables
- A local variable
- defined inside a function
- scope
- the part of the program where the variable can be referenced
- A local variable
- pass by value [값에 의한 전달]
- the argument variable is not affected, regardless of the changes made to the parameter inside the function.
#include <iostream>
using namespace std;
void max(int a, int b, int m);
int main_value()
{
int num=0;
max(5, 7, num);
cout << "max : " << num << endl;
int target = 20;
int& ref = target; // Declaration and init must be in one statement
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
cout << "address of ref is " << &ref << endl;
cout << "address of target is " << &target << endl;
ref = 100;
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
return 0;
}
void max(int a, int b, int m)
{
if (a > b)
{
m = a;
}
else
{
m = b;
}
}
- Reference Vars [참조자]
#include <iostream>
using namespace std;
int main()
{
int target = 20;
int& ref = target; // Declaration and init must be in one statement
cout << "ref " << ref<< endl;
cout << "target is " << target << endl; //share memory
cout << "address of ref is " << &ref << endl;
cout << "address of target is " << &target << endl;
ref = 100;
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
return 0;
}
-> initialized at the same time as they are declared
- pass by reference
- pass a regular variable to invoke the function
- the parameter becomes an alieas for the original variable
#include <iostream>
using namespace std;
//void max(int a, int b, int m);
void max(int a, int b, int& m);
void mulplus(int a, int b, int & m,int& p);
int main()
{
int num = 0;
max(5, 7, num); //argument num ha sassociationwith reference var mn
cout << "max : " << num << endl;
int target = 20;
int& ref = target; // Declaration and init must be in one statement
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
cout << "address of ref is " << &ref << endl;
cout << "address of target is " << &target << endl;
ref = 100;
cout << "ref " << ref << endl;
cout << "target is " << target << endl; //share memory
int mul=0;
int plus = 0;
mulplus(5, 7,mul,plus);
return 0;
}
void max(int a, int b, int &m)
{
if (a > b)
{
m = a;
}
else
{
m = b;
}
}
void mulplus(int a, int b, int& mul, int &plus)
{
mul = a*b;
plus = a+b;
cout << "a*b= "<< mul << " a+b= " << plus << endl;
}
728x90
반응형
'Programming > C++ 1' 카테고리의 다른 글
[C++] Swap Function (0) | 2021.04.04 |
---|---|
[C++] Chapater 7. Single-Dimensional Arrays and C-Strings (0) | 2021.04.04 |
[C++] Chapter 2. Elementary Programming (0) | 2021.04.04 |
[C++] Chapter1. Introduction to Computers, Programs, and C++ (0) | 2021.04.04 |
C++ 공부하기 1 (0) | 2021.03.27 |