知識點(diǎn)1:函數(shù)(function)

Where there's a lot of synonyms for the same word.

So we call the functions. But you might also hear them referred to as procedures, or methods, particularly, if you've ever done any object oriented programming before -- and don't worry if you haven't, not a big deal -- but in audit oriented languages are frequently called methods. sometimes they're called subroutines.

But they really all refer to the same basic idea.

Well a function is really nothing more than a black box. A black box that has a set of zero or more inputs and a single output.

Why do we call it a black box?

We've seen print f, for example, which is a function that we didn't write ourselves, but we do use all the time.

we don't really need to know how it's actually implemented under the hood. It just means we don't really care how it's implemented under the hood as long as the output is what we expect.

In fact, that's part of the contract of using functions, particularly functions that others write.

The behavior is always going to be typical, unpredictable
based on the name of the function.

And that's why it's really important when you write functions
or when other people write functions that you might use, that those functions have clear, relatively obvious names, and are well documented. Which is certainly the case for function like printf.

So why do we use functions?

Well as I said earlier, if we write all of our code inside of main things can get really cumbersome and really complicated.

Well as I said earlier, if we write all of our code inside of main things
can get really cumbersome and really complicated.

Functions also allow us to simplify the coding process. It's a lot easier to debug a 10 line function versus a 100 line function or a 1,000 line function.

Lastly, if we write functions we can reuse those various parts.

Functions can be recycled. They can be used in one program or another. You've already written the function, all you need to do is tell that program where to find that function.

We've been recycling and using print f for over 40 years. But it was only written one time. Pretty useful, right.

declaration and definition:

float mult_two_reals(float x, float y);

The semicolon " ; " indicates that that is a function declaration.

float mult_two_reals(float x, float y) {
    float product = x * y;
    return product;
}

The beginning of the function definition looks almost exactly the same, type, name, comma separated argument list, no semicolon, open curly brace.

The open curly brace, just as we've been doing with main, means that we are now beginning to define what happens inside the black box that we've decided to call mult two reals.

What does return mean here. Well return is the way we indicate that's how we're passing the output back out. So return something, is the same as, this is the output of the black box.


最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末咖杂,一起剝皮案震驚了整個濱河市绍绘,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌谦趣,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,590評論 6 517
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叔锐,死亡現(xiàn)場離奇詭異芋哭,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)睬隶,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,157評論 3 399
  • 文/潘曉璐 我一進(jìn)店門锣夹,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人苏潜,你說我怎么就攤上這事银萍。” “怎么了贴唇?”我有些...
    開封第一講書人閱讀 169,301評論 0 362
  • 文/不壞的土叔 我叫張陵飞袋,是天一觀的道長滤蝠。 經(jīng)常有香客問我授嘀,道長物咳,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 60,078評論 1 300
  • 正文 為了忘掉前任蹄皱,我火速辦了婚禮,結(jié)果婚禮上巷折,老公的妹妹穿的比我還像新娘。我一直安慰自己锻拘,他們只是感情好油吭,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,082評論 6 398
  • 文/花漫 我一把揭開白布击蹲。 她就那樣靜靜地躺著,像睡著了一般婉宰。 火紅的嫁衣襯著肌膚如雪歌豺。 梳的紋絲不亂的頭發(fā)上心包,一...
    開封第一講書人閱讀 52,682評論 1 312
  • 那天,我揣著相機(jī)與錄音蟹腾,去河邊找鬼。 笑死值戳,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的堕虹。 我是一名探鬼主播,決...
    沈念sama閱讀 41,155評論 3 422
  • 文/蒼蘭香墨 我猛地睜開眼鲫凶,長吁一口氣:“原來是場噩夢啊……” “哼衩辟!你這毒婦竟也來了波附?” 一聲冷哼從身側(cè)響起艺晴,我...
    開封第一講書人閱讀 40,098評論 0 277
  • 序言:老撾萬榮一對情侶失蹤封寞,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后狈究,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體盏求,經(jīng)...
    沈念sama閱讀 46,638評論 1 319
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,701評論 3 342
  • 正文 我和宋清朗相戀三年磅废,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片拯勉。...
    茶點(diǎn)故事閱讀 40,852評論 1 353
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖宫峦,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情斗遏,我是刑警寧澤,帶...
    沈念sama閱讀 36,520評論 5 351
  • 正文 年R本政府宣布账蓉,位于F島的核電站,受9級特大地震影響铸本,放射性物質(zhì)發(fā)生泄漏遵堵。R本人自食惡果不足惜箱玷,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,181評論 3 335
  • 文/蒙蒙 一陌宿、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧壳坪,春花似錦、人聲如沸沐批。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,674評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽发框。三九已至,卻和暖如春梅惯,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背个唧。 一陣腳步聲響...
    開封第一講書人閱讀 33,788評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留犁河,地道東北人鳖枕。 一個月前我還...
    沈念sama閱讀 49,279評論 3 379
  • 正文 我出身青樓桨螺,卻偏偏與公主長得像,于是被迫代替她去往敵國和親魏烫。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,851評論 2 361

推薦閱讀更多精彩內(nèi)容

  • **2014真題Directions:Read the following text. Choose the be...
    又是夜半驚坐起閱讀 9,585評論 0 23
  • 郭芳艷 焦點(diǎn)網(wǎng)絡(luò)初級五期 堅(jiān)持原創(chuàng)分享第143天 真是好慚愧!與大家定好的連講十一天的任務(wù)我沒有完成煌张。...
    冰山藍(lán)鷹閱讀 164評論 0 0
  • 感賞七月的工作一切順利。 感賞所有人對我的信任和支持链嘀。 感賞今年已經(jīng)過半档玻。 感賞苦難的日子一天天遠(yuǎn)去怀泊。 感賞我的孩...
    四王點(diǎn)菜閱讀 150評論 0 0
  • 人靠衣裝馬靠鞍误趴、三分長像七分打扮等等這都是古人留給我們的智慧。在穿衣打扮上要找到自身的優(yōu)缺點(diǎn)冤留,怎樣搭配最大程度...
    艾陽陽閱讀 182評論 0 2
  • 晚上看到閨蜜朋友圈說今天她生日树灶,我就給她發(fā)視頻聊天,樂呵呵的聊了很久(其實(shí)是聽我馬不停蹄地講了很久天通。。像寒。能容忍我的...
    亦塵亦塵閱讀 248評論 0 1