Function
Function
When a function has no arguments, it does not receive any data from
calling function.
When a function does not return any value, the calling function doesn’t
receive any data from called function.
There is no data transfer in between the calling function and called
function.
This type of functions may be useful to print some messages, draw a set of
lines, etc.
Eg:
#include<stdio.h>
displayline();
message ();
int main( )
{
displayline( );
message( );
displayline( );
return 0;
}
displayline ( ) // no arguments and no return type for display line
{
int i;
for(i=1; i<=80; i++)
printf(“-“);
printf(“\n”);
}
message( )
{
printf(“ ** TRY TO UNDERSTAND THE LANGUAGE **** \n ”);
printf(“ ***NOT TO BYHARD THE LANGUAGE ***** \n ”);
Functions with arguments and without return values:-
The nature of data communication between the calling function and the
called function is with arguments but no return values.
One-way data communication between calling function and called function
through arguments.
The control is transferred to the called function by passing some arguments,
after performing the task; the control is back to the calling function
without returning any value.
Eg.-
#include<stdio.h>
displayline(char);
message (char);
int main( )
{
char name[30] = “DENNIS RITCHIE”;
char ch = ‘-’;
displayline( ch);
message( name);
displayline( ch);
return 0;
}
displayline (char x ) // with arguments and no return type for displayline
{
int i;
for(i=1; i<=80; i++)
printf(“%c”, x);
printf(“\n”);
}
message( charstr[30]) // with arguments and no return type
{
printf(“ \t \t”);
printf(“ %s”, str);
printf(“\n”);
}
Functions without arguments and with return values:
The function is called without passing any arguments, but after performing
the task, it returns some value to the calling function.
One-way data communication between calling function and called function
through return statement.
The control is transferred to the called function without passing any
arguments, after performing the task; the control is back to the calling
function by passing some value.
Eg:
#include<stdio.h>
int factorial();
int main( )
{
int f;
f = factorial( );
printf(“ \n Factorial f = %d”, f); return 0;
}
int factorial( ) // without arguments, but returns some value.
{
int i, f =1,n;
printf(“ enter a value “);
scanf(“%d”, &n);
for(i=1; i<=n; i++)
f= f * i ;
return f;
}
Functions with arguments and with return values:-
Eg:
#include<stdio.h>
int factorial (int x);
int main( )
{ int n, f;
int factorial(int); // function declaration or prototyping
printf(“ enter a value “);
scanf(“%d”, &n);
f = factorial (n); // function calling statement
printf(“ \n Factorial f = %d”, f);
return 0;
}
int factorial( int x) // function definition with argument, with return value.
{
int i, f =1;
for(i=1; i<=x; i++)
f= f * i;
return f;
}
Parameter passing techniques:
and
Eg.
Eg.
x y x y
Example of Call by Value:
#include<stdio.h>
int main( )
{
int a, b;
printf(“ enter two values “);
scanf(“%d %d”, &a, &b);
Eg.
x y x y
1000 2000
Example of Call by Reference:
#include<stdio.h>
int main( )
{
int a, b;
Storage Class:
These example shows how local variables are used. Here all the
variables a, b, and c are local
to main() function.
2. #include <stdio.h>
/* global variable declaration */
int g;
void main ()
{
/* local variable declaration */
int a, b;
/* actual initialization */
a = 10;
b = 20;
g = a + b;
printf ("value of g = %d\n", g);
}
A program can have same name for local and global variables but the
value of local variable inside a function will take preference.
A variable’s storage class gives the following information:
Storage : Memory.
Output:
2. Register storage classes:
Eg. main ( )
{
register int i;
for(i = 1; i < = 10; i++)
printf(“\n %d”, i);
}
3. Static storage classes:
Storage : Memory
Eg. main ( )
{
int i;
for(i = 1; i < = 3; i++)
Display ( );
}
Display ( )
{
static int x = 0; x = x +1;
printf(“x = %d \n”, x);
}
Output:- x =
x=
x=
4. External storage classes:
Storage : Memory
Scope : Global
Output:
When to use those storage class:
− Use register storage class for only those variables that are
being used very often in a program. Reason is, there are very
few CPU registers at our disposal and many of them might be
busy doing something else. Make careful utilization of the
scarce resources. A typical application of register storage class
is loop counters, which get used a number of times in a
program.
− Use extern storage class for only those variables that are being
used by almost all the functions in the program. This would
avoid unnecessary passing of these variables as arguments
when making a function call. Declaring all the variables as
extern would amount to a lot of wastage of memory space
because these variables would remain active throughout the
life of the program.
PROBLEM SOLUTION
5! 5*4*3*2*1!
= 5 * 4! = 5*4*3*2*1
= 5 * 4 * 3! = 5*4*3*2
= 5 * 4 * 3 * 2! = 5*4*6
= 5 * 4 * 3 * 2 * 1! = 5*24
= 120
** So, every recursive function must have at least one base case.
Otherwise, the recursive function will generate an infinite
sequence of calls thereby resulting in an error condition known as
an infinite stack.
Example:
#include<stdio.h>
int fact (int);
int main()
{
int num, factorial;
printf(“\n Enter the number: ”);
scanf(“%d”, &num);
factorial = fact (num);
printf(“\n The factorial of %d = %d”, num, factorial);
return 0;
}
int fact ( int n )
{
if ( n==1 )
return 1;
else
return ( n * fact ( n-1 ) );
}
Example:-
TOWER OF HANOI:-
The tower of Hanoi is one of the main application of recursion.
It says, ‘if you can solve n-1 cases, then you can easily solve the nth
case’.
The Tower of Hanoi problem is solved using the set of rules given
below: