305171 Computer Programming
Rattapoom Waranusast
Department of Electrical and Computer Engineering
Faculty of Engineering, Naresuan University
Structured programming enables
programmers to break complex systems into
manageable components.
In C, these components are known as
functions.
The most relevant structured programming
concepts are:
– Top-down design
– Code reusability
– Information hiding
2
In C, top-down design enables analysts and
programmers to define detailed statements
about a system’s specific tasks.
Top-down design experts argue that humans
are limited in their multitasking capabilities.
Those who excel at multi-tasking and enjoy
the chaos it brings are generally not
programmers.
Programmers are inclined to work on a
single problem with tedious detail.
3
To demonstrate top-down design, an ATM
design is used as an example. The following
steps demonstrate the top-down design
process.
1. Break the problem into small, manageable
components, starting from the top. In C, the
top component is the main() function from
which other components are called.
2. Identify all major components. For the ATM
example, assume there are 4 major
components:
•
•
•
•
Display balance
Deposit funds
Transfer funds
Withdraw funds
4
3. Decompose one major component at a time
and make it more manageable and less
complex.
4. The withdraw funds component can be
broken down into smaller pieces, such as
these:
•
•
•
•
•
•
Get available balance
Compare available balance to amount requested
Update customer’s account
Distribute approved funds
Reject request
Print receipt
5
5. Go even further with the decomposition
process and divide the “distribute approved
funds” component even smaller:
• Verify ATM funds exist
• Initiate mechanical processes
• Update bank records
6
ATM
Display
Balance
Get
Available
Balance
Deposit
Funds
Compare
Balance to
Request
Transfer
Funds
Withdraw
Funds
Update
Account
Disperse
Funds
Reject
Request
Verify
ATM Funds
Exist
Initiate
Mechanical
Process
Update
Bank
Records
Print
Receipt
7
In the world of C programming, code
reusability is implemented as functions.
Specifically, programmers create userdefined functions for problems that
generally need frequently used solutions.
Example: “Get Available Balance” in ATM
Example: printf() sqrt() strcpy()
8
Information hiding is a conceptual
process by which programmers conceal
implementation details into functions.
Functions can be seen as black boxes.
A black box is simply a component, logical
or physical, that performs a task. You
don't know how the black box performs
the task; you just simply know how to use
it.
9
Input
floating point
data
Output
sqrt()
square root
value
Input
Output
string
strcmp()
integer
string
Input
string
list of
parameters
Output
printf()
print to
screen
10
Function prototypes tell C how your
function will be built and used.
It is a common programming practice to
construct your function prototype before
the actual function is built.
Programmers must think about the
desired purpose of the function, how it
will receive input, and how and what it
will return.
11
int findMax(int, int);
return type
function name
list of parameters
Examples
float addTwoNumbers(int, int);
double myFunction(char, int, int);
void printBalance(int);
int createRandomNumber(void);
12
Function prototypes should be placed
outside the main() function and before
the main() function starts.
#include <stdio.h>
int addTwoNumbers(int, int);
int subtractTwoNumbers(int, int);
int main(void)
{
}
13
Function definitions implement the
function prototype.
The first line of the function definition
(also known as the header) resembles the
function prototype, with minor
exceptions.
return type
function name
parameters declarations
int addTwoNumbers(int operand1, int operand2)
{
int result = operand1 + operand2;
return result;
}
return value
14
#include <stdio.h>
int addTwoNumbers(int, int);
Function prototype
int main(void)
{
printf("Nothing happening in here.");
}
int addTwoNumbers(int operand1, int operand2)
{
return operand1 + operand2;
}
Function definition
15
1.
2.
3.
4.
C functions generally adhere to the following rules:
Every function must have a name.
Function names are made up and assigned by the
programmer (you!) following the same rules that
apply to naming variables.
All function names have one set of parentheses
immediately following them. This helps you (and C)
differentiate them from variables. The parentheses
might or might not contain something.
The body of each function, starting immediately
after the closing parenthesis of the function name,
must be enclosed by braces. This means that a block
containing one or more statements makes up the
body of each function.
16
It’s now time to put your functions to
work with function calls.
You work with your user-defined
functions the same way you work with
other C library functions such as printf()
and scanf().
17
#include <stdio.h>
int addTwoNumbers(int, int);
int main(void)
{
int iResult;
iResult = addTwoNumbers(4,5);
printf(“%d”, iResult);
}
int addTwoNumbers(int operand1, int operand2)
{
return operand1 + operand2;
}
18
Exercise 1: Function call
1. Start and prepare to run a new project in
VC++ Express
2. Copy the code from addTwoNumbers.c and
paste into the code editor.
3. Run the code and observe the result.
4. Let’s play with the code.
5. Modify the code to
1. Subtract, multiply, or divide numbers
2. Receive more arguments
19
Function calls can also be placed in other
functions. To demonstrate, study the next
block of code that uses the same
addTwoNumbers() function call inside a
printf() function.
20
#include <stdio.h>
int addTwoNumbers(int, int);
int main(void)
{
printf(“%d”, addTwoNumbers(4,5));
}
int addTwoNumbers(int operand1, int operand2)
{
return operand1 + operand2;
}
21
Exercise 2: Function calls 2
1. Start and prepare to run a new project in
VC++ Express
2. Copy the code from functions.c and paste
into the code editor.
3. Run the code and observe the result.
4. Let’s play with the code.
22
Variable scope identifies and determines
the life span of any variable in any
programming language.
When a variable loses its scope, it means
its data value is lost.
The two common types of variables
scopes in C are local and global.
23
Local variables are defined in functions,
such as the main() function, and lose their
scope each time the function is executed.
#include <stdio.h>
void main(void)
{
int num = 5;
printf(“%d”, num);
}
24
Because local scope variables are tied to
their originating functions, you can reuse
variable names in other functions without
running the risk of overwriting data.
25
Exercise 3: Local variables
1. Start and prepare to run a new project in
VC++ Express
2. Copy the code from local.c and paste into
the code editor.
3. Run the code and observe the result.
4. Let’s play with the code.
26
Locally scoped variables can be reused in
other functions without harming one
another’s contents.
At times, however, you might want to share
data between and across functions.
To support the concept of sharing data, you
can create and use global variables.
Global variables are created and defined
outside any function, including the main()
function.
27
Exercise 4: Global variables
1. Start and prepare to run a new project in
VC++ Express
2. Copy the code from global.c and paste into
the code editor.
3. Run the code and observe the result.
4. Let’s play with the code.
28
It is not wise, however, to use global
variables liberally as they can be error
prone and deviate from protecting data.
Using global variables allows all functions
in a program file to have access to the
same data, which goes against the
concept of information hiding.
29
Be careful about creating local variables
with the same name in the same
function.
If you define a local variable early in a
function and then define another local
variable with the same name inside a new
block, C uses only the innermost variable,
until its block ends.
30
Exercise 5: Nested local variables
1. Start and prepare to run a new project in
VC++ Express
2. Copy the code from nested_local.c and
paste into the code editor.
3. Run the code and observe the result.
4. Let’s play with the code.
31
The terms automatic and static describe
what happens to local variables when a
function returns to the calling procedure.
By default, all local variables are
automatic, meaning that they are erased
when their function ends.
To declare a variable as an automatic
variable, prefix its definition with the
term auto. The auto keyword is optional
with local variables because they are
automatic by default.
32
Local static variables do not lose their
values when their function ends. They
remain local to that function.
When the function is called after the first
time, the static variable's value is still in
place.
You declare a static variable by placing
the static keyword before the variable's
definition.
33
Exercise 6: Static variables
1. Start and prepare to run a new project in
VC++ Express
2. Copy the code from static.c and paste into
the code editor.
3. Run the code and observe the result.
4. Let’s play with the code.
34
© Copyright 2026 Paperzz