目录

D编程 - 功能(Functions)

本章介绍D编程中使用的功能。

D中的函数定义

基本函数定义由函数头和函数体组成。

语法 (Syntax)

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

以下是函数的所有部分 -

  • Return Type - 函数可以返回值。 return_type是函数返回的值的数据类型。 某些函数执行所需的操作而不返回值。 在这种情况下,return_type是关键字void

  • Function Name - 这是Function Name的实际名称。 函数名称和参数列表一起构成函数签名。

  • Parameters - 参数类似于占位符。 调用函数时,将值传递给参数。 该值称为实际参数或参数。 参数列表是指函数参数的类型,顺序和数量。 参数是可选的; 也就是说,函数可能不包含任何参数。

  • Function Body - 函数体包含一组语句,用于定义函数的功能。

调用一个函数 (Calling a Function)

您可以按如下方式调用函数 -

function_name(parameter_values)

D中的函数类型

D编程支持多种功能,如下所示。

  • 纯粹的功能
  • Nothrow功能
  • 参考功能
  • 自动功能
  • 变量函数
  • Inout功能
  • Property Functions

各种功能解释如下。

纯函数 (Pure Functions)

纯函数是无法通过参数访问全局或静态,可变状态的函数。 这可以基于以下事实实现优化:保证纯函数不会改变任何未传递给它的函数,并且在编译器可以保证纯函数不能改变其参数的情况下,它可以实现完整的,功能纯的,是,保证函数将始终为相同的参数返回相同的结果)。

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)); 
}

编译并执行上述代码时,会产生以下结果 -

Value returned from pure function : 30 

Nothrow Functions

Nothrow函数不会抛出从类Exception派生的任何异常。 Nothrow函数与投掷函数协变。

Nothrow保证函数不会发出任何异常。

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)); 
}

编译并执行上述代码时,会产生以下结果 -

adding 
Added value is 30 

Ref 函数

参考函数允许函数通过引用返回。 这类似于ref函数参数。

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);   
}

编译并执行上述代码时,会产生以下结果 -

a: 1, b: 12

Auto 函数

自动功能可以返回任何类型的值。 要返回的类型没有限制。 下面给出了自动类型函数的一个简单示例。

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)); 
}

编译并执行上述代码时,会产生以下结果 -

add(a,b) = 3.5

Variadic 函数

Variadiac函数是在运行时确定函数的参数数量的函数。 在C中,存在至少一个参数的限制。 但在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); 
}

编译并执行上述代码时,会产生以下结果 -

int 2 
long 3 
double 4.5

Inout 函数

inout可用于参数和返回类型的函数。 它就像是mutable,const和immutable的模板。 mutability属性是从参数中推导出来的。 means,inout将推断的mutability属性传递给返回类型。 下面显示了一个显示可变性如何变化的简单示例。

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); 
} 

编译并执行上述代码时,会产生以下结果 -

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

Property 函数

属性允许使用成员函数,如成员变量。 它使用@property关键字。 属性与相关函数链接,该函数根据需求返回值。 属性的简单示例如下所示。

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); 
}

编译并执行上述代码时,会产生以下结果 -

The area is 200 
Value set! 
Modified width is 30
↑回到顶部↑
WIKI教程 @2018