text stringlengths 1 81 | start float64 0 10.1k | duration float64 0 24.9 |
|---|---|---|
square that does exactly that? | 6,616.08 | 2.38 |
It still, at the end of the
day, does the same math. | 6,618.46 | 2.18 |
But let's abstract
away the idea of taking | 6,620.64 | 1.77 |
one number multiplied by
another, and just give it a name, | 6,622.41 | 2.87 |
like square this value. | 6,625.28 | 2.08 |
>> And, in other words, in
C, let's create a function | 6,627.36 | 2.2 |
called square that does exactly that. | 6,629.56 | 3.1 |
It's going to be called square. | 6,632.66 | 1.94 |
It's going to take an int. | 6,634.6 | 1.19 |
And we'll will just
call it n, by default. | 6,635.79 | 2.03 |
>> But we could call it anything we want. | 6,637.82 | 1.583 |
And all that it's going to
do, literally, is return | 6,639.403 | 3.497 |
the result of n times n. | 6,642.9 | 2.91 |
But because it is
returning something, which | 6,645.81 | 3.17 |
is the keyword in purple we've
never seen before, I, on line 11, | 6,648.98 | 4.71 |
can't just say void this time. | 6,653.69 | 1.72 |
>> Void, in the example we just saw
rather of print name, just means, | 6,655.41 | 5.91 |
do something. | 6,661.32 | 0.87 |
But don't hand me something back. | 6,662.19 | 1.98 |
In this case, I do want
to return n times n, | 6,664.17 | 2.62 |
or whatever that is, that number. | 6,666.79 | 1.67 |
>> So I can't say, hey, computer,
I return nothing, void. | 6,668.46 | 4 |
It's going to return, by nature, an int. | 6,672.46 | 3.706 |
And so that's all that's going on here. | 6,676.166 | 1.624 |
>> The input to square
is going to be an int. | 6,677.79 | 2.28 |
And so that we can use it, it has to
have a name, N. It's going to output | 6,680.07 | 4.69 |
an int that doesn't need a name. | 6,684.76 | 1.48 |
We can leave it to main, or whoever is
using me to remember this value if we | 6,686.24 | 3.35 |
want with its own variable. | 6,689.59 | 1.53 |
>> And, again, the only new
keyword here is Return. | 6,691.12 | 2.11 |
And I'm just doing some math. | 6,693.23 | 1.25 |
If I really wanted to be unnecessary,
I could say int product gets n times n. | 6,694.48 | 7.345 |
>> And then I could say, return product. | 6,701.825 | 2.345 |
But, again, to my point earlier of
this just not being good design-- | 6,704.17 | 3.19 |
like, why introduce a name,
a symbol, like product, | 6,707.36 | 2.7 |
just to immediately return it? | 6,710.06 | 1.51 |
It's a little cleaner,
a little tighter, so | 6,711.57 | 2.1 |
to speak, just to say return n times
n, get rid of this line altogether. | 6,713.67 | 5.71 |
>> And it's just less code to read,
less opportunity for mistakes. | 6,719.38 | 3.48 |
And let's see if this
actually now works. | 6,722.86 | 2.32 |
Now, I'm going to go
ahead and make return. | 6,725.18 | 4.2 |
>> Uh-oh, implicit declaration of function. | 6,729.38 | 2.08 |
I made this mistake before, no big deal. | 6,731.46 | 2.62 |
Let me just type, or highlight and
copy, the exact same function prototype, | 6,734.08 | 4.87 |
or signature, of the function up here. | 6,738.95 | 2.392 |
Or I could move the whole function. | 6,741.342 | 1.458 |
>> But that's a little lazy. | 6,742.8 | 1.041 |
So we won't do that. | 6,743.841 | 1.029 |
Now, let me make return
again, dot slash return. | 6,744.87 | 3.09 |
>> x is 2. x squared is 4.
x is 3. x squared is 9. | 6,747.96 | 4.83 |
And the function seems
now to be working. | 6,752.79 | 2.51 |
So what's the difference here? | 6,755.3 | 1.25 |
I have a function that's called square,
in this case, which I put in an input. | 6,756.55 | 5.97 |
And I get back an output. | 6,762.52 | 1.31 |
And yet, previously, if
I open the other example | 6,763.83 | 2.38 |
from earlier, which
was called prototype.c, | 6,766.21 | 5.43 |
I had print name, which
returned void, so to speak, | 6,771.64 | 3.13 |
Or it returned nothing, and
simply had a side effect. | 6,774.77 | 3.96 |
>> So what's going on here? | 6,778.73 | 1.5 |
Well, consider the function
get string for just a moment. | 6,780.23 | 3.29 |
We've been using the function
get string in the following way. | 6,783.52 | 3.05 |
>> We've had a function get
string, like include CS50.h, | 6,786.57 | 3.894 |
include standard IO.h, int, main, void. | 6,790.464 | 6.16 |
And then every time I've
called get string thus far, | 6,796.624 | 2.166 |
I've said something like, string s
gets get string, because get string-- | 6,798.79 | 4.47 |
let's call this get.c-- get string
itself returns a string that I can then | 6,803.26 | 4.62 |
use, and say, hello, comma,
percent s, backslash n, s. | 6,807.88 | 4.17 |
>> So this is the same example,
really, that we had earlier. | 6,812.05 | 3.61 |
So get string returns a value. | 6,815.66 | 2.26 |
But a moment ago, print string
does not return a value. | 6,817.92 | 3.34 |
It simply has a side effect. | 6,821.26 | 1.461 |
So this is a fundamental difference. | 6,822.721 | 1.499 |
We've seen different
types of functions now, | 6,824.22 | 2.49 |
some of which have returned
values, some of which don't. | 6,826.71 | 2.78 |
So maybe it's string, or int, or float. | 6,829.49 | 2.4 |
Or maybe it's just void. | 6,831.89 | 1.59 |
>> And the difference is
that these functions that | 6,833.48 | 2.23 |
get data and return a value are actually
bringing something back to the table, | 6,835.71 | 4.23 |
so to speak. | 6,839.94 | 1.17 |
So let's go ahead and
look at one final set | 6,841.11 | 2.6 |
of examples that gives a sense, now, of
how we might, indeed, abstract better, | 6,843.71 | 5.419 |
and better, and better, or more,
and more, and more, in order | 6,849.129 | 2.541 |
to write, ultimately, better code. | 6,851.67 | 2.14 |
Let's go ahead, and in the spirit
of Scratch, do the following. | 6,853.81 | 3.05 |
>> Let me go ahead and include
CS50.h and standard IO.h. | 6,856.86 | 4.84 |
Let me go ahead and give
myself an int, main, void. | 6,861.7 | 2.31 |
And let me go ahead, call this cough.c. | 6,864.01 | 3.37 |
>> And let me go ahead and just
like Scratch, print out cough/n. | 6,867.38 | 8.13 |
And I want to do this three times. | 6,875.51 | 1.66 |
So I'm, of course, just going
to copy and paste three times. | 6,877.17 | 2.5 |
I'm now going to make
cough dot slash cough. | 6,879.67 | 6.77 |
Let's give myself a little more room
here, Enter, cough, cough, cough. | 6,886.44 | 3.68 |
>> There's, obviously, already an
opportunity for improvement. | 6,890.12 | 3.85 |
I've copied and pasted
a few times today. | 6,893.97 | 1.709 |
But that was only so I didn't
have to type as many characters. | 6,895.679 | 2.582 |
I still changed what
those lines of code are. | 6,898.261 | 1.989 |
>> These three lines are identical,
which feels lazy and indeed is, | 6,900.25 | 3.99 |
and is probably not the right approach. | 6,904.24 | 2.87 |
So with what ingredient
could we improve this code? | 6,907.11 | 3.919 |
We don't have to copy and paste code. | 6,911.029 | 1.541 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.