Title: FUNCTIONS
1FUNCTIONS
2Why we use functions?
- Taking a problem and breaking it into small ,
manageable pieces is critical to write large
programs - Each program consists of zero or more functions
and one of them is main() !!! - Program execution starts with main() function
3Function definition
- return_type function_name(input_parameter_list)
- declarations
- statements
- double twice(double x) / header /
- double result / body starts here /
- result x2
- return result
- // return x2
-
- int add( int x, int y) / header /
- int result / body starts here /
- resultxy
- return result
- // return (xy)
- // return xy
4Global versus Local variables
- Any variables declared in the body of functions
are said to be local to that function - Other variables may be declared external to the
function , called global variables - EXAMPLE
- includeltstdio.hgt
-
- int a 3 // global variable to main()
- void main(void)
- int b 7 // local variable to main()
- printf(d, a)
- printf(d, b)
- printf(d, ab)
- printf(d, a)
-
5Global versus Local variables
- includeltstdio.hgt
- int a 3 // global variable to main() and
new_func -
- int new_func(int y)
-
- void main(void)
- int b7, r 0 // local variables to main()
-
- printf(a d \n, a)
- printf(b d \n , b)
- rnew_func(b) // value of variable b (7) is
sent to function - printf(a d \n, a)
- printf(b d \n, b)
- printf(r d \n, r)
-
6return statement
- The return statment may or may not include an
expression - The return statement terminates the execution of
the function - EXAMPLES
- float f(int a, char b)
- int i
- ..
- return i / value of i will be converted to
float / - // return (i)
- / return value can also be written in
braces / -
7Function Prototypes
- Each function has to be declared before it is
used - EXAMPLE
-
- includeltstdio.hgt
- double twice (double x)// double twice (double
) -
- void main(void)
- double y, r
- r twice(y) // twice()is used in this line
- printf(result is f\n, r)
-
- double twice (double x)
- return x2
8Function Definition Order
- EXAMPLE 1
- includeltstdio.hgt
- int func_2()
- int func_1()
- int func_3()
-
- void main(void)
- func_1()
- func_2()
- func_3()
-
- int func_2()
- int func_1()
- int func_3()
- EXAMPLE 2
- includeltstdio.hgt
- int func_2()
-
- int func_1()
- func_3()
-
- int func_3()
-
- void main(void)
- func_2()
- func_1()
-
- int func_2()
- int func_3()
9Function Declarations from the Compilers view
point
- int f(x) // traditional C style
- double x
- ..
- /compiler does not knows about the parameter
list and the type of x is double so f(1) will
fail / - int f(double x) // ANSI C style
- ..
- / compiler knows about the parameter list so
f(1) will NOT fail /
10CALL BY VALUE
- if a variable is passed to a function , the
stored value in the calling environment will not
be changed!!! - EXAMPLE
- includeltstdio.hgt
- int my_sum(int n)
- void main(void)
- int n9
- printf(d\n ,n)
- printf(d\n ,my_sum(n)) //call function
my_sum() - printf(d\n ,n)
- int my_sum(int n)
- nn2 // stored value of n in my_sum() is
changed - return n
11Developing a large programread 5.8 (pg 209)
- / my_program.c /
- includemy_header.h
- void main(void)
-
- int a3, b4, result0
- resultmy_sum(a,b)
- printf(d\n,result)
-
- resultmy_subtract(a,b)
- printf(d\n,result)
- printf(f\n,PI)
- / my_header.h /
- includeltstdio.hgt
- define PI 3.14
- int my_sum(int x,int y)
-
- x
- y
- return xy
-
- int my_subtract(int x, int y)
-
- return x-y-1
-
12SCOPE RULES
- Each identifier is accessible only within the
block in which it has been declared!!! - EXAMPLE
- includeltstdio.hgt
- void func_1(int a)
- int b, c .
- void func_2(int a, double b, float d)
- char c .
- void main ()
- int a,b,d
- char c .
13SCOPE RULES
- EXAMPLE
- int a2 float b3.2 //outer block a
- printf(d,a)
- int a 5 //inner block a
- printf(d,a)
- printf(f,ab)
- int b4
- printf(d,ab)
-
-
- printf(d,a)
-
14STORAGE CLASSES
- auto / variables declared within function
bodies are automatic by default / - extern / look for it elsewhere either in
this file or in some other file / - register
- static
- EXAMPLES
- extern int a1
- auto float f
15STORAGE CLASSESExtern
- / file1.h /
- int f(void)
-
- extern int a
- / look for it elsewhere /
- int b, c
- a b c 4
- return (a b c)
-
/ prog1.h/ include ltstdio.hgt include
file1.h int a 1, b 2, c 3 /
external variables / int f(void) int
main(void) printf("3d3d3d\n", a, b, c)
printf("3d\n", f()) printf("3d3d3d\n", a,
b, c) return 0
16STORAGE CLASSESREGISTER an attempt to improve
execution time
- register / tell compiler that these
variables should be stored in high
speed memory registers / - EXAMPLES
- register char c
- register int a1
- / IS EQUIVALENT TO /
- register a1 // default type is int
17STORAGE CLASSESSTATIC memory opened for the
static variable will not be closed after the
execution of the function
- static
- EXAMPLES
- includeltstdio.hgt
- int ver()
- main()
-
- printf(1. valued, ver())
- printf(2. valued, ver())
- printf(3. valued, ver())
-
- int ver()
-
- static int k
- kk5
- return k
18STORAGE CLASSESSTATICcan be seen below the
prototype but can not be seen in any other file
- EXAMPLES
- void f(void)
- ...... // v is not avaible
- static int v
- void g(void)
- ..... // you can use v
- EXAMPLES
- static int g(void)
- void f(int a) // g() is avaible here but not
avaible in any other file - .....
- static int g ( void)
19RECURSION
- If a function calls itself , then it is called
recursive !!! - EXAMPLES
- void main(void)
- printf(I am calling myself!\n)
- main()
- int sum(int n) // what does this function compute
?? -
- if(nlt1) return n
- else return(nsum(n-1))
-
20RECURSION FACTORIAL
- /iterative version/
- int factorial (int n)
-
- int product1
-
- for(ngt1--n)
- productproductn
-
- return product
-
- / recursive version /
- int factorial(int n)
-
- if(nlt1)
- return 1
- else
- return (nfactorial(n-1))
-
21YOUR HOMEWORK