0% found this document useful (0 votes)
2 views

functions

The document explains how to write a simple Python program using functions, specifically focusing on the built-in print function and how to define custom functions. It demonstrates creating a program called hello.py that prints 'Hello, world!' and 'This is CS50P' by defining a main function and calling it. The document emphasizes the importance of functions in programming as they take inputs and produce outputs.

Uploaded by

a.bruce.ed
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

functions

The document explains how to write a simple Python program using functions, specifically focusing on the built-in print function and how to define custom functions. It demonstrates creating a program called hello.py that prints 'Hello, world!' and 'This is CS50P' by defining a main function and calling it. The document emphasizes the importance of functions in programming as they take inputs and produce outputs.

Uploaded by

a.bruce.ed
Copyright
© © All Rights Reserved
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

[AUDIO LOGO] SPEAKER: So in a moment, we're going to write a very simple program.

And while we do, we're going to think about what a program actually is at the end
of the day.

So we've seen so far that we have this language called Python. And we can use
Python to write programs. And Python itself has all these tools at our disposal to
actually write those programs with. So among them are things like conditionals,
variables, loops, and in this case, importantly, functions, where a function is
some piece of code that takes an input and produces some output. You can think of
it like a verb that you can actually run inside of your own program.

So here, I'll go ahead and make a program called hello.py by typing code hello dot
py. And I'll hit Enter. And I'll get this blank Python file. I know because it ends
with .py here.

And I want to call, or use, my very first function. And one function built into
Python is called the print function, where the print function takes some text as
input and produces as output this text in my terminal down below.

So I'll type print here. And I'll follow it with parentheses, just like this. And
I'll type "Hello, world!" like that. So now I've used, or called, my very first
function, print. And maybe afterwards, I'll also type print. Maybe this is CS50P,
like this. So now I have a very simple program. And I've, in this case, used two
functions.

Now, if you're new, you might be asking, how do I know that these are functions?
Well, it just so happens that functions have these identifiers you can use to
determine when you're actually looking at a function.

Now, one of these identifiers are these parentheses here. Notice how I called the
name of the function, print. And I followed it with parentheses, open and closed
here. So when you use, or call, functions, you're often going to type the name of
the function followed by parentheses, and then some input.

Some functions might not even take any input. You can call them a bit like this--
print, open and closed parentheses. And that would mean this function doesn't take
any input or, in this case, I haven't given it any input on line 3. But I have, of
course, on lines 1 and 2.

So to give you an idea of what these functions will do, let me go ahead and run the
program. I'll type python hello.py down below to run this program top to bottom so
that as-- first, we'll see the print function will run on line 1. And then on line
2, the second print function will run. So I'll hit Enter here. And I should see
"Hello, world! This is CS50P" from top to bottom.

So we've used these functions. But as a programmer, you might also want to create
your own functions. And in particular, you might want to write programs that are
composed of individual functions.

Now, to define your own function-- that is, to tell us what code should run when I
call or use the function-- you can use the keyword called def, D-E-F, where def
stands for "define." So maybe because this is the main function-- I'm about to
write the main function in my program-- I could just call it main, def main, like
this. And I want to include an open parentheses and a closed parentheses, followed
by a colon. And that then says I have a new function that I've defined called main
that, in this case, won't take any particular inputs.

But now if I want to tell Python or my program in particular what code should be
run when I use this function, I need to indent those lines of code and put it
inside this definition here. So why don't I take these two, these two prints, and
put it inside of my function called main? I'll indent both of them and move this
back up to the top. And now I have defined, again, a function called main that
doesn't take any particular input. But it does run these two functions-- first the
print on line 2, and then the print on line 3.

So now that you've seen how to define a function, you could think about how or who
defined the print function for us because right now, we're saying print "Hello,
world!" But what does it mean to print? What goes into printing something to the
terminal?

Well, it just so happens that the Python developers defined what happens in the
print function elsewhere in the Python library, which you'll learn more about as we
go through the course. But for now, you can just know that you can use this
function, print. But somewhere else, somebody before you has defined it and allowed
you to use it in this context, just like I've now defined a function called main
here.

So let me go and run this program now. I'll say python hello.py. Hit Enter. I don't
see anything, which is maybe surprising if you're new because here, I said I'm
going to define a function called main. And within that function, I want to do the
following. I want to print "Hello, world!" and then print, "This is CS50P." But in
Python, defining a function is different from running it, or this technical term
called calling it.

So I've defined my function called main. But I haven't called main. And by
convention, I can do that using two new lines underneath my definition here and
then calling main. So I'm going to now run whatever I've defined as part of my main
function here. I'll type python hello.py again, hit Enter, and now I should see
"Hello, world! This is CS50P."

Because this program is read top to bottom, it will first define a function called
main. It will then say what functions compose that function, which functions should
be run when I call this function. And then down below, I finally actually call it
on line 6.

And so now in this case, you've seen that my entire program is really a function.
As you go off and build new and better programs, you'll see that a lot of what
you're doing is just writing things that take inputs and produce outputs. And those
things are called functions.

You might also like