Hello and welcome.
We're starting a new section.
Our section is based on functions.
So we're going to talk about some of the basics of functions first.
A function is a self-contained unit of program code designed
to accomplish a particular task, that's a high level definition.
Function is going to be basically something that you can invoke that
helps you reduce duplication of code.
But what it does is it's self-contained inside the code
itself, and it has just some tasks that it's going to accomplish.
Syntax rules are going to define the structure of a
function and how it can be used.
So depending on what programming language you use, functions
may be defined differently.
For c, we're going to talk about specific rules for c.
Most programming languages support this concept of a function, and
I’ll tell you the advantages and why they're supported.
A function in c is the same as subroutines or procedures in
other programming languages.
So oftentimes you hear these terms subroutines and procedures,
same thing as a function.
Self-contained unit that executes some code, that you can call.
Some functions cause an action to take place.
So we're familiar with a couple functions up to this point.
We've studied the printf function which displays output
to your screen that takes an action, does something for you.
And then result we've also looked at the scanf function which
takes input from the keyboard.
Again, it sort of does another action.
Other functions though can actually just find a
value for a program to use.
So for example when we study strings and character arrays, we're going
to look at functions for string manipulation and changing things.
There's a function in particular called stir length and that
will tell a programmer how long a certain string is.
It doesn't perform an action it just gives information back to you.
So functions are going to perform tasks depending on the task what
you need to accomplish in your program, it's either going to cause
an action or it's going to modify a program value or return a value or
tell you some status information.
Advantages of functions.
We're going to use functions because have a ton of advantages.
Functions allow for the divide and conquer strategy.
So if you were to write down and go and write a medium-sized program
or a large program, it's going to be really, really hard to maintain.
It's going to be hard to test, debug and find and fix any errors
if you have a single large function.
So breaking down and doing this dividing conquer strategy is
going to make it a lot easier to find and fix bugs in your code.
And can you imagine a single c program that all I had was the main function
and there was 5000 lines in it.
We would have no clue how to attack that problem.
It's really, really poor organization.
You're not having any cohesion and you're just kind of diving right
in there to try to solve a problem.
So we're going to use functions.
Divide and conquer basically means that tasks can be divided into
several independent subtasks.
It reduces the overall complexity of the code.
You can create separate functions for each subtask, and then you can
even create more functions to divide the subtask into smaller subtasks.
Each time you create these additional functions, it reduces the complexity.
Your goal should be when you're creating these functions and
using the divide and conquer strategy is to make your
functions as cohesive as possible.
And what that means is make sure each function is doing just one
task, don't have a function do 5 different tasks or 10 different tasks.
If you write a function that reads input from the keyboard, have it
read input from the keyboard, don't have it check a string or display
output or anything like that.
Functions need to be small, and they need to be very cohesive.
But the divide and conquer strategy is really nice because
it reduces that complexity.
Instead of having a main function with 5000 lines of code, you can
have many, many functions that are doing specific tasks and you
can identify them very quickly.
So functions are going to reduce the duplication of code.
And when you reduce duplication of code, it's going to save
you time when writing, testing and debugging that code.
It's also going to re reduce the size of the source code.
And why do functions reduce duplication of code because if you
have a task instead of doing that task over and over and over again and
writing the same code over and over and over again, you encapsulate that
task into a function and you just invoke the function, and the function
will do it for you each time, you don't have any duplication there.
If you have to do a certain task several times in a program, you
only need to write an appropriate function once, just like I
mentioned.Yyou don't need to write that code multiple times.
So a program can then use that function whenever needed.
And you can also use the same function in different programs, and
that's what constitutes a library.
We've been doing that, we've been using the printf
function, which is from the standard input output library.
And that's only written once, and we invoke it over and over and over.
If that function didn't exist, we'd have to take the implementation
of that function and put it in our program and just keep repeating it.
So functions can save a lot of time and they can reduce
your source code and you can reuse them over and over again.
So it's also a software reuse technique.
It also helps with readability.
When you modulize your program which is what you're doing when you
create these separate functions to perform specific tasks, the program
becomes much better organized.
Like I mentioned, if you had a main routine, a main function and it had
tons of code in it, it's going to be much harder to follow than a main
routine that calls 5 functions and those functions do specific tasks.
So it helps with program organization.
It helps with program maintenance.
It's much easier to read and easier to change or fix any source code.
And remember, the maintenance phase is the most expensive phase
of the software lifecycle, this is where you're fixing your bugs,
the program has been released and where you're adding new features.
The better organized your program is, the easier it
is going to be to maintain.
So this organization, this readability, this being able
to follow what's going on in your source code very important
and functions help a ton.
The divide and conquer approach also allows parts of the
program to be developed, tested and debugged independently.
When you get out there in the real world and you start working on
code, you're going to notice that programs are really, really big.
And there's many people working on these programs,
hundreds maybe even thousands.
When you have all these people work on a program, it's much easier to
work on that program if you if you do this dividing conquer approach
if you have small tasks that people do and it's independent.
You're not touching the same source code and you can go ahead
and implement tasks much easier.
And when you do this and you have multiple people working on a project,
it can reduce the overall development time which again reduces maintenance
costs, reduces development costs.
The functions can also be reused which again will reduce time.
And again we do that with the printf function, reuse that.
But you when you write your own user defined functions, you can reuse them
over and over and over in the program.
So software reuse is a big, big advantage of functions.
When you're thinking about these functions.
The other nice thing about designing your program on functions is you
want to think of it as a black box.
What it means when you when you think of it as a black box, all
you're thinking about is the input to the function and the output.
And when you focus just on the input in the output, it allows you
to look at the design as a whole before you start implementing.
And so functions in general will get you in this type of thinking
where you can focus on design.
And I mentioned design is important.
Design is when you start planning out your program, how you're
going to organize it, how you're going to write the code.
And it's important to do this planning before you even start implementation
which is writing the code.
And by thinking about functions and thinking about just inputs and
outputs, you can think about how the program is going to be organized and
what functions you need to create.
So you don't have to worry about just implementation in the details.
And this is important because the first thing you want to do before
writing code is just to think about design, you don't want to
start thinking about implementing.
A lot of issues that students have when they write code
for the first time is they just start implementation.
They don't even think about the problem, they don't think how
they're going to solve the problem, they just start writing code.
Never a good approach.
When you're thinking about writing code and functions, think about
it as just inputs and outputs of the function and it's going
to help you with your design.
So let's look at some examples.
We have used built-in functions such as printf and scanf, as I mentioned.
Those are in the standard input output library provided
by the c programming language.
So we're familiar somewhat with what a function is because we've used it,
but we haven't gone through the fine grained details of how you define
a function or how you actually pass data to a function, nothing like that.
But with the printf function, you should have noticed
that it does take data.
You can pass data to the function.
The data is the arguments between the parentheses
following the function name.
We're going to have a whole other lecture on arguments and
parameters to explain them more.
But at a high level understand that you can pass data to a function.
And understand that by looking at the printf function.
The first argument to the printf function which is the data that
we're passing to it was a string literal, it was all that all those
characters inside of double quotes.
And then we also had arguments which there could have been 0 or many there
were variable arguments, which were a series of variables or expressions
whose values were to be displayed.
So remember, if we had any format specifiers in the first argument
we would then have the variables in this in the subsequent arguments.
So were you able to pass data to the printf function.
You also should have noticed how you can receive information back from a
function, data can be returned to you.
In the scanf function, one of the arguments was how you
were going to store the data.
And so you would pass it a variable and that variable
would have a value whatever the user typed in the keyboard.
So that's can kind of thought of as a return value in a function.
So functions are very valuable because they can take data into them and then
they can return data out of them.
So a coding example of a function and this would just be a function
that we're going to invoke, but this is the main function.
We're familiar with the main functions, the entry
point of the program.
But look at how nice this is.
We're calling functions read list sort and average.
We only have 3 lines of code.
We have to go and implement those functions, but it makes the program
more organized because we immediately know what this program is doing.
This program is going to read a list, it's then going to
sort a list, it's then going to display the average of the list,
and the list contains floats.
So it's very readable, it's going to be very easy to maintain.
If we have any bugs in the program, we'll immediately know
how to find or isolate that bug.
If the bug isn't sorting, we can go to the sort function.
If the bug is in the averaging, we can go to the average function,
very easy to isolate errors.
So when we implement those functions in the previous slide,
we were just calling them.
To actually implement functions, you have to write the code
inside the body of the function.
You can either utilize functions that are implemented by the c language
like printf or you can create your own user defined functions.
You do not have to implement the printf functions or any of the
functions in the library, you only implement your own functions.
So in this previous example with the read list sort and the average,
we would have to go and we would have to implement that function.
We'd have to provide a function header, a function body that
actually does the reading of the list, the sorting of the
list and averaging of the list.
We're not done just by defining functions, we have to implement them.
And when you implement the functions, you always want to
use descriptive function names to make it clear what the program
does and how it is organized.
In the previous example, we had a function named read list, pretty clear
what that's doing, function name sort.
We didn't name the function x y or z.
We described that function.
It's the same thing when you're describing or creating
variables, meaningful names.
And also if you make these functions where they're generic enough where
they're actually doing something and they're taking generic data,
you would actually be able to reuse them in other programs,
just like a library function.
Again, supporting software reuse.
So in these upcoming lectures, we're going to talk about how
we can define a function,, the specific syntax, how we can invoke
and call a function and how we can pass and return data from them.
That's what we're going to focus on in the upcoming lectures.
I also wanted to mention that our main routine that we've been
seeing over and over in every c program, that's a function.
The main function is a specially recognized name in the c
system, it indicates where the program is to begin execution.
All c programs always have a main.
The main function can take data when you pass to it, the
command line arguments or it doesn't have to take data.
It also has optional data that it can return.
You'll notice that we use main functions that return 0.
Usually, that's used when you want to return error codes.
But we've used specific functions already and the most important
function is that main function and that again helps organize the code.
We always know where the program is starting because
we just have to find the main.
This is a high level description of functions kind of identifying
the advantages of functions and what exactly a function is.
Now we want to talk about implementation.
Thank you.