D (The Programming Language)/d2/Intro to Functions
Tools
General
Sister projects
In other projects
| Topics: | Language and Phobos -DGui (example placeholder - can be any library) | ||
| Previous | Lessons: | 0 -1 -2 -3 -4 -5 -6 -7 -8 -9 -10 -T | Next |
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.
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!*/
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.
Let's take a look at the syntax:
return_typename_of_function(arg1typearg1,arg2typearg2){// function body code herereturnsomething;}