Introduction to Computing
Lecture 09:
Functions (Part II)
[Link]. Nükhet ÖZBEK
Ege University
Department of Electrical & Electronics Engineering
[Link]@[Link]
Topics
• Structured Programming
• Scope
• Prototypes
• C Library Functions
Structured Programming
• Individual program tasks are performed by
independent sections of program code
• Advantages
– Easier to write, because complex
programming problems are broken into a
number of smaller, simpler tasks. Each task is
performed by a function in which code and
variables are isolated from the rest of the
program
Structured Programming
• Advantages
– It's easier to debug a structured program. If
your program has a bug (something that
causes it to work improperly), a structured
design makes it easy to isolate the problem to
a specific section of code (a specific function).
Structured Programming
• Advantages
– Time saving: You can easily use a function
you wrote in another program that needs to
do the same task. Even if the new program
needs to do a slightly different task, you can
modify the function, that is easier than writing
a new one from scratch: Consider the two
functions printf() and scanf(). If your
functions have been created to perform a
single task, using them in other programs is
much easier
Planning a Structured Program
• A program to manage name and address
list
– Enter new names and addresses
– Modify existing entries
– Sort entries by last name
– Print mailing labels
Planning a Structured Program
• Enter new names and addresses
– Read the existing address list from disk
– Prompt the user for one or more new entries
– Add the new data to the list
– Save the updated list to disk
• Modify existing entries
– Read the existing address list from disk
– Modify one or more entries.
– Save the updated list to disk
Structured Programming “Do”s and
“Don’t”s
• DO plan before starting to code. By
determining your program's structure
ahead of time, you can save time writing
the code and debugging it
• DON'T try to do everything in one
function. A single function should perform
a single task, such as reading information
from a file
main()
• main() is also a function
• When you return a value in main, it is
passed to the operating system
• 0 indicates that the program terminated
normally
• All other values indicate some error
• It is a good idea and it is required in ANSI
C to return a value in main
Scope
• Variables declared in a function body
(including formal parameters) are only
accessible whilst the function is executing
• In fact, this is true of any block in a
program
Example: scope.c
int globalVar = 0;
int increment ( int paramVar )
{
int localVar = 2;
globalVar += localVar;
return (paramVar + localVar);
}
int main()
{
int localVar = 5;
for (localVar=0; localVar < 5; localVar++)
{
int blockVar = 1;
blockVar = increment(blockVar);
}
printf("%d\n", globalVar);
printf("%d\n", blockVar); /* Error! Out of scope. */
return 0;
}
Example: scope.c
int globalVar = 0; Global variable declaration
int increment ( int paramVar )
{
int localVar = 2;
globalVar += localVar;
return (paramVar + localVar);
}
int main()
{
int localVar = 5;
for (localVar=0; localVar < 5; localVar++)
{
int blockVar = 1;
blockVar = increment(blockVar);
}
printf("%d\n", globalVar);
printf("%d\n", blockVar); /* Error! Out of scope. */
return 0;
}
Example: scope.c
int globalVar = 0;
int increment ( int paramVar )
{
int localVar = 2; Variables local to
globalVar += localVar; this block.
return (paramVar + localVar);
}
int main()
{
int localVar = 5;
for (localVar=0; localVar < 5; localVar++)
{
int blockVar = 1;
blockVar = increment(blockVar);
}
printf("%d\n", globalVar);
printf("%d\n", blockVar); /* Error! Out of scope. */
return 0;
}
Example: scope.c
int globalVar = 0;
int increment ( int paramVar )
{
int localVar = 2;
Scope of the
globalVar += localVar;
return (paramVar + localVar);
variable localVar.
}
int main()
{
int localVar = 5;
for (localVar=0; localVar < 5; localVar++)
{
int blockVar = 1;
blockVar = increment(blockVar);
}
printf("%d\n", globalVar);
printf("%d\n", blockVar); /* Error! Out of scope. */
return 0;
}
Example: scope.c
int globalVar = 0;
int increment ( int paramVar )
{
int localVar = 2;
globalVar += localVar;
return (paramVar + localVar);
}
int main()
{
int localVar = 5;
for (localVar=0; localVar < 5; localVar++) Scope of
{ the variable
int blockVar = 1;
blockVar = increment(blockVar); blockVar.
}
printf("%d\n", globalVar);
printf("%d\n", blockVar); /* Error! Out of scope. */
return 0;
}
Variable Scope – “Do”s and
“Don’t”s
• DO use local variables for items such as
loop counters
• DON'T use global variables if they aren't
needed by a majority of the program's
functions
• DO use local variables to isolate the
values the variables contain from the rest
of the program
Prototyping of Functions
• Must declare functions before use (like
variables)
• Declaration is called a “prototype”
• Specifies the name, parameters and return
type of the function, without the function
body.
Prototype Example
#include <stdio.h>
int imax(int,int);
int main(void)
{
printf("The maximum of %d and %d is %d.\n", 3, 5, imax(3,5));
return 0;
}
int imax(int n, int m) {
int max;
if (n > m) max = n;
else max = m;
return max;
}
Prototyping of Functions
• Why use prototypes?
– They enable the compiler to catch many
errors, or oversights, you might make
using a function
Example: Continuing Fraction
Given n and terms, compute:
1
y= 1 +
1 + 1
1 + ... 1
1 + n
Example: Continuing Fraction
Example: If n is 2, and terms is 1:
1
y= 1 +
1 + n
1
= 1 +
1 + 2
1.33333
Example: Continuing Fraction
Given n and terms, compute:
1
y= 1 +
1 + 1
1 + ... 1
1 + n
Fraction(n, terms)
Example: Continuing Fraction
1
1 + 1
1 + ... 1
1 + n
Sum(n, terms)
Example: Continuing Fraction
1 + 1
1 + ... 1
1 + n
Fraction(n, terms - 1)
Example: Continuing Fraction
Last term:
1
1 + n
Sum(n, 1)