PSPC Unit II B Functions
PSPC Unit II B Functions
UNIT –II
Functions
Program Modules in C
• Functions: Modules in C
– Programs combine user-defined functions with library functions
• C standard library has a wide variety of functions
• Math function, I/O function, such as printf(), scanf()
• Function calls: Invoking functions
• Provide function name and arguments (data)
• Function performs operations or manipulations and return the results
• Write function once and call it many times
– Function call analogy:
• Boss asks worker to complete task
– Worker gets information, does task, returns result
– Information hiding: boss does not know details
– Also called Encapsulation
Functions
• Functions: Modularize a program
– All variables declared inside functions are local variables
• Known only in function defined
– Parameters
• Communicate information between functions
• Local variables
• Benefits of functions
– 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
Function Definitions
• 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
• An unspecified return-value-type is always assumed by the compiler to be int
– Parameter-list: comma separated list, declares parameters
• A type must be listed explicitly for each parameter, unless the parameter is of type int
– 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;
Function Prototypes
• Function prototype
– Function name
– Parameters – what the function takes in
– Return type – data type function returns (default int)
– Used to validate functions
– Prototype only needed if function definition comes after use in
program
– The function with the prototype
int maximum( int, int, int );
• Takes in 3 ints
• Returns an int
Examples:
Example 1: …
float mySquare(float);
main()
{
float y;
printf(“%f\n”, mySquare(5.0));
}
float mySquare(float x)
{
return x*x;
}
Header Files
• Header files
– Contain function prototypes for library functions
– <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
addone(int x)
{
return ++x;
}
10
11
Recursion
We could write:
6! = 6 * 5!
12
13
factorial function
14
factorial function
• Assume the number typed is 3, that is, numb=3.
fac(3) :
3 <= 1 ? No.
fac(3) = 3 * fac(2)
fac(2) :
2 <= 1 ? No.
fac(2) = 2 * fac(1)
fac(1) :
1 <= 1 ? Yes.
return 1
int fac(int numb){
fac(2) = 2 * 1 = 2 if(numb<=1)
return fac(2) return 1;
else
fac(3) = 3 * 2 = 6 return numb * fac(numb-1);
return fac(3) }
fac(3) has the value 6
15
factorial function
• For certain problems (such as the factorial function), a recursive solution often
leads to short and elegant code. Compare the recursive solution with the
iterative solution:
Iterative solution
Recursive solution
int fac(int numb){
int fac(int numb){ int product=1;
if(numb<=1)
while(numb>1){
return 1;
else
product *= numb;
return numb*fac(numb-1); numb--;
} }
return product;
}
16
Recursion
17
Recursion
Oops!
int result = 1;
while(result >0){
...
result++;
}
Oops!
18
Recursion
Similarly, if we use recursion we must be careful not
to create an infinite chain of function calls:
int fac(int numb){ Oops!
return numb * fac(numb-1); No termination
} condition
Or:
int fac(int numb){
if (numb<=1)
return 1;
else
return numb * fac(numb+1);
} Oops!
19
THANKS
20