Functions PDF
Functions PDF
Computer Programming I
1. Introduction
2. Functions
4. Example
5. Headers
7. Storage Classes
8. Recursion
1
Introduction
Introduction
2
Introduction
3
Introduction
Observations
• Familiarize yourself with the rich collection of functions in the C
standard library.
• Avoid reinventing the wheel. When possible, use C standard library
functions instead of writing new functions. This can reduce program
development time.
• Using the functions in the C standard library helps make programs
more portable.
• Each function should be limited to performing a single,
well-defined task, and the function name should express the task.
• If you cannot choose a concise name that expresses what the
function does, it’s possible that your function is attempting to
perform too many diverse tasks. It’s usually best to break such a
function into several smaller functions.
4
Functions
Functions
Function Definition
Example
1 // F u n c t i o n maximum d e f i n i t i o n
2 // x , y and z are p a r a m e t e r s
3 int maximum ( int x , int y , int z )
4 {
5 int max = x ; // assume x is largest
6
17 } // end f u n c t i o n maximum
7
Functions
Function Prototype
Example
It states that maximum takes three arguments of type int and returns a
result of type int.
Argument Coercion
• In general, argument values that do no correspond precisely to the
parameter types in the function prototype are converted to the
proper type before the function is called. These conversions can lead
to incorrect results if C’s usual arithmetic conversion rules are not
followed.
printf ( " %.3 f \ n " , sqrt (4) ) ;
9
Functions
• Mixed-type expressions
int b = 3 + 4.67
10
Functions
11
Functions
12
Function Call Stack and Stack
Frames
Function Call Stack and Stack Frames
14
Function Call Stack and Stack Frames
15
Function Call Stack and Stack Frames
16
Function Call Stack and Stack Frames
17
Example
Example
1 // F u n c t i o n maximum d e f i n i t i o n
2 // x , y and z are p a r a m e t e r s
3
19
Headers
Headers
20
Headers
21
Headers
Example:
# define PI 3.14159
File 1: myformula.h
1 # ifndef MYFORMULA_H
2 # define MYFORMULA_H
3
10 # endif // M Y F O R M U L A _ H
23
Headers Example
Listing 2: myformula.c
10 return result ;
11 }
24
Headers Example
Listing 3: main.cpp
3 int main () {
4
5 double num ;
6 printf ( " Ingrese un numero : " ) ;
7 scanf ( " % lf " , & num ) ;
8 expSquare ( num ,1) ;
9
12 }
25
Passing Arguments By Value and
By Reference
Passing Arguments By Value
int main () {
double num ;
printf ( " Ingrese un numero : " ) ;
scanf ( " % lf " , & num ) ;
expSquare ( num ,1) ; // num is passed by value
int main () {
double num ;
printf ( " Ingrese un numero : " ) ;
scanf ( " % lf " , & num ) ; // num is passed by r e f e r e n c e
expSquare ( num ,1) ;
27
Storage Classes
Storage Classes
28
Storage Classes
3 int accumulateSum () ; // f u n c t i o n p r o t o t y p e
4
13 int accumulateSum () {
14 // because the static keyword , sum is created
15 // only once . The default value is 0.
16 static int sum ;
17 // e v e r y t i m e we access the function ,
18 // sum i n c r e a s e s it value by 1.
19 sum = sum +1;
20 return sum ;
21 } 30
Storage Classes
1 http://www.geeksforgeeks.org/understanding-extern-keyword-in-c/
31
Scope Rules
• The function actually knows how to solve only the simplest case(s),
or so-called base case(s). If the function is called with a base case,
the function simply returns a result.
• If the function is called with a more complex problem, the function
divides the problem intro two conceptual pieces: a piece that the
function knows how to do and a piece that it does not know how to
do. This is called the recursion step.
• For the recursion to terminate, each time the function calls itself
with a slightly simpler version of the original problem, this sequence
of smaller problems must eventually converge on the base case.
• When the function recognizes the base case, it returns a result to
the previous copy of the function, and a sequence of returns ensues
all the way up the line until the original call of the function
eventually returns the final result to main.
33
Recursion - Example - Factorial
1 // R e c u r s i v e f a c t o r i a l f u n c t i o n
2 # include < stdio .h >
3
17 } // end main
34
Recursion - Example - Factorial
35
Recursion - Example - Factorial
36
Recursion - Example - Factorial
37