D編程 函數(shù)

2021-09-01 10:24 更新

本章介紹D編程中的函數(shù)。

函數(shù)定義

return_type function_name( parameter list ) { 
   body of the function 
}

調(diào)用函數(shù)

您可以按以下方式調(diào)用函數(shù)-

function_name(parameter_values)

函數(shù)類型

D編程支持多種函數(shù),下面列出了它們。

  • Pure  
  • Nothrow
  • Ref        
  • Auto
  • Variadic
  • Input
  • Property

下面說明各種函數(shù)。

Pure 函數(shù)

Pure函數(shù)是無法通過其參數(shù)訪問全局或靜態(tài),可變狀態(tài)的函數(shù)。

Pure函數(shù)是無法通過其參數(shù)訪問全局或靜態(tài),可變狀態(tài)的函數(shù)。這可以保證純函數(shù)不對沒有傳遞給它的任何東西進(jìn)行改變,并且在編譯器可以保證純函數(shù)不能更改其參數(shù)的情況下。

import std.stdio; 

int x=10; 
immutable int y=30; 
const int* p;  

pure int purefunc(int i,const char* q,immutable int* s) { 
   //writeln("Simple print"); //cannot call impure function 'writeln'
   
   debug writeln("in foo()"); //ok, impure code allowed in debug statement 
   //x=i;  //error, modifying global state 
   //i=x;  //error, reading mutable global state 
   //i=*p; //error, reading const global state
   i=y;     //ok, reading immutable global state 
   auto myvar=new int;     //Can use the new expression: 
   return i; 
}

void main() { 
   writeln("Value returned from pure function : ",purefunc(x,null,null)); 
}

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

Value returned from pure function : 30 

Nothrow 函數(shù)

Nothrow 函數(shù)不會拋出任何異常信息。

import std.stdio; 

int add(int a, int b) nothrow { 
   //writeln("adding"); This will fail because writeln may throw 
   int result; 
   
   try { 
      writeln("adding"); //compiles 
      result=a + b; 
   } catch (Exception error) { //catches all exceptions 
   }

   return result; 
} 
 
void main() { 
   writeln("Added value is ", add(10,20)); 
}

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

adding 
Added value is 30 

Ref 函數(shù)

Ref 引用函數(shù)允許函數(shù)通過引用返回,這類似于ref函數(shù)參數(shù)。

import std.stdio;

ref int greater(ref int first, ref int second) { 
   return (first > second) ? first : second; 
} 
 
void main() {
   int a=1; 
   int b=2;  
   
   greater(a, b) += 10;   
   writefln("a: %s, b: %s", a, b);   
}

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

a: 1, b: 12

Auto 函數(shù)

Auto 自動函數(shù)可以返回任何類型的值,對于要返回的類型沒有任何限制。

import std.stdio;

auto add(int first, double second) { 
   double result=first + second; 
   return result; 
} 

void main() { 
   int a=1; 
   double b=2.5; 
   
   writeln("add(a,b)=", add(a, b)); 
}

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

add(a,b)=3.5

Variadic 函數(shù)

Variadiac 函數(shù)是在運(yùn)行時確定函數(shù)參數(shù)數(shù)量的那些函數(shù),在C語言中,存在至少一個參數(shù)的限制,但是在D編程中,沒有這種限制。

import std.stdio;
import core.vararg;

void printargs(int x, ...) {  
   for (int i=0; i < _arguments.length; i++) {  
      write(_arguments[i]);  
   
      if (_arguments[i] == typeid(int)) { 
         int j=va_arg!(int)(_argptr); 
         writefln("\t%d", j); 
      } else if (_arguments[i] == typeid(long)) { 
         long j=va_arg!(long)(_argptr); 
         writefln("\t%d", j); 
      } else if (_arguments[i] == typeid(double)) { 
         double d=va_arg!(double)(_argptr); 
         writefln("\t%g", d); 
      } 
   } 
}
  
void main() { 
   printargs(1, 2, 3L, 4.5); 
}

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

int 2 
long 3 
double 4.5

Inout 函數(shù)

inout函數(shù)可用于參數(shù)和返回類型,inout將推導(dǎo)的可變性屬性轉(zhuǎn)換為返回類型。下面顯示了一個簡單的示例,顯示了如何更改可變性。

import std.stdio;

inout(char)[] qoutedWord(inout(char)[] phrase) { 
   return '"' ~ phrase ~ '"';
}

void main() { 
   char[] a="test a".dup; 

   a=qoutedWord(a); 
   writeln(typeof(qoutedWord(a)).stringof," ", a);  

   const(char)[] b="test b"; 
   b=qoutedWord(b); 
   writeln(typeof(qoutedWord(b)).stringof," ", b); 

   immutable(char)[] c="test c"; 
   c=qoutedWord(c); 
   writeln(typeof(qoutedWord(c)).stringof," ", c); 
} 

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

char[] "test a" 
const(char)[] "test b" 
string "test c"

Property 函數(shù)

Property屬性允許使用成員函數(shù),它使用@property關(guān)鍵字。

import std.stdio;

struct Rectangle { 
   double width; 
   double height;  

   double area() const @property {  
      return width*height;  
   } 

   void area(double newArea) @property {  
      auto multiplier=newArea/area; 
      width *= multiplier; 
      writeln("Value set!");  
   } 
}

void main() { 
   auto rectangle=Rectangle(20,10); 
   writeln("The area is ", rectangle.area);  
   
   rectangle.area(300); 
   writeln("Modified width is ", rectangle.width); 
}

編譯并執(zhí)行上述代碼后,將產(chǎn)生以下輸出-

The area is 200 
Value set! 
Modified width is 30


以上內(nèi)容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號