Funksiyalar

Funksiyadan istifade eden kod.
funksiya salam_dunya(){
  
  xaricet("Salam Dunya");
  
}

salam_dunya();
Salam Dunya

Funksiyadan istifadə etməklə ekranda cərgə çap edən kod.
tam x[10][10], k , j;

funksiya cerge_cap(){

  say (k 10){
  say (j 10)
     xaricet(x[k][j] , " ");
     xaricet("\y");
   }
}

   
//cergeye qiymetler menimsedek
say (k 10) 
  say (j 10)
    x[k][j] = 100 + k*j;

// cerge_cap funksiyasindan istifade etmekle
// x cergesini cap ededk
cerge_cap();
101 102 103 104 105 106 107 108 109 110
102 104 106 108 110 112 114 116 118 120
103 106 109 112 115 118 121 124 127 130
104 108 112 116 120 124 128 132 136 140
105 110 115 120 125 130 135 140 145 150
106 112 118 124 130 136 142 148 154 160
107 114 121 128 135 142 149 156 163 170
108 116 124 132 140 148 156 164 172 180
109 118 127 136 145 154 163 172 181 190
110 120 130 140 150 160 170 180 190 200

Cərgənin mənimsədilməsi və çap olunması ayrı-ayrı funksiyalarla yerinə yetirilir.
tam x[10][10], k , j;
   
funksiya cerge_cap(){
    
  say (k 10){
    say (j 10)
      xaricet(x[k][j] , " ");
      xaricet("\y");
   }
}
   
funksiya cerge_menimset(){
    
  say (k 10) 
    say (j 10)
      x[k][j] = 100 + k*j;
}
 
cerge_menimset();
cerge_cap();
101 102 103 104 105 106 107 108 109 110
102 104 106 108 110 112 114 116 118 120
103 106 109 112 115 118 121 124 127 130
104 108 112 116 120 124 128 132 136 140
105 110 115 120 125 130 135 140 145 150
106 112 118 124 130 136 142 148 154 160
107 114 121 128 135 142 149 156 163 170
108 116 124 132 140 148 156 164 172 180
109 118 127 136 145 154 163 172 181 190
110 120 130 140 150 160 170 180 190 200

Funksiyaya parametr ötürülməsi
//test funksiyasi
funksiya test (tam x){

  xaricet("men qebul etdim ", x , "\y");

}

// test funksiyasini cagiraq ve ona
//  parametr olaraq 5 qiymeti oturek
test(5);
men qebul etdim 5

Funksiyaya parametr ötürülməsi
tam x, y, z[5], q[10][10];

//test funksiyasi
funksiya test ( tam x){
  
  xaricet("mene oturuldu ", x , "\y");

}

x = 17;
y = 12 + x;
z[3] = 2*x*y;
q[8][4] = 5 + z[3] + x - y;
  
// test funksiyasini bir nece defe cagiraq
// ve ona muxtelif parametrler oturek
test(90);
test(x);
test(y);
test(z[3]);
test(q[8][4]);
test(45 + x);
test(x + y + z[3] + q[8][4]);
men qebul etdim 90
men qebul etdim 17
men qebul etdim 29
men qebul etdim 986
men qebul etdim 979
men qebul etdim 62
men qebul etdim 2011

Funksiyadan funksiyanı çağırmaq
tam x[10];

funksiya crg_daxilet (){
 
 tam i;
  
  xaricet("10 eded daxil edin \y");
  
  //x qlobal cergedir
  say (i 10)
   daxilet(x[i]);
}

funksiya crg_xaricet (){

  tam i;
  
  //x qlobal cergedir
  say (i 10)
   xaricet(x[i], " ");
  
  xaricet("\y");  
}

funksiya crg_artan (){

  tam i,j, mvq;

  //x qlobal cergedir
  say (i 10)
   say (j i 10)
    eger (x[i] > x[j]){
      mvq = x[i];
      x[i] = x[j];
      x[j] = mvq;  
    }
}

funksiya crg_azalan (){

 tam i,j, mvq;

  //x qlobal cergedir
  say (i 10)
   say (j i 10)
    eger (x[i] < x[j]){
     mvq = x[i];
     x[i] = x[j];
     x[j] = mvq;  
    }
}
  
crg_daxilet();
crg_artan();
xaricet("Cergenin elementleri artan sirada\y");
crg_xaricet();
crg_azalan();
xaricet("Cergenin elementleri azalan sirada\y");
crg_xaricet();
10 eded daxil edin
12 3 45 6 768 90 321 4 55 420
Cergenin elementleri artan sirada
3 4 6 12 45 55 90 321 420 768
Cergenin elementleri azalan sirada
768 420 321 90 55 45 12 6 4 3

Rekursiya. Aşağıdakı kod rekursiyadan istifadə etməklə cərgənin elementlərin çap edir.
tam x[10],k;

funksiya f (tam k){
   
 eger (k > 10) 
   qaytar;
   
 xaricet(x[k], " "); 
 
 f(k + 1);
} 

xaricet("10 eded daxil edin.\y");   

say (k 10)
 daxilet(x[k]);
  
xaricet("cergenin elementleri \y"); 

f(1);
10 eded daxil edin.
2 34 5 7 67 8 9 21 33 4
cergenin elementleri
2 34 5 7 67 8 9 21 33 4

N! - n faktorialı rekursiv hesablayan kod.
tam k, z;

funksiya nf (tam k){
  
  eger (k == 0 )  
   qaytar 1;
   
  qaytar  k * nf(k-1);
}

xaricet("Her hansi eded daxil edin.\y");
daxilet(k);
 
z = nf(k);
xaricet(k, "! = ", z, "\y");
Her hansi eded daxil edin.
7
7! = 5040

kvk() Kitabxana funksiyasından istifadə etməklə verilmiş ədədin kvadrat kökünü hesablayan kod.
kesr x, net;

xaricet("Her-hansi eded daxil edin\y");
daxilet(x);

net = kvk(x);
xaricet(x, " -in kvadrat koku = ", net , "\y");
Her-hansi eded daxil edin
64.256
64.256 -in kvadrat koku = 8.015984

Kvadrat tənliyin köklərini tapan kod.
kesr a, b, c, x1, x2, determinant;

xaricet(" a, b ve c emsallarini daxil edin: ");
daxilet(a, b, c);
determinant = b*b - 4*a*c;

eger (determinant > 0) {
    x1 = (-1*b + kvk(determinant)) / (2*a);
    x2 = (-1*b - kvk(determinant)) / (2*a);
    xaricet("Kokler ferqlidir. \y");
    xaricet("x1 = ", x1, "\y");
    xaricet("x2 = ", x2, "\y");
}
yoxsa 
    eger(determinant == 0) {
        xaricet("Kokler eynidir. \y");
        x1 = (-1*b + kvk(determinant)) / (2*a);
        xaricet("x1 = ", x1, "\y");
}
yoxsa 
    xaricet("Kvadrat tenliyin heqiqi koku yoxdur. \y");
a, b ve c emsallarini daxil edin: 
1 -1 -6
Kokler ferqlidir.
x1 = 3
x2 = -2