Функции

Функции


прога перегрузит процессор, оперативную память если будет бегать до функции, поэтому дописываем ключевое слово inline

inline работает только с примитивными функциями (без циклов и т.д.)

#include "stdafx.h"

#include <iostream>

using namespace std;

inline int summa(int x, int y)

{

   return x + y;

}

int main()

{

   setlocale(0, "");

   for (int i = 0; i < 10; i++)

      cout << summa(2, i) << endl;

   return 0;

}


define - предпроцессорная функция

#include "stdafx.h"

#include <iostream>

using namespace std;

int a = 1;

#define SUM(a, b) (a+b)

int main()

{

   setlocale(0, "");

   cout << SUM(10, 5) << endl;

   return 0;

}


#include "stdafx.h"

#include <iostream>

using namespace std;

int a = 1;

#define SUM(a, b) (a>0?a+b:a-b)

int main()

{

   setlocale(0, "");

   cout << SUM(10, 5) << endl;

   return 0;

}


#include "stdafx.h"

#include <iostream>

using namespace std;

int a = 1;

#define SUM(a) (a>0?a+a:a-a)

int main()

{

   setlocale(0, "");

   cout << SUM(10, 5) << endl;

   return 0;

}


математическая ошибка: из подстановки в выражение a*a получится 10+x*10+x:

#include "stdafx.h"

#include <iostream>

using namespace std;

int a = 1;

#define SUM(a) (a*a)

int main()

{

   setlocale(0, "");

   int x;

   cin >> x;

   cout << SUM(10+x) << endl;

   return 0;

}

работать будет так:

#include "stdafx.h"

#include <iostream>

using namespace std;

int a = 1;

#define SUM(a) ((a)*(a))

int main()

{

   setlocale(0, "");

   int x;

   cin >> x;

   cout << SUM(10+x) << endl;

   return 0;

}

для нахождения модуля числа:

#include "stdafx.h"

#include <iostream>

using namespace std;

int a = 1;

#define SUM(a) (a<0?a*-1:a)

int main()

{

   setlocale(0, "");

   int x;

   cin >> x;

   cout << SUM(x) << endl;

   return 0;

}


Перегрузка функций - одинаковое имя, разные аргументы:

#include "stdafx.h"

#include <iostream>

using namespace std;

int sum(int a, int b, int c)

{

   return a*b*c;

}

int sum(int a, int b)

{

   return a*b*b;

}

int sum(int a)

{

   return a*a*a;

}

int main()

{

   setlocale(0, "");

   cout << sum(2, 4, 1) << endl;

   return 0;

}

перегруженая функция существует в нескольких экземплярах (ошибка):

#include "stdafx.h"

#include <iostream>

using namespace std;

double sum(double a, int b)

{

   return a + b;

}

int sum(int a, double b)

{

   return a + b;

}

int main()

{

   setlocale(0, "");

   cout << sum(12, 5) << endl;

   return 0;

}


DZ

#include "stdafx.h"

#include <iostream>

#include <time.h>

using namespace std;


int func(char mas[5][5])

{

   int cout = 0;

   for (int i = 0; i < 5; i++)

      for (int j = 0; j < 5; j++)

      {

         if (mas[i][j] == 'a')

            cout++;

      }

   return cout;

}


int func(int mas[10])

{

   int cout = 0;

   for (int i = 0; i < 10; i++)

   {

      if (mas[i] == 0)

         cout++;

   }

   return cout;

}


int func(double mas[30])

{

   int cout = 0;

   for (int i = 0; i < 30; i++)

   {

      if (mas[i] == 0)

         cout++;

   }

   return cout;

}

int main()

{

   setlocale(0, "");

   srand(time(NULL));

   int imas[10];

   double dmas[30];

   char cmas[5][5];


   cout << "Массив символов: " << endl;

   for (int i = 0; i < 5; i++)

   {

      for (int j = 0; j < 5; j++)

      {

         cmas[i][j] = 'a' + rand() % 26;;

         cout << cmas[i][j] << " ";

      }

      cout << endl;

   }


   cout << "Массив целых чисел: ";

   for (int i = 0; i < 10; i++)

   {

      imas[i] = rand() % 5;

      cout << imas[i] << " ";

   }

   cout << endl;


   cout << "Массив дробных чисел: ";

   for (int i = 0; i < 30; i++)

   {

      dmas[i] = (double)rand() / 5000;

      cout << dmas[i] << " ";

   }


   cout << "\n\n";

   cout << "Букв \"а\" в первом массиве " << func(cmas) << endl;

   cout << "Нулей во втором массиве " << func(imas) << endl;

   cout << "Нулей в третьем массиве " << func(dmas) << endl;

   return 0;

}

Report Page