知识点1:函数(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.


最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • **2014真题Directions:Read the following text. Choose the be...
    又是夜半惊坐起阅读 13,559评论 0 23
  • 郭芳艳 焦点网络初级五期 坚持原创分享第143天 真是好惭愧!与大家定好的连讲十一天的任务我没有完成。...
    冰山蓝鹰阅读 1,346评论 0 0
  • 感赏七月的工作一切顺利。 感赏所有人对我的信任和支持。 感赏今年已经过半。 感赏苦难的日子一天天远去。 感赏我的孩...
    四王点菜阅读 1,324评论 0 0
  • 人靠衣装马靠鞍、三分长像七分打扮等等这都是古人留给我们的智慧。在穿衣打扮上要找到自身的优缺点,怎样搭配最大程度...
    艾阳阳阅读 1,504评论 0 2
  • 晚上看到闺蜜朋友圈说今天她生日,我就给她发视频聊天,乐呵呵的聊了很久(其实是听我马不停蹄地讲了很久。。。能容忍我的...
    亦尘亦尘阅读 1,846评论 0 1

友情链接更多精彩内容