Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikibooksThe Free Textbook Project
Search

D (The Programming Language)/d2/Intro to Functions

50% developed
From Wikibooks, open books for an open world
<D (The Programming Language)
Topics:Language and Phobos -DGui (example placeholder - can be any library)
PreviousLessons:0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -TNext


Lesson 3: Intro to Functions

[edit |edit source]

In this lesson, you see more of functions, which were used back in Lesson 1. If you are already familiar with another C-like language, you only need to skim this over.

Introductory Code

[edit |edit source]
importstd.stdio;intwatch_tv(intfirst_watched,intthen_watched,stringchannel="The PHBOS Channel"){writefln("Finished watching %s.",channel);returnfirst_watched+then_watched;}intcommercials(intminutes){writefln("Watching %s minutes of commercials.",minutes);returnminutes;}intcartoons(intminutes){writefln("Watching %s minutes of cartoons.",minutes);returnminutes;}voidmain(){autominutesoftv=watch_tv(commercials(10),cartoons(30));writeln(minutesoftv," minutes of TV watched!");}/*Output:  Watching 10 minutes of commercials.  Watching 30 minutes of cartoons.  Finished watching The PHBOS Channel.  40 minutes of TV watched!*/

Concepts

[edit |edit source]

Functions

[edit |edit source]

Functions allow you to write more organized code. With functions, you can write some code once, and then call it whenever you need to use that code.

Function Syntax

[edit |edit source]

Let's take a look at the syntax:

return_typename_of_function(arg1typearg1,arg2typearg2){// function body code herereturnsomething;}
Retrieved from "https://en.wikibooks.org/w/index.php?title=D_(The_Programming_Language)/d2/Intro_to_Functions&oldid=3676128"
Category:

[8]ページ先頭

©2009-2025 Movatter.jp