본문 바로가기

Programming/C++ 1

[C++] Chapter 6. Functions

반응형
  • 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)
#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

  • 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;
}
반응형