Escolar Documentos
Profissional Documentos
Cultura Documentos
CS 101: Introduction to
C
Computing
ti
Topics to be covered
Flowchart
Flo chart basic idea
Conditional statements
Iteration and loops
Functions
2/14/2013
Functions in C
Example
Guess what the output is
#include <stdio.h>
printNewLine()
i tN Li () {
C programming is easy
But you still fail!
But,
printf(\n);
We have used a function in the program.
The functions name is printNewLine().
This function serves to display a new line
}
main() {
2/14/2013
Example
#include <stdio.h>
printNewLine() {
printf(\n);
}
main() {
printf(C programming is easy);
printNewLine();
printf(But,
i tf(B t you still
till fail!);
f il!)
}
Why Function
Divide and conquer
Manageable program development
Software reusability
Use existing functions as building blocks for new programs
Abstraction - hide internal details (library functions)
2/14/2013
Function Definition
Format
return-value-type function-name( parameter-list )
{
declarations and statements
}
Function Definition
Format
return-value-type function-name( parameter-list )
{
declarations and statements
}
Returning control
If nothing returned
return;
or, until reaches right brace
If something returned
return expression;
2/14/2013
Function Prototypes
Function prototype
Function
F
ti name
Parameters what the function takes in
Return type data type function returns (default int)
Prototype only needed if function definition comes after
use in program
The function with the prototype
p
yp
int maximum( int, int, int );
Takes in 3 ints
Returns an int
Example
#include <stdio.h>
Void printNewLine();
Function prototype
main() {
printf(C programming is easy);
printNewLine();
Function call
Function Definition or
Function Implementation
2/14/2013
Header Files
Header files
C
Contain
t i function
f ti prototypes
t t
for
f library
lib
functions
f ti
<stdlib.h> , <math.h> , etc
Load with #include <filename>
#include <math.h>
Function Calling
Call by value
Copy
C
off argumentt passedd to
t function
f ti
Changes in function do not effect original
Use when function does not need to modify argument
Avoids accidental changes
Call by reference
Passes original argument
Changes in function effect original
Only used with trusted functions
2/14/2013
Storage Classes
Storage class specifiers
St
Storage duration
d ti how
h long
l
an object
bj t exists
i t in
i
memory
Scope where object can be referenced in program
Linkage specifies the files in which an identifier is
known
Storage Classes
Automatic storage
Obj
Objectt created
t d andd destroyed
d t
d within
ithi its
it block
bl k
auto: default for local variables
auto double x, y;
register: tries to put variable into high-speed registers
Can only be used for automatic variables
register int counter = 1;
2/14/2013
Storage Classes
Static storage
Variables exist for entire program execution
Default value of zero
static: local variables defined in functions
Keep value after function ends
Only known in their own function
Scope Rules
File scope
Identifier defined outside function, known in all functions
Used for global variables, function definitions, function
prototypes
2/14/2013
Scope Rules
Scope Rules
Block scope
Id
Identifier
tifi declared
d l d inside
i id a block
bl k
Block scope begins at declaration, ends at right brace
Used for variables, function parameters (local variables of
function)
Outer blocks "hidden" from inner blocks if there is a
variable with the same name in the inner block
2/14/2013
Scope Rules
Function scope
Can only be referenced inside a function body
Used only for labels with goto statements (start:, case: ,
etc.)
10
2/14/2013
Scope Rules
11
2/14/2013
Recursion
C offers two approaches to repetitive programming
loops and recursion
We define recursion when a function calls itself
2.
3.
In general:
if (stopping case)
solve it
else
reduce the problem using recursion
12
2/14/2013
Four Criteria
1.
2
2.
E h recursive
Each
i call
ll solves
l
an identical,
id ti l but
b t smaller,
ll problem
bl
A recursive function solves a problem by solving another problem that is
identical in nature but smaller in size
3.
A test for the base case enables the recursive calls to stop
There must be a case of the problem (known as base case or stopping case)
that is handled differently from the other cases (without recursively calling
itself)
In
I th
the base
b
case, the
th recursive
i calls
ll stop
t andd the
th problem
bl is
i solved
l d directly
di tl
4.
13
2/14/2013
Iterative Definition in C:
fval = 1;
for (i = n; i >= 1; ii--))
fval = fval * i;
14
2/14/2013
if n = 0
if n > 0
int fact(int n)
{
if (n ==0)
return (1);
else
return (n * fact(n-1));
}
This fact function satisfies the four criteria of a recursive
solution
15
2/14/2013
Recursive Factorial
Review
All recursive functions have two critical elements:
The recursive function call either solves one part of the problem OR
the call reduces the size of the problem
16
2/14/2013
Review
1.
2
2.
3.
17
2/14/2013
18
2/14/2013
Draw an arrow from the statement that initiates the recursive process
to the first box
Then draw an arrow to a new box created after a recursive call,
put a label on that arrow
5.
On exiting a function, cross off the current box and follow its arrow
back to the box that called the function
This box becomes the current box
Substitute the value returned by the just-terminated function call
into the appropriate item in the current box
Continue the execution from the returned point
19
2/14/2013
20
2/14/2013
21
2/14/2013
Recursive Solution
Observation:
All of the pairs alive in month nn-11 cannot give birth at
the beginning of month n
Only, all of the pairs alive in month n-2 can give birth
The number pairs in month n is the sum of the number
of pairs alive in month n-1 plus the number rabbits
alive in month n-2
Recurrence relation for the number of pairs in month n:
22
2/14/2013
Recursive Solution
Two base cases are necessary because there are two smaller
problems.
rabbit(1) = 1
rabbit(2) = 1
Recursive Solution:
rabbit(n) = 1
rabbit(n) = rabbit(n-1) + rabbit(n-2)
if n is 1 or 2
if n > 2
Recursive Solution in C
// Computes a term in the Fibonacci sequence.
// Precondition: n is a positive integer.
// Postcondition: Returns the nth Fibonacci
number.
int rabbit(int n)
{
if (n <= 2)
return 1;
else
// n > 2, so n-1 > 0 and n-2 > 0
return (rabbit(n-1) + rabbit(n-2));
} // end rabbit
23
2/14/2013
Limits of Recursion
Because recursion involves function calls, recursive
algorithms cost overhead
Algorithms that include deep recursion (includes a
large number of recursive function calls) may use too
much memory
However, it is still better to design some algorithms
(especially those dealing with data structures)
24