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

subtitle

The document introduces the concept of functions in programming, emphasizing their importance for code reuse and encapsulation. Functions allow programmers to structure code into manageable pieces, similar to paragraphs in writing, which enhances clarity and testability. Key aspects of functions include their syntax, purpose of doing one thing well, and the significance of preconditions and postconditions for expected outputs.

Uploaded by

spiritgnx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views

subtitle

The document introduces the concept of functions in programming, emphasizing their importance for code reuse and encapsulation. Functions allow programmers to structure code into manageable pieces, similar to paragraphs in writing, which enhances clarity and testability. Key aspects of functions include their syntax, purpose of doing one thing well, and the significance of preconditions and postconditions for expected outputs.

Uploaded by

spiritgnx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

Now, we're going to start one of the most important topics in this whole course,

and that's Functions. A reference if you're


following the textbook is Chapter 5 especially 5.1. So here's a quote. "A paragraph
of
coding is a function, Dr. P." We already have
used functions all along. We started with our basic
beginning program with the function in main (void). Inside that function,
which was function with an open brace was the start
of the executable program. We've also used standard IO which had functions
like printf and scanf, and it was again, an integer function printf and it could
have many arguments, actually a variable
number of arguments. We've also used the pseudo random number
generator function in rand and others. Why are functions so important? They are in
effect the paragraph. If you can write a paragraph, you can write an essay. If you
can write an essay, you can write a book. So structurally, the
paragraph is a big jump. It takes you from the single correct sentence to a
coherent set of thoughts. The same is true for function. It takes you from a
single statement, understanding that statement like an assignment statement. Or
like a call on a
function such as printf, and putting it together to
achieve some coherent purpose. Functions are important because they are crucial to
the reuse of code. They're especially crucial
if they exist in libraries. Libraries like standardio.h,
standardlive.h, these are universally
useful libraries, and we don't have
to rewrite them. So we have to understand
how to use them. Indeed to be good coder, we have to know how we should be writing
code that's reusable. They're a fundamental
encapsulation technique. When you have a
big piece of code, and nowadays code can be in the tens or even
hundreds of millions of lines to produce a big system, you don't start on
that all by itself. You break it up into
smaller and smaller pieces, that's coding at small
pieces encapsulation. A function is a form
of encapsulation. It allows you to structure
a big piece of code. You structured into a
bunch of functions, functions call other functions, and then finally you have
the overall function main. Here are some things
you should think about when writing functions. A function largely should be
able to do one thing well. Think of rand, think of printf, think of square root.
This do one thing, they don't move multiple things. You don't have a
function that does both square roots and cube root. You keep those things separate.
Functions were unwritten should stay with one page of code. Why? Because that's
like a paragraph. It's easy to understand, and it's easy to test. So a function is
a unit as you can manage to test effectively, and sometimes we want what are called
preconditions
and postconditions. If we have the right input, we should get the
expected output, and that should be something
we can properly test. Here's our syntax for a function. We have a types such
as int or void. We have a function name, and the function name is a form of
documentation like printf, or rand, or main. Then we have a parameter list. This
can be an arbitrary
comma separated list. These are the
parameters passed in, and you've seen that with
for example, printf. Then you have the
body of the function, which is typically this bracing
close what we call block. The block starts with a bunch
of declarations if needed, and then a bunch of
executable statements. So here's an example,
simple example. Void write address, void. So returns nothing. It needs no
arguments, and we're going to
say printf clause, new line printf north pole. Send a clauses
address, close brace. So now, we've
encapsulated this idea and we can use it
wherever we need it. This is a function definition. It allows us to
call the function. How will we call the function? We would just say
write_address(); and one could imagine this
being called inside main, and then this would execute
those two printf's. As I said, it acts
as a paragraph. The code is needed
repeatedly, it abbreviates, makes it easier to test, and makes it easier to
understand big pieces of code. We're going to go on from here, and show in our
coding Window several functions
and how they work.

You might also like