text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
because it doesn't look
like you really need them. | 6,041.4 | 2.14 |
They don't really complete your
thought like a parenthesis or a curly | 6,043.54 | 3.25 |
brace does. | 6,046.79 | 0.924 |
Let's go ahead and
recompile the code and see. | 6,047.714 | 1.916 |
So make switch, dot slash switch. | 6,049.63 | 4.06 |
Let's type in lower case
y, the top case, Enter. | 6,053.69 | 2.745 |
So I typed y. | 6,059.39 | 1.31 |
>> The program said yes, no, error,
as though it was changing its mind. | 6,060.7 | 3.72 |
But it kind of was, because what happens
with a switch is the first case that | 6,064.42 | 4.86 |
match essentially means, hey computer,
execute all of the code beneath it. | 6,069.28 | 4.619 |
And if you don't say break, or
don't say break, or don't say break, | 6,073.899 | 2.791 |
the computer is going to blow
through all of those lines | 6,076.69 | 2.85 |
and execute all of them until
it gets to that curly brace. | 6,079.54 | 3.239 |
So the brakes are, indeed, necessary. | 6,082.779 | 1.541 |
But a takeaway here is, when
in doubt, try something. | 6,084.32 | 2.8 |
Maybe save your code first,
or save it in an extra file | 6,087.12 | 2.39 |
if you're really worried about
messing up and having to recover | 6,089.51 | 3.42 |
the work that you know is working. | 6,092.93 | 1.5 |
>> But try things. | 6,094.43 | 0.98 |
And don't be as afraid, perhaps,
of what the computer might do, | 6,095.41 | 2.664 |
or that you might break something. | 6,098.074 | 1.416 |
You can always revert back
to some earlier version. | 6,099.49 | 3.3 |
>> So let's end by looking
at the design of code. | 6,102.79 | 2.85 |
We have this ability now to write
conditions, and write loops, | 6,105.64 | 3.38 |
and variables, and call functions. | 6,109.02 | 1.83 |
So, frankly, we're kind of back at
where we were a week ago with Scratch, | 6,110.85 | 3.74 |
albeit with a less compelling textual
environment than Scratch allows. | 6,114.59 | 5.53 |
>> But notice how quickly we've acquired
that vocabulary, even if it's | 6,120.12 | 3.87 |
going to take a little while to sink in,
so that we can now use this vocabulary | 6,123.99 | 3.58 |
to write more interesting programs. | 6,127.57 | 2.75 |
And let's take a baby step
toward that, as follows. | 6,130.32 | 2.62 |
Let me go ahead and
create a new file here. | 6,132.94 | 1.95 |
>> I'm going to call this
prototype.c, and introduce | 6,134.89 | 2.86 |
for the first time, the ability
to make your own functions. | 6,137.75 | 3.204 |
Some of you might have
done this with Scratch, | 6,140.954 | 1.916 |
whereby you can create your
own custom blocks in Scratch, | 6,142.87 | 2.56 |
and then drag them into place
wherever you'd like in C. | 6,145.43 | 2.462 |
And in most programming
languages, you can do exactly | 6,147.892 | 2.208 |
that-- make your own functions,
if they don't already exist. | 6,150.1 | 3.48 |
>> So, for instance, let me go ahead
and include CS50.h, and include | 6,153.58 | 5.08 |
standard IO.h, int main void. | 6,158.66 | 4.45 |
And now we have a
placeholder ready to go. | 6,163.11 | 2.91 |
I keep printing things
like people's names today. | 6,166.02 | 2.53 |
And that feels like--
wouldn't be nice if there | 6,168.55 | 3.36 |
were a function called print name? | 6,171.91 | 2.026 |
I don't have to use printf. | 6,173.936 | 1.124 |
I don't have to remember
all the format codes. | 6,175.06 | 1.916 |
Why don't I, or why
didn't someone before me, | 6,176.976 | 3.074 |
create a function called print
name, that given some name, | 6,180.05 | 2.93 |
simply prints it out? | 6,182.98 | 1 |
>> In other words, if I say, hey,
computer, give me a string | 6,183.98 | 4.72 |
by asking the user for such,
via CS50's get string function. | 6,188.7 | 3.17 |
Hey, computer, put that string in
the variable in the left hand side, | 6,191.87 | 3.22 |
and call it s. | 6,195.09 | 1.06 |
And then, hey computer, go ahead
and print that person's name, done. | 6,196.15 | 6 |
>> Now, it would be nice, because
this program, aptly named, | 6,202.15 | 4.09 |
tells me what it's supposed to do
by way of those function's names. | 6,206.24 | 2.93 |
Let me go and make prototype, Enter. | 6,209.17 | 3.76 |
And, unfortunately,
this isn't going to fly. | 6,212.93 | 2 |
>> Prototype.c, line 7, character
5, error, implicit declaration | 6,214.93 | 4.5 |
of function print name
is invalid in C99, C99 | 6,219.43 | 3.53 |
meaning a version of C
that came out in 1999. | 6,222.96 | 2.17 |
That's all. | 6,225.13 | 0.6 |
>> So I don't know what
all of this means yet. | 6,225.73 | 3.05 |
But I do recognize error in red. | 6,228.78 | 2.03 |
That's pretty obvious. | 6,230.81 | 0.96 |
>> And it seems that with
the green character here, | 6,231.77 | 1.999 |
the issue is with print name, open
paren s, close paren, semi-colon. | 6,233.769 | 3.751 |
But implicit declaration of
function we did see briefly earlier. | 6,237.52 | 4.28 |
This means, simply, that Clang
does not know what I mean. | 6,241.8 | 3.08 |
>> I've used a vocabulary word that it's
never seen or been taught before. | 6,244.88 | 4.12 |
And so I need to teach it
what this function means. | 6,249 | 2.95 |
So I'm going to go ahead and do that. | 6,251.95 | 1.64 |
>> I'm going to go ahead and implement
my own function called Print Name. | 6,253.59 | 4.38 |
And I'm going to say, as follows, that
it does this, printf, hello, percent | 6,257.97 | 6.75 |
s, backslash n, name, semi-colon. | 6,264.72 | 3.04 |
So what did I just do? | 6,267.76 | 1.49 |
>> So it turns out, to
implement your own function, | 6,269.25 | 2.075 |
we kind of borrow some of
the same structure as main | 6,271.325 | 2.52 |
that we've just been
taken for granted, and I | 6,273.845 | 1.875 |
know just copying and
pasting pretty much what | 6,275.72 | 2.01 |
I've been writing in the past. | 6,277.73 | 1.44 |
But notice the pattern here. | 6,279.17 | 1.4 |
Int, Main, Void, we'll tease apart
before long what that actually means. | 6,280.57 | 3.18 |
>> But for today, just
notice the parallelism. | 6,283.75 | 2.41 |
Void, print name,
string name, so there's | 6,286.16 | 2.05 |
a purple keyword, which
we're going to start | 6,288.21 | 2.1 |
calling a return type, the name of
the function, and then the input. | 6,290.31 | 3.757 |
So, actually, we can distill
this kind of like last week | 6,294.067 | 2.333 |
as, this is the name or the
algorithm of the code we're | 6,296.4 | 2.63 |
going to write-- the
algorithm underlying | 6,299.03 | 1.731 |
the code we're going to write. | 6,300.761 | 1.249 |
>> This is its input. | 6,302.01 | 1.17 |
This is its output. | 6,303.18 | 1.49 |
This function, print name, is
designed to take a string called name, | 6,304.67 | 4.06 |
or whatever, as input, and then void. | 6,308.73 | 2.62 |
It doesn't return anything,
like get string or get int does. | 6,311.35 | 2.554 |
So it's going to hand me something back. | 6,313.904 | 1.666 |
It's just going to have a
side effect, so to speak, | 6,315.57 | 2.39 |
of printing a person's name. | 6,317.96 | 1.61 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.