0% found this document useful (0 votes)
10 views

Function

The document provides an overview of functions in C programming, categorizing them based on arguments and return values. It also discusses parameter passing techniques (call by value and call by reference), storage classes, and recursion, including types of recursion. Examples are provided to illustrate each concept, highlighting their usage and behavior in C.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Function

The document provides an overview of functions in C programming, categorizing them based on arguments and return values. It also discusses parameter passing techniques (call by value and call by reference), storage classes, and recursion, including types of recursion. Examples are provided to illustrate each concept, highlighting their usage and behavior in C.

Uploaded by

it10800223115
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

FUNCTION

1. Functions without arguments and without return values.

2. Functions with arguments and without return values.

3. Functions without arguments and with return values.

4. Functions with arguments and with return values.


Functions without arguments and without return values:-

 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:-

 Two-way data communication between calling function and called function


is by passing arguments and sending back some value.
 The control is transferred to the called function by passing some arguments,
after performing the task; the control is back to the calling function by
returning some value.
 At the time of function calling the actual arguments are dumped into
formal arguments.

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:

1. pass by value (call by value)

and

2. pass by reference (call by reference).

Remember those point in previous class:

Actual parameters: The parameters passed to a function.


Formal parameters: The parameters received by a function.

Eg.

mult( m, n); int mult(int a, int b)


{
Return(a*b);
}
1. Call by value:
 In call by value mechanism a copy of the data is sent to the function.
That is the values of actual arguments are being copied into formal
arguments. And ensures that the original data in the calling function
cannot be changed.
 Memory is allocated temporarily for formal parameters and local
variables.
 Whatever the modifications are done for formal parameters will not
affect the actual parameters.

Eg.

int x = 10, y = 20; int fun(int x, int y)


fun( x, y ); {
X = 20;
Y = 10;
}

x y x y
Example of Call by Value:

#include<stdio.h>

void swap(int, int); // function declaration or prototyping

int main( )
{
int a, b;
printf(“ enter two values “);
scanf(“%d %d”, &a, &b);

swap(a, b); // function calling statement

printf(“ Values after swap function call are: “);


printf(“ a= %d \t b = %d ”, a, b);
}

void swap( int x, int y) // function definition.


{
int temp;
temp = x;
x = y;
y = temp;
}

o/p is - Enter two values 10 20


Values after swap function call are: a = 10 b=20
2. Call by Reference:

 In call by reference mechanism the address of the data rather


than a copy is sent to the function. That is the address of actual
arguments is being passed into formal arguments. The called
function can change the original data in the calling function.
 When we pass the addresses, the receiving parameters should be
pointers to hold these addresses.
 Whatever the modifications are done for formal parameters will
directly affect the actual parameters.
 C language does not have a true call by reference and it is
stimulated by call by address.

Eg.

int x = 10, y = 20; int fun(int *ptr1, int *ptr2)


fun( &x, &y ); {
*ptr1 = 20;
*ptr2 = 10;
}

x y x y

1000 2000
Example of Call by Reference:

#include<stdio.h>

void swap(int, int); // function declaration or prototyping.

int main( )
{
int a, b;

printf(“ enter two values “);


scanf(“%d %d”, &a, &b);

swap(&a, &b); // function calling statement.

printf(“ Values after swap function call are: “);


printf(“ a= %d \t b = %d ”, a, b);
return 0;
}

void swap( int *x, int *y) // function definition.


{
int temp;
temp = *x;
*x = *y;
*y = temp;
}

o/p is - Enter two values 10 20


Values after swap function call are: a = 20 b=10
Storage classes in C:

Storage Class:

SCOPE : A scope in any programming is a region of the program where a


defined variable can have its existence and beyond that variable it cannot be
accessed.

Local and Global variables:

Eg: 1. #include <stdio.h>


void main ()
{
/* local variable declaration */
int a, b;
int c;
/* actual initialization */
a = 10;
b = 20;
c = a + b;
printf ("value of a = %d, b = %d and c = %d\n", a, b,
}

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 global variable can be accessed by any function. That is, a global


variable is available for use throughout your entire program after its
declaration.

Eg. #include <stdio.h>


/* global variable declaration */
int g = 20;
int main ()
{
/* local variable declaration */
int g = 10;
printf ("value of g = %d\n", g);
return 0;
}

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:

1. Where the variable would be stored.


2. What will be the default initial value.
3. What is the scope of the variable.
4. What is the life of the variable that is how long would the
variable exist.

There are four storage classes in C:

1. Automatic storage classes,


2. Register storage classes ,
3. Static storage classes,
4. External storage classes .

1. Automatic storage classes:

Storage : Memory.

Initial value: garbage value

Scope : Local to the block in which the variable


is defined.

Life time : Till the control remains within the block


in which the variable is defined.
Eg. main ( )
{
auto int a = 1;
{
auto int a = 2;
{
auto int a = 3;
printf(“a = %d”, a);
}
printf(“ \n a = %d”, a);
}
printf(“ \n a = %d”, a );
}

Output:
2. Register storage classes:

Storage : CPU registers

Initial value : Garbage value

Scope : Local to the block in which the variable


is defined.

Life time : Till the control remains within the block


in which the variable is defined.

Eg. main ( )
{
register int i;
for(i = 1; i < = 10; i++)
printf(“\n %d”, i);
}
3. Static storage classes:

Storage : Memory

Initial value: Zero

Scope : Local to the block in which the variable


is defined

Life time : Value of the variable persists between


different function calls

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

Initial value: Zero

Scope : Global

Life time : As long as the program’s execution


doesn’t come to an end
Eg.
int i = 10;
main ( )
{
int i = 20;
printf(“\n %d”,i);
display ( );
{
printf (“\n %d”, i)
}

Output:
When to use those storage class:

− Use static storage class only if you want the value of a


variable to persist between different function calls.

− 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.

− If you don’t have any of the express needs mentioned above,


then use the auto storage class. In fact most of the times we
end up using the auto variables, because often it so happens
that once we have used the variables in a function we don’t
mind loosing them.
Recursion Funtion:

Recursion function is defined as a function that the process of calling


the same function itself again and again until some condition is
satisfied. This process is used for repetitive computation in which
each action is satisfied in terms of a previous result.

To understand recursive function ,


Let us take an example of calculating factorial of a number.
i.e : Calculate n!, that means, we multiply the number with factorial
of the number that is less than that number. i.e, n * (n-1)!

Let us we need to find the value of 5! :


5! = 5 * 4 * 3 * 2 * 1
= 120

So, the series of problems and solutions can be given -

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

** Every recursive solution has two major cases:

1. Base case : in which the problem is simple enough to be


solved directly without making any further calls to the same
function.
E.g - Base case is when n=1, because if n = 1, the result will
be 1 as 1! = 1.
2. Recursion case : Recursive case of the factorial function
will call itself but with a smaller value of n, this case can be
given as
Factorial (n) = n * factorial (n-1)

** 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 ) );
}

Try to write a program to calculate GCD and to print


Fibonacci Series using recursive function.
Type of Recursion:
i) Direct recursion- A function is said to be direct recursive if it
explicitly calls itself.
E.g int func (int n)
{
if ( n==0 )
return n ;
else
return ( func (n-1) );
}

ii) Indirect recursion- A function is said to be indirectly recursive if it


contains a call to another function which ultimately calls it.
E.g int func1 (int n)
{
if ( n==0 )
return n ;
else
return func2(n);
}
int func2 (int x)
{
return func1(x-1);
}
iii) Tail recursion- A recursive function is said to be tail recursive if
no operations are pending to be performed when the recursive
function returns to its caller.
E.g int fact ( int n)
{
if (n==1)
return 1;
else
return ( n * fact (n-1) );
}
iv) Linear or Tree recursion- Recursive function can also be
characterized depending on the way in which recursion grows, i.e., in
a linear fashion or forming a tree structure.
Recursive function is said to be linearly
recursive when the pending operation does not make another
recursive call to the function. E.g- factorial function
Recursive function is said to be tree
recursive if the pending operation makes another recursive call to
the function. E.g- fibonacci function

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:

1. Only one disc can be moved at a time.


2. Only the top disc of one stack can be transferred to the top of
another stack or an empty rod.
3. Larger discs cannot be stacked over smaller ones.

You might also like