Chapter-8
Functions
Introduction
Functions are basic building blocks of program where we write our code.
We can assume any C program is collection of several function.
Till now we write our code in main function. In all our previous programs, we used to
create only one function that is main().
Modularization
Modular programming is a software design technique that increases the extent to which
software is composed of separate, interchangeable components, called modules by
breaking down program functions into modules, each of which accomplishes one
function and contains everything necessary to accomplish this
Suppose a C program is designed to perform a task. And say this task can be divided into
few subtasks, each of which is independent functionality. Each subtask can be coded in a
separate block known as function.
Program Execution always starts from main(). Main can then call other function. Any
function can call other function. This call is an invitation to that function to perform its
subtask.
Technical terms
l Calling Function: Function who call another function
l Called Function : Function who is called by another function
l Function call: Action of calling a function
You have been using few functions from the starting of programming. For example
printf(), scanf(), clrscr() and getch(). These are all functions and you often call them to
perform task for which they are designed.
There are many more such functions. These are predefined functions. There code kept in
library files. These files are comprehensively called built-in C library.
Function Types
There are two types of functions:
1) Predefined Functions
2) User defined Functions
2)
Ans:
3)
Ans:
4)
Ans:
5)
Ans:
Is there any limit of total number of functions that can c programs have?
No
6)
Ans:
7)
Ans:
8)
Ans:
9)
Ans:
10)
Why write separate functions at all? Why not squeeze the entire logic into one
function main()?
Writing functions avoids rewriting the same code over and over.
It provides strong readability.
It makes program easy to debug.
It becomes easy to modify.
Better memory utilization
Ans:
Recursion
Function calling itself is called recursion.
Example:
int fun(int );
main()
{
int x, k=3;
x = fun (k);
printf(%d, x);
}
int fun ( int a)
{
int f;
if (a == 1) return(1);
f = 2 + fun (a 1);
return(f);
}
The output is
5
Explanation:
Operating system calls main() function. Memory for x and k are being allotted. k is assigned with
3. Now fun() is called by main() and passes value of k(called by value). Memory for the function
fun(3) is allotted, in which there are two variables f and a. a contains 3.
Now condition a==1 is false so control moves on f=2+fun(a-1). Here, again make a call to
function fun(). fun() is calling fun(), this is called recursion.
Remember this time a new and separate memory is being allotted, call it fun(2). It also uses the
same definition but its variable a contains 2. The condition a==1 is again false. And again control
moves to f=2+fun(a-1). Again function fun(1) is called from fun(2) and recursion continues.
It reads a character and never wait for Enter key.Just gets processed after getting any key
pressed.And it never echoes the character on screen which u pressed.
getche()
Whenever you are pressing any key then the these are kept in Buffer. After hitting enter
the first character gets processed. And it obviously echoes on the screen.
Formatting with printf()
"printf" writes formatted output to "stdout". The result of "printf" is the number of
characters written. The output is formatted according to the "format" string. This string
may contain two kinds of objects:
Each placeholder starts with the character '%' and ends with one or two letters that
indicate what "type" of formatting is necessary.
Between the '%' and the "type" field may appear "modifiers", "width", and "precision"
fields. An ANSI placeholder has the form
%[modifiers][width][.precision]type
decimal integer
integer
character
float
long integer
double
long double
unsigned int
unsigned long int
The modifiers field consists of zero or more characters that indicate how output should be
padded (e.g. whether numbers are preceded by blanks or leading zeros), and whether or
not '+' or '-' signs are printed.
- (minus) indicates that values should be left-justified. The default action is to rightjustify them.
The width field
The width field is a non-negative decimal integer giving the minimum number of
characters to be printed
If the output value is shorter than the given width, it is padded to the appropriate width by
putting blanks on the right (or on the left, if the '-' "modifier" character is specified).
With numeric placeholders, the number in the width field may have a leading 0. With
this, the output value will be expanded with zeros to give the number the specified width.
For example, with "%05d" the value -1 will be printed as "-0001".
The width field can also be the character '*', in which case "printf" will take the next
argument in the argument list and take that as the width value. For example,
printf("%*d",4,X);
prints the value of X with a width of 4. Note that the width value is obtained from the
argument list BEFORE the output value is obtained.
The precision field
The precision field is a dot '.' followed by a non-negative decimal integer. Its meaning
depends on the "type" field as given below.
If the "type" is 'd', 'o', 'u', 'x' or 'X', the precision number is the smallest number of
digits that may appear in the output value. If necessary, the number will be padded on the
left with leading zeros. If the precision number is 0 or the field is just a '.' with no number
following, an output value of 0 will result in no characters being printed.
If the "type" is 'e', 'E', or 'f', the precision number is the number of digits printed after
the decimal point. If the precision number is 0 or the field is just a '.' with no number
following, no decimal point is printed.
The precision field can also be the character '*', in which case "printf" will take the next
argument in the argument list and take that as the precision value. For example,
printf("%*.*f",8,3,Y);
scanf(%c%c%c,&a,&b,&c);
For testing our input is qwe
q is stored in a, w is stored in b and e is stored in c.
scanf(%c%c%c,&a,&b,&c);
For testing our input is: q w e
q is stored in a, space is stored in b and w is stored in c.
This is because white-space characters are read like any other character
scanf(%c%c%c,&a,&b,&c);
For testing our input is: q enter
q is stored in a, space is stored in b
w enter
w is stored in c.
scanf(%c %c %c,&a,&b,&c);
For testing our input is: q enter
q is stored in a, space is a delimiter
w enter
w is stored in b, space is a delimiter
e enter
e is stored in c.
scanf(%c %c %c,&a,&b,&c);
For testing our input is: q w e
Output :
Enter a string terminate with a tilde (-) . . . t am a string
I am a string.