D语言 - 函数声明

D语言 - 函数声明 首页 / D语言入门教程 / D语言 - 函数声明

本章介绍D编程中的函数。

函数定义

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

调用函数

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

function_name(parameter_values)

函数类型

D编程支持多种函数,下面列出了它们。

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

下面说明各种函数。

Pure 函数

Pure函数是无法通过其参数访问全局或静态,可变状态的函数。

Pure函数是无法通过其参数访问全局或静态,可变状态的函数。这可以保证纯函数不对没有传递给它的任何东西进行改变,并且在编译器可以保证纯函数不能更改其参数的情况下。

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"); //不能调用impure函数 'writeln'
   
   debug writeln("in foo()"); //调试语句中允许调用
   //x=i;  //错误,修改全局状态
   //i=x;  //错误,读取可变全局状态
   //i=*p; //错误,读取 const 全局状态
   i=y;     //OK,读取不可变的全局状态
   auto myvar=new int;     //可以使用新的表达式:
   return i; 
}

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

编译并执行上述代码后,将产生以下输出-

无涯教程网

Value returned from pure function : 30 

Nothrow 函数

Nothrow 函数不会抛出任何异常信息。

import std.stdio; 

int add(int a, int b) nothrow { 
   //writeln("adding"); 这将失败,因为 writeln 可能会抛出
   int result; 
   
   try { 
      writeln("adding"); 
      result=a + b; 
   } catch (Exception error) { //捕获所有异常
   }

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

编译并执行上述代码后,将产生以下输出-

无涯教程网

adding 
Added value is 30 

Ref 函数

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 函数

Auto 自动函数可以返回任何类型的值,对于要返回的类型没有任何限制。

链接:https://www.learnfk.comhttps://www.learnfk.com/d-programming/d-programming-functions.html

来源:LearnFk无涯教程网

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函数可用于参数和返回类型,inout将推导的可变性属性转换为返回类型。下面显示了一个简单的示例,显示了如何更改可变性。

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属性允许使用成员函数,它使用@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

祝学习愉快!(内容编辑有误?请选中要编辑内容 -> 右键 -> 修改 -> 提交!)

技术教程推荐

如何设计一个秒杀系统 -〔许令波〕

.NET Core开发实战 -〔肖伟宇〕

互联网人的英语私教课 -〔陈亦峰〕

微信小程序全栈开发实战 -〔李艺〕

Selenium自动化测试实战 -〔郭宏志〕

etcd实战课 -〔唐聪〕

Redis源码剖析与实战 -〔蒋德钧〕

超级访谈:对话张雪峰 -〔张雪峰〕

B端体验设计入门课 -〔林远宏(汤圆)〕

好记忆不如烂笔头。留下您的足迹吧 :)