Eo Bil110e 5
Eo Bil110e 5
5
C Functions
OBJECTIVES
In this chapter you will learn:
▪ To construct programs modularly from small pieces
called functions.
▪ The common math functions available in the C
Standard Library.
▪ To create new functions.
▪ The mechanisms used to pass information between
functions.
▪ Simulation techniques using random number
generation.
▪ How to write and use recursive functions, i.e., functions
that call themselves.
5.1 Introduction
Why functions?
Break longer jobs into conceptually smaller jobs which are
precisely defined.
C program generally consists of many small functions.
A piece of a code which repeats at many places can be
written only once and used again and again.
Debugging and maintenance is easy
5.2 Functions in C
C standard library has a wide variety of functions
#include <math.h>
#include <stdio.h>
int main() {
sqrt(900);
sqrt(900.0);
- Calls function sqrt, which returns the square root of its
argument
- All math functions return data type double
function body
// function definition
int max(int a){
int b=100;
return a>b ? a : b;
}
nothing returns
// function definition
int max(int a, int b){ Function definition
return a>b ? a : b;
}
1 4 9 16 25 36 49 64 81 100
int main {
… }
sample_f ( parameter-list )
{
declarations and statements, calculations of x
return x;
}
int main {
… }
int main {
… }
return x;
}
sample_f (parameter-list );
int main {
… }
sample_f ( parameter-list )
{
declarations and statements, calculations of x
return x;
}
int main {
… } y is declared as
integer
double sample_f ( double x, y )
{
declarations and statements, calculations of x
return x;
}
int main {
… }
int main {
… }
unsigned int %u %u
int %d %d
unsigned short %hu %hu
short %hd %hd
char %c %c
Converting to
Fig. 5.5 | Promotion hierarchy for data types. lower data types
can lead to
errors
int main {
… }
// function definition
int sample_f ( int x, int y )
{
declarations and statements, calculations of x
return x;
}
5.8 Headers
▪ Header files
– Contain function prototypes for library functions
– <stdlib.h> , <math.h> , etc
– Load with #include <filename>
#include <math.h>
<assert.h> Contains macros and information for adding diagnostics that aid
program debugging.
<ctype.h> Contains function prototypes for functions that test characters for
certain properties, and function prototypes for functions that can
be used to convert lowercase letters to uppercase letters and vice
versa.
<errno.h> Defines macros that are useful for reporting error conditions.
<float.h> Contains the floating-point size limits of the system.
<limits.h> Contains the integral size limits of the system.
<locale.h> Contains function prototypes and other information that enables a
program to be modified for the current locale on which it is
running. The notion of locale enables the computer system to
handle different conventions for expressing data like dates, times,
dollar amounts and large numbers throughout the world.
1 + ( rand() % n )
1 + ( rand() % 6)
Face Frequency
1 1003
2 1017
3 983
4 994
5 1004
6 999
srand function
– <stdlib.h>
– Takes an integer seed and jumps to that location in its
"random" sequence
srand( seed );
Enter seed: 67
6 1 4 6 2
1 6 1 6 4
Enter seed: 67
6 1 4 6 2
1 6 1 6 4
If you hard code the seed or allow the user to enter a seed,
simulated randomness is lost
Most programmers use the “time” function to seed the generator
This lessens the probability that a user can determine the pattern of
psuedo-random numbers
Player rolled 4 + 1 = 5
Point is 5
Player rolled 6 + 2 = 8 fig05_11.c
Player rolled 2 + 1 = 3
Player rolled 3 + 2 = 5
Player wins
Player rolled 1 + 1 = 2
Player loses
Player rolled 6 + 4 = 10
Point is 10
Player rolled 3 + 4 = 7
Player loses
Local Variable
The variables declared inside the function are automatic or local variables.
The local variables exist only inside the function in which it is declared. When the
function exits, the local variables are destroyed.
int main() {
int n; // n is a local variable to main() function
... .. ...
}
void func() {
int n1; // n1 is local to func() function
}
In the above code, n1 is destroyed when func() exits. Likewise, n gets destroyed
when main() exits.
Global Variable
Variables that are declared outside of all functions are known as global variables.
Global variables are accessible to any function.
#include <stdio.h>
void display();
int n = 5; // global variable
int main() {
++n; // variable n is not declared in the main() function
display();
return 0;}
void display() {
++n; // variable n is not declared in the display() function
printf ("n = %d", n); }
▪ Automatic storage
– Object created and destroyed within its block
– Automatic storage is default for local variables
auto double x, y;
Or
double x,y
Automatic storage is a means of conserving memory,
because automatic variables exist only when they are
needed. They are created when the function in which they
are defined is entered and they are destroyed when the
function is exited.
▪ Static storage
– Variables exist for entire program execution
– Default value is zero
local x in main is 5
5.14 Recursion
▪ Recursive functions
– Functions that call themselves.The function launches a new copy of itself
(recursion step) to solve what it cannot do
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;
0! = 1
1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800
Enter an integer: 0
Fibonacci( 0 ) = 0
Enter an integer: 1
Fibonacci( 1 ) = 1
Enter an integer: 2
Fibonacci( 2 ) = 1
(continued on next slide… )
Enter an integer: 4
Fibonacci( 4 ) = 3 fig05_15.c
(3 of 4 )
Enter an integer: 5
Fibonacci( 5 ) = 5
Enter an integer: 6
Fibonacci( 6 ) = 8
(continued on next slide… )
Enter an integer: 20
Fibonacci( 20 ) = 6765 fig05_15.c
(4 of 4 )
Enter an integer: 30
Fibonacci( 30 ) = 832040
Enter an integer: 35
Fibonacci( 35 ) = 9227465