Functions are sections of code used within a larger program
to perform a particular task.
They are also known as procedures, subroutines, and subprograms,
since they really are just mini programs within programs.
Even though you might not have noticed,
you most definitely have used functions already if you've programmed in C.
When you write stuff to the screen using printf,
you're actually using a function called printf.
Likewise, GetInt and GetString are functions that CS50 provides for you.
As programmers, we use functions for a number of reasons.
>> Having functions to perform common tasks like writing to the screen
and prompting the user for input saves us time when programming.
It's not necessary to write code to do those same things
since they're already done for us.
It's not even necessary for us to understand exactly how those functions work internally.
Instead, we just have to know how to use or call them in our programs.
It's also helpful to write functions in larger programs
even if you're not planning on having others use them,
just to divide up the larger task at hand.
This way, you give structure to your code much like you'd give structure to an essay,
and with really large projects you enable multiple people
to work on different parts at the same time
since it's clear where one person's work ends and another's begins.
>> Functions typically operate on 1 or more inputs, known as arguments or parameters,
transforming those inputs into an output value that is then returned.
However, it's also possible to have functions that don't use any input arguments
and don't return an output.
Okay, enough with the fluffy stuff.
Let's take a look at some real functions in C.
>> Here we have a function called square that, believe it or not,
computes the square of the input.
Let's dissect the syntax of it.
The first word you see in the function, int,
specifies the return type of the function; that is, the C type of the function's output.
This function will output a value with type int.
The next word you see is the name of the function, square in this case.
Naming functions appropriately is particularly important
since it's probably the most visible documentation of what the function does.
It might be cool to name this function shazam or booyah,
but that wouldn't tell us anything about what the function does.
>> Following the function name come the inputs to the function, the arguments.
They are listed inside a set of parentheses
with both their type and a name to use for them within the body of the function.
Square takes just 1 argument, the int to square,
which I've decided to give the name x.
This ends the function declaration or prototype.
The curly braces surrounding the rest of the function
denote the beginning and end of the function body,
the definition of the function itself.
Square is a pretty simple function.
It's only got 1 line, since all we have to do is multiply x by itself.
The word return specifies the output value and triggers the end of the function.
To use the square function, we need to supply the appropriate inputs
and then capture the output.
Here, you see a couple of ways of doing that.
We can pass in a raw int, like 6, or we can pass in a variable like a.
Note that each time we call square,
we capture the output in a variable and then print it.
However, we don't have to store the return value in a variable and then print.
We could just send it directly to printf as we do when we square 10,
but this does mean that we don't have access to the return value of square 10
elsewhere in the code.
Let's look at another simple example.
>> This time, we'll sum 2 floats together so that we can see what a function looks like
when it's got 2 inputs instead of just 1.
As you can see, it's not much different.
All we do is add in the 2nd argument, a float named b,
to the argument list using a comma to separate it from the first argument, float a.
So, our square and sum functions are pretty easy to understand
because they're equivalent to functions that you've seen before in math class.
Now, let's look at a function that is slightly different.
Instead of returning a value, it modifies a state.
This is known as having a side effect.
In this program, the printf_fudd_style function
has a return type that we haven't seen before, void.
>> Void is used to say that a function doesn't return anything.
There are no variables of type void,
so if you try to return something from a function like printf_fudd_style,
the compiler will yell at you.
Inside printf_fudd_style, we see that while the function still performs a task,
namely, switching all Rs to Ws,
the point of the function is to modify the string and then print it out on the screen,
changing the state of the program instead of returning a result.
Using this function is very much like using our square and sum functions,
except we don't store the result in a variable or pass it off to another function
since there is no result to speak of.
So, when we compile and run our Elmer Fudd program,
you see that "You rascally rabbit!" gets transformed right to
"You wascally wabbit!"
>> Finally, there's 1 more function worth discussing
since you've been using it in every C program you've ever written, main.
Main is a function much like any other.
It's got a return type, a name, a list of arguments, and a body.
The return type of main is always an int,
and this int is used to communicate the status of the program when it finishes.
>> Did it run successfully, or did it end early because of an error?
Returning to zero indicates a successful run,
and returning any non-zero value indicates an error.
Depending on the kind of program you're writing,
returning different codes for different errors can be helpful
for people who use your program later on.
Mains arguments are a little more complicated.
>> The argument list can either be empty,
which we indicate by writing void in between the parentheses,
or the list can have 2 arguments: an int and an array of char*s.
These are used when your program intends to use
arguments passed in on the command line when the program is invoked.
The int argument is typically called argc,
and it's equal the length of the char* array, known as argv.
Argv contains the command line arguments passed into the program.
In another short, we'll talk about how to use these variables in more detail.
For now, you can feel free to declare main either way.
I typically choose to write it out the shorter way, with no arguments, when possible.
>> Now that you know how to write functions,
you're well on your way to developing larger, more complex programs.
My name is Nate Hardison. This is CS50.