>> SPEAKER 1: So I feel like I tend to write printf hello world or printf
hello percent s quite often in contexts where I want to print out a
string, sometimes asking the user part of that string.
Well, wouldn't it be nice if we created a function of our own called,
say, print name, whose purpose in life is simply to print hello comma
so-and-so by accepting the so-and-so as an argument to that function?
We can declare a function of our own just as we can in Scratch as follows.
>> In other words, suppose I'd like to printf your name and then ask the user
for his or her name with string s equals GetString.
And then I want to call a function called print name
passing an s as its input.
>> Now unfortunately, there is no function called print name in C or in
the cs50 library.
But no matter, because we can make it ourselves.
To declare a function of our own and then implement it, we can simply make
some room in this file and declare this function as follows, void--
signifying that the function's not actually going to return something,
even though it will have a side effect--
print name, and then let's specify in parentheses that print name expects an
argument of type string.
And in general, we'll think of that as a name.
>> And let's open our brace and close our brace.
And then printf inside hello comma percent s new line comma name.
In other words, even though this function's only one line of code, it
does now have some higher level purpose to print
someone's name with a greeting.
>> Now inside of main, notice that I'm calling print name by passing an s.
And so I seem now to have a complete program.
The only difference being from others we've written is that not only does
this program have main, it also has a declaration and implementation of
print name, a function of my own.
>> Let's now compile my program with make function zero, and run it with dot
slash function zero.
When prompted for my name I'll type in David.
And I indeed am greeted with "Hello, David."
>> Now, it turns out I very deliberately declared print name above main,
thereby telling C in advance that there exists a function called print
name so that I could use it later in main.
Now, had I instead put print name at the bottom of my file, as frankly I
probably should do so that main remains the first thing that I or
another user sees in my code, it turns out that my code is
not going to compile.
>> Specifically, if I go back to my terminal window and run make function
zero now, notice that I'm yelled at.
If I scroll up in my terminal window, I'll see this first error in red,
implicit declaration of function print name.
>> Now, what does that mean?
Well, even though I've implemented print name in this file, I've not
implemented it before I've used it in main.
Now, we could go back to the old version and put print name's
implementation up top.
>> But I can instead just give C a hint that there's a function to come.
Specifically, above main I can declare print name just as I did earlier, but
end the line with a semicolon, thereby promising to implement but not yet
actually implementing the function called print name.
I can then have main at the top of my file as I hoped, and I can actually
implement print name at the bottom.
>> If I now return to my terminal window and recompile function zero with make,
this time it compiles.
And if I run it with dot slash function zero providing my name, I'm
greeted as "Hello, David."