This is a Clilstore unit. You can link all words to dictionaries.

Functions

In language C++ all functions are divided into two categories:

User-defined functions;

Library functions that are stored in the compiler package of language C++. Let’s consider the first category.

It should be noted that the mechanism of operation with function issignificantly different from the implementation in Pascal. In C ++ only one type of subprograms is used (in contrast to Pascal), it is a - function. Generally,it is not accepted to use term "subprogram", because the function is the basic program unit in C ++, minimum executable module. Any program shall include the main function with name main (). If the program uses other functions, then they play the role of subprograms.

The structure of the standard definition of the function has the form:

typename_offunction (list of the formal parameters)

{

function body

}

where type- type of the return result by the function.

The important statement of the function body is statement of returning from function to the point of its call return. This statement can be used in two forms:

return; - do not return any value as its result.

return expression; - expression in the return statement specifies the value returned by the function.

Examples: return х;

Expression in the return statement specifies the value returned by the function – is thelist of names of formal parameters of function with indication of type for each of them.

Actual parameters are located in the main program.

The actual parameters are located in the main program.

If the variable is described inside any unit, then it will be localized (local) in this unit and from other units that are external to this unit, "will not be visible."

In the right place of the main program, a function call will occur.

The format of the function call:

name_offunction (list of actual parameters);

When calling the function, the matched condition between formal and actual parameters shall be observed, as in the sequence order and for data types.

Example 51. Find the greatest of three numbers using the function for searching of the greatest of two.

#include <iostream.h>

#include <conio.h>

// определение функции

int MAX( int x, int y)

{ if (x>y) return x; else return y;

}

// main function

main()

{int a,b,c,M;

cin>>a>>b>>c;

M=MAX(MAX(a,b),c);

cout<<" Maximum ="<<M;

getch();

}

In this program it is possible to use local variables.

Example 52.

int MAX( int x, int y)

{ int k;

if (x>y) k=x; else k=y;

return k;

}

main()

{int a, b, c, M1, M;

cin>>a>>b>>c;

M1=MAX(a,b);

M=MAX(M1,c);

cout<<" Maximum ="<<M;

}

Example 53. Using the function for finding of the greatest common divisor (GCD) of two integers, find the GCD of four natural numbers.

int NOD( int x, int y)

{ while (x!=y)

if (x>y) x=x-y; else y=y-x;

return x;

}

main()

{int a,b,c, d, M1, M2, M;

cin>>a>>b>>c>>d;

M1=EYOB(a, b);

M2=EYOB(c, d);

M=EYOB(M1, M2);

cout<<" 4 sannin EYOB- i= "<<M;

}

Example 54. Using the function for counting of the amount of three-digit number, display all numbers from 100 to 300, in which the sum of digits is equal to 5.

int Sum5( int x)

{ int a,b,c,d;

a=x/100;

b=x%100;

c=b/10;

d=b%10;

return a+c+d;

}

main()

{int i,k;

for (i=100; i<=300; i++)

{k=Sum5(i);

if (k==5) cout<<"\n i= " <<i; }

getch();

}

Example 55. Using the function for searching of prime numbers, find all prime numbers in range a, b.

int Jai_S( int x)

{ int i,k=0,S=0;

for (i=1; i<=x; i++)

if (x%i==0) k++;

if (k==2) S=1;

return S;

}

main()

{int a,b,j,t;

cout<<"Input the range a,b"; cin>>a>>b;

for (j=a; j<=b; j++)

{t=Jai_S(j);

if (t==1) cout<<"\n Prime number " <<j; }

}

Short url:   http://multidict.net/cs/6253