美文网首页
知识点1:函数(function)

知识点1:函数(function)

作者: BryantHe | 来源:发表于2017-03-12 19:05 被阅读16次

    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.


    相关文章

      网友评论

          本文标题:知识点1:函数(function)

          本文链接:https://www.haomeiwen.com/subject/epvagttx.html