Presentation is loading. Please wait.

Presentation is loading. Please wait.

Http://ioerc.mk Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class.

Similar presentations


Presentation on theme: "Http://ioerc.mk Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class."— Presentation transcript:

1 Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class

2 Author: Blagorodna Lazova Subject: Programming Languages for III year Topic: Functions in C ++: Level:High school Language: Macedonian Language Type of material: Exercises-oriented functions in C++ Material Format :Presentation

3 Abstract: The realization of the function podalgoritmi in programming languages is carried out by sub-function. Function subprograms are called short features. This material covered is a brief explanation of the functions, types of functions and their definition and declaration in C ++. All this can analyze it through appropriate resolved examples of tasks where they used customized and integrated functions.

4 - Example 2 user function;
Contents: Types of functions, declaration of multi-functional prototype; Definition of functions; - Example 1 for customized function - Example 2 user function; Embedded mathematical functions library; -Programme that illustrate the functions sin i cos? Other solved problems, examples of functions

5 1. Function The tipe of function Function subprograms are called functions and can be: - embedded (functions found in programming libraries) - custom functions Declaration of the functions performed by the order, which takes the following form: Type Name (list of formal arguments) This command is called the function prototype. type-value type that restores function name- function name list of formal arguments Declaration ends with;

6 2. Definition of functions The general form of function definition in C ++ is:       type name (a list of formal arguments)       {                body function                return feedback value;       } Funkciite declare the beginning of the program to be able to call in the main program. Variables used in the function are called local variables, and those used in the program are called global variables.

7 Example of user functions:
- int Mnozi (int m, int n); -name Mnozi - formal arguments m и n - type rezultat- int float koren(float x) - type rezultat- float int Pomal(int ,int) - list without names arguments int g() list without names arguments int h(void) function list without arguments int f(int a,b) - wrong, the right is int f(int a,int b)

8 Ex 1. Collection of two numbers with function
#include<iostream> using namespace int suma (int n, int m) { return n+m; } int main() int a,b; cout<<“Enter a :”; cin>>a; cout<<“Enter b :”; cin>>b; cout<<“The sum of the numbers is“<<suma(a,b); return 0;

9 Ex.2 Write a function to calculate the largest of three preset numbers :
#include<iostreаm> using namespace int maximum (int x, int y, int z); // function prototype int main() { int number1; int number2; int number3; int most; cout<<“Enter three integers:”; cin>> number1 >> number2>> number3; /*number1, number2 and number3 are arguments of a function call to function maximum most =maximum(number1, number2, number3) cout<<“ The Maksimum is :<<most<<endl; return 0; } /*Definition of function maximum, x, y and z are arguments*/ int maximum (int x, int y, int z) int max=x; // We assume that x is max if (y > max ) // If y is greater than max max=y; //then y e max if (z > max ) //If z is greater than od max, max=z; // then z is max return max; // max is most

10 Functions of the mathematical library in C ++
sqrt (x) - the square root of x exp (a) - exponential function eª log (x) - natural logarithm of x log10 (x) - decade logarithm of x fabs (x) - absolute value of x ceil (x) - x completion of the smallest integer not less than x floor (x) - rounding of x largest integer not greater than x pow (x, y) - x y grade fmod (x, y) - residue of x y as real number sin (x) - sine of x cos (x) - cosine of x tan (x) - tangent of x

11 Example 1 program to illustrate the functions sin i cos
#include<iostream> #include<cmath> // for this purpose include the mathematical library cmath using namespace std; const double Pi= ; main() { float angle_in_degrees, angle_in_radians;  cout<<“Enter angle in degrees"<<endl;  cin>> angle_in_degrees; angle_in_radians =(angle_in_degrees *Pi)/180; cout<<" Sine of of the input angle is :"<<endl; cout<<"sin("<<agol_vo_stepeni<<")="<<sin(angle_in_radians)<<endl; cout<<" Cosine of the input angle is :"<<endl;  cout<<"cos("<<angle_in_degrees<<")="<<cos(angle_in_radians)<<endl;   return 0; }

12 Examples tasks for functions:
Conversion of Farenhajt to Celsius degree #include <iostream> using namespace std; float Converse(float); float TempFar; //global variable int main() { float TempCel; //local variable cout<<“Enter the temperature in Farenhajt:”; cin>>TampFar; TempCel=Converse (TempFar) cout<<“The temperature in Celsius degree is“; cout<<TempCel<<endl; return 0; } float Converse (float TempFar) float TempC; TempC=( (TempFar-32)*5)/9 return TempC;

13 Examples tasks for functions :
2. To find the sum of the first to feature even numbers #include<iostream> using namespace std; int collection(int n) { int i, sum=0, number; for(i=0,i<n,i++) { cout<<“Enter number:”; cin>>number; if (number%2==0) sum+=number;} return sum; } int main() { int n; cout<<“Enter n:”; cin>>n; cout<<“The sum is “<< collection(n);

14 3. Printing a triangle of stars
Examples tasks for functions : 3. Printing a triangle of stars #include <iostream> using namespace std; int stars(int n) int i; if(n==0) return 0; else {for (i=1;i<=n;i++) cout<<“*”<<endl; return stars(n-1) }} int main() { int n; cout<<“How many stars must be printed“; cin>>n; stars(n) } return 0; }

15 Examples tasks for functions :
4. Calculate the factorial of the number added feature rekruzivna #include <iostream> using namespace std; int faktoriel(int n) if(n==1) return n; else return n*faktoriel(n-1) } int main() int n; cout<<“Enter n:”; cin>>n; cout<<“Faktoriel of n is”; fakoriel(n) return 0;


Download ppt "Http://ioerc.mk Functions in C ++ Subject: Programming Languages ​​for III year Topic: functions, member class."

Similar presentations


Ads by Google