- The C library is brimming with functions.
These functions do a lot of things, but perhaps
not everything you need for your programs.
Therefore, when necessary, you can
concoct your own C language functions.
In fact, most programs have dozens,
if not hundreds, of their own functions.
In this movie, I introduce you to the function.
You'll see how to set up and prototype a function,
how to call or access the function,
and how to bail out of a function early.
Like everything in the C language, a function
has a certain style, and various etiquettes must
be followed to add a function to your code.
A function has a type, a name and arguments in parentheses.
The type is a value returned by the function, its output.
The name is a unique name that identifies the function.
The arguments are zero, one or more values
passed to the function, its input.
Here, the alpha function has no input
or output, which is valid in the C language.
Therefore, it's a void function.
Void is a variable type, which means
I have no variable type.
Function count is an integer function.
It generates, or returns, integer values.
It requires no input and, therefore, has no arguments.
Function hangUp returns no values, so it's a void function.
It does, however, accept a single character as input.
The character is an argument,
and it's referenced by variable ch.
Before you can use a function,
it must be introduced to the compiler.
That way, the compiler can check its arguments
to ensure that it's being used properly.
Functions are called by their name,
and then parentheses.
When a function is called, control passes
to the function's statements.
Then, when the function is over, flow continues
with a statement after the function call.
This code contains two functions.
The main function is required in every C program.
You also see the blorf function.
Line three prototypes the blorf function.
This informs the compiler
of the function type and arguments.
The compiler can confirm that the function
is being used properly by checking the prototype.
Also, the prototype is a statement.
It ends in a semicolon.
Line eight calls the function.
It has no arguments, so the parentheses are empty.
The function itself is found at line 14.
This is, effectively, a repeat
of the prototype, minus the semicolon.
The statements belonging to the function
are enclosed in curly brackets.
Build and Run the code.
To call the function again, you simply specify it again.
Duplicate line eight to line nine.
Save.
Build and Run, and the output appears twice.
The prototype works like a definition for the function,
describing how to use it before it appears in the code.
You can avoid specifying a prototype, but only
when you write the function before it's used.
To see an example, open exercise file function2.
The soup function appears above the main function.
That's okay, because the compiler still sees the
main function in there somewhere, and it's always
the first function run in the program.
What's missing here is the prototype for the soup function.
Build and Run.
And, the code outputs text, as expected, but try this.
Move the soup function after the main function.
Save.
Just Build.
On my screen, I see one warning.
Your compiler may report the misdeeds in a different manner.
Either way, it's not good.
You have to add the prototype.
So, I'm going to insert a line above the main function.
Void, soup, (void) and a semicolon.
Save.
Now, Build and Run.
No errors, and the output is the same as it originally was.
Open exercise function3.
In this code, the cheers function
contains its own variable, x at line 18.
This variable is used only in the function,
here, to repeat the string three times.
Build and Run the code.
You cannot reference variable x outside of its function.
To prove it, insert a line
above line 11 that displays its value.
Save the change and Build.
Don't even bother trying to run it because you won't.
The compiler is basically saying, "What the heck is x?"
Try to define x and set its value inside the main function.
Save.
Build and Run.
The value of x in the main function is unaffected
by the x-variable in the cheers function.
They are two different variables.
Here's another thing to remember.
A function ends its run when its last statement is executed.
In the cheers function, that's a putchar at line 26.
You can bail out of a function early
by using the return keyword.
Type return; at line 26.
You don't need to specify a value here,
because the cheers function is a void function.
It doesn't return anything.
In the main function, return must specify a value
as it does at line 17, because main is an int function.
Save the changes, Build and Run.
The output is messy
because the putchar statement is skipped.
That code is never executed because
of the return statement right above it.
More often, you'll see a function return early
based on a condition, which usually
involves an if statement or a loop.
Otherwise, anywhere the return is found marks the spot
where the function bails out, and control
returns to the calling function.
Or, in the case of the main function, control
is returned back to the operating system.