Professional Documents
Culture Documents
by
Dr. Subodh Srivastava
CSE,IIT (BHU)
Functions:
A function groups a number of program statements into a
single unit and give it names.
Every c program a collection of functions.
The function main() is executed first and it calls the other
functions directly or indirectly.
Every C program must have the function main().
We may have user defined functions which can be
called(invoked) from other parts of the program.
Monolethic program (a large single list of instructions)
becomes difficult to understand , debug ,test, and maintain.
A function has a clearly defined objective(purpose) and a
clearly defined interface with other functions in the
program.
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
Structure of C Functions:
Single Level Functions
#include<stdio.h>
main()
{
clrscr();/*clear the screen*/
printf(Welcome to the world of C programming\n);
printf(Programming is nothing but logic impletion);
The output of the Program will be:
Welcome to the world of C programming
Programming is nothing but logic impletion
An executable statement is
terminated by a semicolon(;).
A statement calling/executing a
function needs to be terminated by a
semicolon.
But, a statement marking the
beginning of a function-definition
does not have a semicolon at the end.
Note that main() may be called like
any other function.
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
Function Definition :
In C, a function must be defined prior to its use in the program.
..
executablen;
return(expression);
}
Calling Convention:
It specifies the order in which
arguments(parameters) are passed when a
function is called. There are two possibilities:
(i) Parameters might be passed from left to right.
(ii) Parameters might be passed from right to
left .
C languages obeys the second order.
In C programs, function that have parameters are
called in one of the two ways:
Call by value
Call by reference
Call by value :
big=x;
if(y>big)
big =y
if(z>big)
big=z;
printf(\n\nLagest of three numbers is %d\n,big);
}
The output of Program will be:
Enter the three numbers
60 75 40
a=
60
b=
75
c=
40
x=
60
y=
75
z=
40
Largest of three numbers is 75.
In the above program, value entered for variable a,b and
c in the main() function are passed to function
max().The values get copied into memory locations of the
arguments x,y,z respectively of the function max() when it is
called.
Below:
Content of a ,b,c
actual60arguments
75
60
40
75
x
formal arguments
40
int a =2;
modify(a);
modify (int a)
a*=3;
return;
int sum;
clrscr();
scanf(%ld,&num);
}
/* function definition sumdigit() */
int r,s=0;
/* local variables */
do
{
r=n%10;
s+=r; /* sum=sum+r */
n/=10 /* n=n/10 */
} while(n!=0)
return(s);
}
or
return(expression);
In the return(expression );statement, the value of the
expression is returned to the calling of the program.
When return is used without parentheses , no value
is returned to the calling a function. When no return
is specified the function returns an integer value.
big=x;
if(y>big)
big =y
if(z>big)
big=z;
return(big);
}
The output of Program will be:
Enter the three numbers
60 75 40
a=
60
b=
75
c=
40
Largest of three numbers is 75.
Always remember that a returns statement always
return one value. When return is used without
parentheses , no value is returned to the calling a
function. When no return is specified the function
returns an integer value.
A function can have more than one return statement.
Category of Functions:
In C, the functions can be divided
into the following categories:
(i) Function with no arguments and
no returns values.
(ii) Function having arguments but no
return values.
(iii) Function having arguments and
retuns values also.
void display(void)
{
float num1,num2;
char operator;
clrscr();
scanf(%f%f,&num1,&num2);
case /:
if(val12 !=0)
printf(Result is 8.2f,val1/val12);
break;
default:
break;
}
}
The output of Program will be:
50 20
Result is 1000.00
Enter an integer
0
f(0)=o
Enter an integer
7
f(7)=3
Nesting of Functions:
In C, the nesting of functions is allowed. It does not mean that
a function can be defined inside another function but by
nesting we mean calling of a function by another
function , which in turn call another function and so on.
There is no limitation on nesting of function in C.
For example, main() can call function func1(),which calls
function func2(),which calls function func3() and so on.
/* illustration of nesting of functions */
#include<stdio.h>
main()
clrscr();
printf(Hello Students!);
Recursion in Function:
Recursion is a process by which a function calls
itself repeatedly,until some specified condition has
been satisfied.
The process is used for repetitive computation in
which each action is stated in terms of previous
result.
In C, a function can call itself, this is called recursion. A
function is said to be recursive if there exists a statement
in its body for the function call itself.
Recursion is sometimes called circular definition.
The main advantage of recursion is that it is useful writing
clear,short and simple programs.
n!=nx(n-1)!
#include<stdio.h>
main()
int n;
clrscr();
scanf(%d,&n);
if(n<0)
else
#include<stdio.h>
main()
int n;
clrscr();
sacnf(%d,&n);
if(n<0)
else
Working of Recursion:
During each of a recursive function, a different set of local variables
is created. The names of these local variables are same , but all of
them are different .
They store different set of values during each call of the
subprogram.
The fibonacci terms are 0,1,1,2,3,5,8,.In this sequence each
term(except the first two )are obtained by adding its two immediate
predecessor terms. The recursive definition of this sequence is:
fib(n) = { 0 if n=1
1 if n=2
fib(n-1)+fib(n-2) if n>2
While coding recursive function , we must take care that there
exists a reachable termination inside the function so that
function may not be invoked endlessly.
Termination
Iteration: loop condition fails
Recursion: base case recognized
#include<stdio.h>
#include"myhead.h"
void main()
{
int num1 = 10, num2 = 10, num3;
num3 = add(num1, num2);
printf("Addition of Two numbers : %d", num3);
}
Include Our New Header File.
Instead of writing< myhead.h>use this
terminologymyhead.h
All the Functions defined in themyhead.h headerfile are now
ready for use .
Directly call function add(); [ Provide proper parameter and
take care of return type ]
Note
While running your program
precaution to be taken : Both files
[myhead.h and sample.c] should
be in same folder.
A) auto
A variable is declared automatic as given
printf (\n%c\n,x);
}
The o/p of program will be:
A
50.00
1024
B) register
A variable is declared as given given below:
register type variable_name;
All the variable can not be defined of registers storage
type for two reasons:
A) Limited number of CPU registers(14 in a PC usually).
B) Size of the CPU registers(16 bits a PC usually).
But a register variable provides a fast access than those in
memory. So, the time taken to read a variable from memory
is saved in this case. A variable with a register storage
specifier has
1) Storage place: CPU registers (if possible)
2) Default initial value: Grabage vale
3) Scope: Local to the function
4) Life time: Till the parent function is executing.
D) extern
A variable is declared extern as given below: