text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
So notice, line 7, I
can call print name. | 6,319.57 | 2.69 |
Line 10, I can define
or implement print name. | 6,322.26 | 3.66 |
But, unfortunately, that's not enough. | 6,325.92 | 2.53 |
>> Let me go ahead and
recompile this after saving. | 6,328.45 | 2.78 |
Whoa, now, I've made it
worse, it would seem. | 6,331.23 | 2.68 |
So implicit declaration of
function print name is invalid. | 6,333.91 | 3.117 |
And, again, there's more errors. | 6,337.027 | 1.333 |
But as I cautioned earlier, even
if you get overwhelmed with, | 6,338.36 | 3.07 |
or a little sad to see so many
errors, focus only on the first | 6,341.43 | 3.42 |
initially, because it might just
have had a cascading effect. | 6,344.85 | 2.65 |
So C, or Clang more specifically,
still does not recognize print name. | 6,347.5 | 4.47 |
>> And that's because Clang,
by design, is kind of dumb. | 6,351.97 | 2.61 |
It only does what you tell it to do. | 6,354.58 | 1.7 |
And it only does so in the order
in which you tell it to do. | 6,356.28 | 4.67 |
>> So I have defined main on line four,
like we've been doing pretty often. | 6,360.95 | 4.32 |
I've defined print name on line 10. | 6,365.27 | 2.71 |
But I'm trying to use
print name on line seven. | 6,367.98 | 3.813 |
>> It's too soon, doesn't exist yet. | 6,371.793 | 1.877 |
So I could be clever, and be like,
OK, so let's just play along, | 6,373.67 | 5.48 |
and move print name up
here, and re-compile. | 6,379.15 | 4.53 |
Oh my God. | 6,383.68 | 0.87 |
It worked. | 6,384.55 | 0.71 |
It was as simple as that. | 6,385.26 | 1.41 |
>> But the logic is exactly that. | 6,386.67 | 1.45 |
You have to teach Clang what it
is by defining the function first. | 6,388.12 | 2.75 |
Then you can use it. | 6,390.87 | 1.05 |
But, frankly, this feels
like a slippery slope. | 6,391.92 | 2.02 |
>> So every time I run
into a problem, I'm just | 6,393.94 | 1.833 |
going to highlight and copy the code
I wrote, cut it and paste it up here. | 6,395.773 | 3.677 |
And, surely, we could
contrive some scenarios | 6,399.45 | 1.92 |
where one function might
need to call another. | 6,401.37 | 1.916 |
And you just can't put every
function above every other. | 6,403.286 | 2.744 |
>> So it turns out there's
a better solution. | 6,406.03 | 1.9 |
We can leave this be. | 6,407.93 | 2.17 |
And, frankly, it's generally nice,
and convenient, and good design | 6,410.1 | 3.577 |
to put main first, because, again,
main just like when green flag clicked, | 6,413.677 | 3.083 |
that is the function that
gets executed by default. | 6,416.76 | 2.267 |
So you might as well put
it at the top of the file | 6,419.027 | 2.083 |
so that when you or any
other human looks at the file | 6,421.11 | 2.45 |
you know what's going on
just by reading main first. | 6,423.56 | 2.8 |
So it turns out, we can tell Clang
proactively, hey, Clang, on line four, | 6,426.36 | 9 |
I promise to implement
a function called Print | 6,435.36 | 2.58 |
Name that takes a string called name
as input, and returns nothing, void. | 6,437.94 | 4.66 |
And I'll get around to
implementing it later. | 6,442.6 | 2.17 |
>> Here comes Main. | 6,444.77 | 0.91 |
Main now on line 9 can use
Print Name because Clang | 6,445.68 | 3.45 |
is trusting that, eventually,
it will encounter the definition | 6,449.13 | 3.47 |
of the implementation of Print Name. | 6,452.6 | 2.28 |
So after saving my file, let
me go ahead and make prototype, | 6,454.88 | 2.51 |
looks good this time. | 6,457.39 | 1.108 |
Dot slash, prototype, let me
go ahead and type in a name. | 6,458.498 | 4.972 |
David, hello David, Zamila, hello
Zamila, and, indeed, now it works. | 6,463.47 | 4.97 |
>> So the ingredient here is that we've
made a custom function, like a custom | 6,468.44 | 3.76 |
Scratch block we're calling it. | 6,472.2 | 2.019 |
But unlike Scratch where you can
just create it and start using it, | 6,474.219 | 2.791 |
now we have to be a
little more pedantic, | 6,477.01 | 2.32 |
and actually train Clang
to use, or to expect it. | 6,479.33 | 4.08 |
Now, as an aside, why all this time have
we been just blindly on faith including | 6,483.41 | 5.73 |
CS50.h, and including standard IO.h? | 6,489.14 | 3.03 |
>> Well, it turns out,
among a few other things, | 6,492.17 | 3.02 |
all that's in those dot h
files, which happen to be files. | 6,495.19 | 3.36 |
They're header files, so to speak. | 6,498.55 | 1.91 |
They're still written in C. But
they're a different type of file. | 6,500.46 | 2.81 |
>> For now, you can pretty much assume
that all that is inside of CS50.h | 6,503.27 | 5.42 |
is some one-liners like this, not
for functions called Print Name, | 6,508.69 | 4.67 |
but for Get String, Get
Float, and a few others. | 6,513.36 | 3.48 |
And there are similar prototypes,
one liners, inside of standard IO.h | 6,516.84 | 4.67 |
for printf, which is now in
my own Print Name function. | 6,521.51 | 4.731 |
So in other words, this whole time we've
just been blindly copying and pasting | 6,526.241 | 3.249 |
include this, include
that, what's going on? | 6,529.49 | 2.29 |
Those are just kind of clues
to Clang as to what functions | 6,531.78 | 3.53 |
are, indeed, implemented, just
elsewhere in different files | 6,535.31 | 4.86 |
elsewhere on the system. | 6,540.17 | 2.27 |
>> So we've implemented print name. | 6,542.44 | 2.72 |
It does have this side effect of
printing something on the screen. | 6,545.16 | 2.75 |
But it doesn't actually
hand me something back. | 6,547.91 | 2.26 |
How do we go about
implementing a program that | 6,550.17 | 2.03 |
does hand me something back? | 6,552.2 | 2.31 |
>> Well, let's try this. | 6,554.51 | 1.07 |
Let me go ahead and implement
a file called return.c | 6,555.58 | 5.78 |
so we can demonstrate how something
like Get String, or Get Int, | 6,561.36 | 3.17 |
is actually returning
something back to the user. | 6,564.53 | 2.81 |
Let's go ahead and define int main void. | 6,567.34 | 2.5 |
>> And, again, in the future, we'll
explain what that int and that void | 6,569.84 | 3.39 |
is actually doing. | 6,573.23 | 0.86 |
But for today, we'll
take it for granted. | 6,574.09 | 1.75 |
I'm going to go ahead and printf,
for a good user experience, x is. | 6,575.84 | 4.13 |
And then I'm going to wait for the
user to give me x with get int. | 6,579.97 | 4.39 |
>> And then I'm going to go ahead
and print out x to the square. | 6,584.36 | 4.099 |
So when you only have a
keyboard, people commonly | 6,588.459 | 2.041 |
use the little carrot
symbol on the keyboard | 6,590.5 | 2.1 |
to represent to the power
of, or the exponent of. | 6,592.6 | 2.73 |
So x squared is present i. | 6,595.33 | 3.63 |
>> And now I'm going to do this. | 6,598.96 | 1.7 |
I could just do-- what's x
squared? x squared is x times x. | 6,600.66 | 3.28 |
>> And we did this some
time ago already today. | 6,603.94 | 2.75 |
This doesn't feel like
all that much progress. | 6,606.69 | 2.04 |
You know what? | 6,608.73 | 0.84 |
Let's leverage some of that idea
from last time of abstraction. | 6,609.57 | 3.53 |
>> Wouldn't it be nice if
there's a function called | 6,613.1 | 2.98 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.