Shivani Varshney/RCET/Programming With C
Shivani Varshney/RCET/Programming With C
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 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
Promotion rules and conversions
Converting to lower types can lead to errors
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
rand function
Load <stdlib.h>
Returns "random" number between 0 and RAND_MAX (at least
32767)
i = rand();
Pseudorandom
Preset sequence of "random" numbers
Same sequence for every function call
Scaling
To get a random number between 1 and n
1 + ( rand() % n )
rand() % n returns a number between 0 and n - 1
Add 1 to make random number between 1 and n
1 + ( rand() % 6)
number between 1 and 6
srand function
<stdlib.h>
Takes an integer seed and jumps to that location in its
"random" sequence
srand( seed );
srand( time( NULL ) ); //load <time.h>
time( NULL )
Returns the time at which the program was compiled in seconds
Enter seed: 67
6 1 4 6 2
1 6 1 6 4
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 global variables and functions
Known in any function
File scope
Identifier defined outside function, known in all functions
Used for global variables, function definitions, function
prototypes
Function scope
Can only be referenced inside a function body
Used only for labels (start:, case: , etc.)
Block scope
Identifier declared inside a block
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
Function prototype scope
Used for identifiers in parameter list
global x is 1 on entering c
global x is 10 on exiting c
global x is 10 on entering c
global x is 100 on exiting c
local x in main is 5
Pointers as function arguments
The function must be called by passing only the name of the array.
}
The function prototype must show that the argument is an array.
}
The size of the second dimension must be specified.
The prototype declaration should be similar to function header.
}
The function prototype must show that the argument is a string
#include <stdio.h>
#define ABS(a) (a) < 0 ? -(a) : (a)
int main(void)
{
printf("abs of -1 and 1: %d %d", ABS(-1), ABS
(1));
return 0;
}
Recursive functions
Functions that call themselves
Can only solve a base case
Divide a problem up into
What it can do
What it cannot do
What it cannot do resembles original problem
Example: factorials
5! = 5 * 4 * 3 * 2 * 1
Notice that
5! = 5 * 4!
4! = 4 * 3! ...
Can compute factorials recursively
Solve base case (1! = 0! = 1) then plug in
2! = 2 * 1! = 2 * 1 = 2;
3! = 3 * 2! = 3 * 2 = 6;
f( 3 )
return f( 2 ) + f( 1 )
return f( 1 ) + f( 0 ) return 1
return 1 return 0
Enter an integer: 1
Fibonacci(1) = 1
Enter an integer: 2
Fibonacci(2) = 1
Enter an integer: 3
Fibonacci(3) = 2 Program Output
Enter an integer: 4
Fibonacci(4) = 3
Enter an integer: 5
Fibonacci(5) = 5
Enter an integer: 6
Fibonacci(6) = 8
Enter an integer: 10
Fibonacci(10) = 55
Enter an integer: 20
Fibonacci(20) = 6765
Enter an integer: 30
Fibonacci(30) = 832040
Enter an integer: 35
Fibonacci(35) = 9227465
Recursion vs. Iteration
Repetition
Iteration: explicit loop
Recursion: repeated function calls
Termination
Iteration: loop condition fails
Recursion: base case recognized
Both can have infinite loops
Balance
Choice between performance (iteration) and good software
engineering (recursion)