Você está na página 1de 24

2/14/2013

CS 101: Introduction to
C
Computing
ti

Dr. Samit Bhattacharya


Dept. of Comp. Sc. & Engg.,
IIT Guwahati, Assam, India

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() {

printf(C programming is easy);


printNewLine();
printf(But, you still fail!);
}

2/14/2013

Example
#include <stdio.h>
printNewLine() {
printf(\n);
}

During the execution, the program starts in main


function. When reach statement printNewLine(),
the statement(s) defined within the function will
be executed. When it finishes, it will return back
to the main function and execute the next
statement.

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)

Avoid code repetition

2/14/2013

Function Definition
Format
return-value-type function-name( parameter-list )
{
declarations and statements
}

Function-name: any valid identifier


Return-value-type: data type of the result (default int)
void indicates that the function returns nothing

Parameter-list: comma separated list, declares parameters


A type must be listed explicitly for each parameter unless, the
parameter is of type int

Function Definition
Format
return-value-type function-name( parameter-list )
{
declarations and statements
}

Declarations and statements: function body (block)


Variables can be declared inside blocks (can be nested)
Functions can not be defined inside other functions

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

printf(But, you still fail!);


}
printNewLine()
p
() {
printf(\n);
}

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>

Custom header files

Create file with functions


Save as filename.h
Load in other files with #include "filename.h"
Reuse functions

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

For now, we focus on call by value

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

extern: default for gglobal variables and functions


Known in any function

Scope Rules
File scope
Identifier defined outside function, known in all functions
Used for global variables, function definitions, function
prototypes

Function prototype scope


Used for identifiers in parameter list

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

What is the output?

11

2/14/2013

Recursion
C offers two approaches to repetitive programming
loops and recursion
We define recursion when a function calls itself

The Nature of Recursion


1.

2.
3.

One or more simple cases of the problem (called the


stopping cases or base case) have a simple non-recursive
solution.
The other cases of the problem can be reduced (using
recursion) to problems that are closer to stopping cases.
Eventually the problem can be reduced to stopping cases
only, which are relatively easy to solve.

In general:
if (stopping case)
solve it
else
reduce the problem using recursion

12

2/14/2013

Four Criteria
1.

A recursive function calls itself


This action is what makes the solution recursive

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.

Eventually, one of the smaller problems must be the base case


The manner in which the size of the problem diminishes ensures that the base
case is eventually is reached

Four Questions for


Constructing Recursive Solutions
1 How can you define the problem in terms of a smaller
1.
problem of the same type?
2. How does each recursive call diminish the size of the
problem?
3. What instance of the problem can serve as the base
case?
4. As the problem size diminishes, will you reach this
base case?

13

2/14/2013

Factorial Function Iterative


Definition
n! = n * ((n-1)) * (n-2)
( )* *2*1
0! = 1

for anyy integer


g n>0

Iterative Definition in C:
fval = 1;
for (i = n; i >= 1; ii--))
fval = fval * i;

Factorial Function Iterative


Definition
To define n! recursively,
y n! must be defined in terms
of the factorial of a smaller number
Observation (problem size is reduced):
n! = n * (n-1)!
Base case:
0! = 1
We can reach the base case,
case by subtracting 1 from n if n
is a positive integer

14

2/14/2013

Factorial Function Iterative


Definition
Recursive Definition:
n! = 1
n! = n*(n-1)!

if n = 0
if n > 0

Factorial Function Iterative


Definition
// Computes the factorial of a nonnegative integer.
// Precondition: n must be greater than or equal to 0.
// Postcondition: Returns the factorial of n; n is unchanged.

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

The statement that solves the problem is called the


base case. Every recursive function must have a base
case
The rest of the function is called the general case,
case
which reduces the size of the problem

16

2/14/2013

Review
1.
2
2.
3.

Determine the base case


Determine the general case
While paying close attention to logic, design a
function that combines the base case with the
general case
Each recursive call must reduce the size of the
problem and move it toward the base case. The
base case must not include a recursive call, but
must include a return

Tracing a Recursive Function


To trace a recursive function, the box method can
be used
The box method is a systematic way to trace the actions
of a recursive function
Illustrates how compilers implement recursion

17

2/14/2013

The Box Method (for a valued


function)
1.

Label each recursive call in the body of the recursive function


For each recursive call, we use a different label to distinguish
different recursive calls in the body
These labels help us to keep track of the correct place to which we
must return after a function call completes
After each recursive call, we return to the labeled location, and
substitute that recursive call with returned valued
if (n ==0)
return (1);
else
return (n * fact(n-1) )

The Box Method (continued)


2.

Each time a function is called, a new box represents its local


environment. Each box contains:
the values of the arguments
the function local variables
A placeholder for the value returned from each recursive call from
the current box (label in step 1)
The value of the function itself

18

2/14/2013

The Box Method (continued)


3.

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

printf(%d, fact (3));

The Box Method (continued)


4.

After a new box is created, we start to execute the body of the


function

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

Box Trace of fact(3)

Box Trace of fact(3)

20

2/14/2013

Box Trace of fact(3)

Multiplying Rabbits The


Fibonacci Sequence
Rabbits give birth so often. If rabbits did not die, their population would be
quickly get out of hand
Let us assume that:
Rabbits never die
A rabbit reaches sexual maturity exactly two months after birth (at the
beginning of its third month of life)
Rabbits are always born male-female pairs
At the beginning of every month, each sexually mature male-female pair
gives birth to exactly one male-female
male female pair
Question:
Suppose we start with a single newborn male-female pair in the first month
What will be the number rabbit pairs in month n?

21

2/14/2013

Multiplying Rabbits First


Seven Months
Month 1: 1 pair
Month 2: 1 pair
since it is not yet sexually mature
Month 3: 2 pairs
1 original pair + a newborn pair from the original pair because it is now sexually
mature
Month 4: 3 pairs
2 pairs alive in month 3 + a newborn pair from original pair
Month 5: 5 pairs
3 pairs alive in month 4 + 2 new newborn pairs from 2 pairs alive in month 3
Month 6: 8 pairs
5 pairs alive in month 5 + 3 new newborn pairs from 3 pairs alive in month 4
Month 7: 13 pairs
8 pairs alive in month 6 + 5 new newborn pairs from 5 pairs alive in month 5

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:

rabbit(n) = rabbit(n-1) + rabbit(n-2)

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

The series of numbers rabbit(1), rabbit(2), rabbit(3), is


known as Fibonacci Sequence

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

This rabbit function computes Fibonacci sequence

23

2/14/2013

Recursive Calls for rabbit(7)

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

Você também pode gostar