Module3 - Functions - PCD Notes C Programming

Download as pdf or txt
Download as pdf or txt
You are on page 1of 28

Programming in C and Data Structures 14PDS13

Module 3
FUNCTIONS

 Definition:

A function is a self contained block of code that performs a particular task.


Or
A function as series of instructions or group of statements with one specific
purpose.
Or
A function is a program segment that carries out some specific, well defined
task.

 Advantages of functions

 Separating mechanism of function from its particular use in the larger


program.
 Code reusability (writing the code only once and can be used any number of
times).
 Code modularity.
 Code manageability.
 Identification of errors will be difficult in a large complex program which
can be overcome by making use of functions..
 Easier to programming and understandability.
 Most commonly used functions can be stored as standard libraries which
can be used further.

 Types of functions

C functions can be classified into two types, namely


(i) Library functions or pre defined functions or standard functions or
built in functions

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 1


Programming in C and Data Structures 14PDS13

(ii) User defined functions

1. Built in Functions
These functions are defined in the library of C compiler which are used
frequently in the C program. C supports many built in functions like
 Mathematical functions
 String manipulation functions
 Input and output functions
 Memory management functions
 Error handling functions

2. User Defined Functions


Even though many functions are provided by C compiler, these functions are
not enough to perform customized functions. The user can construct their own
functions to perform some pecific task. This type of functions created by the user
is termed as User defined functions.

 Function Structure
The function structure consists of two parts
1. function header
2. function body

1. Function header
It consists of three parts
Syntax: return_type function_name( parameter or without parameters);
 The return value of the function
 The name of the function
 The parameters of the functions may be included or not
Exa: void add( int a, int b);
In the above example the return type of the function is void, the name of the
function is add and the parameters are 'a' and 'b' of type integer.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 2


Programming in C and Data Structures 14PDS13

2. Function body
The function body consists of the set of instructions enclosed between {
and } .

Generally while representing the function we can include the functions in the
program as
 Function Call
 Function Definitions
 Function Declaration

1. Function Call: a function call also termed as signature of the function is the
statement which calls the function. It is declared in the main program. A function
call is defined as function name followed by semicolon.
Exa:
void main()
{
add(a,b);
}

Note: A function call is nothing but invoking a function at the required place in
the program to achieve a specific task.

2. Function Definition or Function Body: a function definition is the body of


the function which would consist of series of instructions embedded with in the
flower braces after the function name.
Exa: void add( int a, int b)
{
// instructions
}

3. Function Declaration: function declaration also termed as function


prototyping consists of the return type of function, name of the function and
parameter list ending with semicolon.
Ex: void add(int a, int b);

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 3


Programming in C and Data Structures 14PDS13

Note: The function declaration should end with a semicolon

Function prototyping is not compulsory, if the definition of the function appears


before the call to the function. Generally the function call will be inserted in the
main function, if the function definition is present before the coding of main
function, then no need of function prototyping. Other wise it is must to specify
the function prototyping to make the finction code reachable.

Example: function to add two numbers

 without prototyping

#include<stdio.h>
#include<conio.h>

void add(int a, int b) // function definition


{
int x;
x=a+b;
printf(“\n the sum is %d”, x);
}

void main()
{
int a, b;
printf(“\n enter the two numbers a and b”);
scanf(“%d%d”, &a, &b);
add(a,b); // function call
getch();
}

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 4


Programming in C and Data Structures 14PDS13

 with prototyping

#include<stdio.h>
#include<conio.h>

void add(int a, int b); // function prototyping

void main()
{
int a, b;
printf(“\n enter the two numbers a and b”);
scanf(“%d%d”, &a, &b);
add(a,b); // function call
getch();
}
void add(int a, int b) // function definition
{
int x;
x=a+b;
printf(“\n the sum is %d”, x);
}

Note: the function prototyping can be specified in different ways if parameters are
passed
1. Only the data type can be specified instead of specifying the variable name
with data type.
Exa: void add(int a, int b); can also be written as void add( int, int);
2. If arrays are used as parameters then we have to specify the ranging values.
Exa: void add( int a[ ], int b[ ]); can be specified as void add( int [ ], int [ ]);

 Calling Function and Called Function

A function that is invoked or called when the program is being executed is called
“called function” and the invoking function is called “calling function”.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 5


Programming in C and Data Structures 14PDS13

#include<stdio.h>
#include<conio.h>

void add(int a, int b); // function prototyping

// main function is the calling function


void main()
{
int a, b;
printf(“\n enter the two numbers a and b”);
scanf(“%d%d”, &a, &b);
add(a,b); // function call
getch();
}

// add is the called function


void add(int a, int b) // function definition
{
int x;
x=a+b;
printf(“\n the sum is %d”, x);
}

In the above program we are calling a function add() from the main function, that
means we are invoking a function add(). We are calling the function add() from
the main function then main() is “calling function” and invoked function add() is
called as “called function”.

 Formal Parameters and Actual Parameters

 Formal Parameters:
The variables defined in the function header of function definition are
called formal parameters. All the variables should be separately declared and

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 6


Programming in C and Data Structures 14PDS13

each declaration must be separated by commas. The formal parameters


receive the data from actual parameters.

 Actual Parameters:
The variables that are used when a function is invoked are called actual
parameters. Using actual parameters, the data can be transferred to the
function. The corresponding formal arguments in the function definition
receive them. The actual parameters and formal parameters must match in
number and type of data.

Example:
#include<stdio.h>

void swap(int,int);

void main()
{
int a,b;
printf("enter values for a and b:");
scanf("%d %d",&a,&b);
printf("the values before swapping are a=%d b=%d \n",a,b);
swap(a,b); // The variables in Function call are known as Actual
parameters
printf("the values after swapping are a=%d b=%d \n",a,b);
}
void swap(int c, int d) // The variables in function header are known as
Formal parameters
{
int temp;
temp=c;
c=d;
d=temp;
}

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 7


Programming in C and Data Structures 14PDS13

 Differences between Actual and Formal Parameters

Actual Parameters Formal Parameters

Actual parameters are also called as Formal parameters are also


argument list. Ex: y=gcd(10,12) called as parameter list. Ex: int
gcd(int x, int y)
Actual parameters are used in calling Formal parameters are used in
function when a function is called or the function header of a called
invoked function.
Example: c= sub(a,b); Example:
Here, a and b are called actual int sub(int m,int n)
parameters { ………..
}
Here, m and n are called formal
parameters.
Actual parameters can be constants, Formal parameters should be
variables or expressions only variables. Expressions and
Example: constants are not allowed
C=sub(4,a+4); Example:
int sub(int m, int n)
{ ….
}
Actual parameters sends values to the Formal parameters receive
formal parameters values from the actual
Example: parameters.
C=sub(4,5); Example:
int sub(int m, int n)
{
}
Here, m will have the value of 4
and n will have the value 5.
Addresses of actual parameters can be If formal parameters contain
sent to formal parameters. addresses, they should be
declared as pointers.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 8


Programming in C and Data Structures 14PDS13

 Categories of the functions

1. Function with no parameters and no return values


2. Function with no parameters and return values.
3. Function with parameters and no return values
4. Function with parameters and return values

 Function with no parameters and no return values


In This category, there is no data transfer between the calling function
and called function. So, calling function cannot send values and the called
function cannot receive the data. Consider the following program

Example:
#include<stdio.h>
void add();
void main()
{
add(); / * Function Call */
}
void add() /* Function Header */
{
int a,b,c;
printf(“enter values for a and b:”);
scanf(“%d %d”,&a,&b);
c = a+b;
printf(“sum is:%d”,c);
}

In the above program


 In The calling function, when the function add() is called , no arguments
are passed to the function add(). So, no parameters are defined in the
function header.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 9


Programming in C and Data Structures 14PDS13

 When the function add() is called , the values for a and b are read, they
are added and the resulted is printed on the monitor.

 Function with parameters and no return values


In this category, there is data transfer from the calling function to the
called function using parameters. But there is no data transfer from called
function to the calling function.

Example:
#include<stdio.h>
void add(int m, int n);
void main()
{
int a,b;
printf(“enter values for a and b:”);
scanf(“%d %d”,&a,&b);
add(a,b); / * Function Call */
}
void add(int m,int n) /* Function Header */
{
int c;
c = m+n;
printf(“sum is:%d”,c);
}

In the above program


 In the calling function, when the function add() is called, two arguments
a and b are passed to the function add(). So, two parameters m and n
are defined in function header.
 The values of actual parameters a and b are copied into formal
parameters m and n.
 The value of m and n are added and result stored in c is displayed on
the screen

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 10


Programming in C and Data Structures 14PDS13

 Function with no parameters and return values


In this category there is no data transfer from the calling function to
the called function. But, there is data transfer from called function to the
calling function. When the function returns a value, the calling function
receives one value from the called function.

Example :
#include<stdio.h>
int add();
void main()
{
int result;
result=add(); / * Function Call */
printf(“sum is:%d”,result);
}
int add() /* Function Header */
{
int a,b,c;
printf(“enter values for a and b:”);
scanf(“%d %d”,&a,&b);
c = a+b;
return c;
}

In the above program


 In the calling function, when the function add() is called, no arguments
are passed to the function add(). So, no parameters are defined in the
function header.
 When the control is transferred to the called function, the two values are
read, they are added and the result is stored in c.
 When a return statement is executed in the function, the function is
terminated immediately and control goes to the calling function.
 The function call is replaced by the value returned by the return

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 11


Programming in C and Data Structures 14PDS13

statement and this value is copied into result in function main.

 Function with parameters and return values


In this category, there is data transfer between the calling function
and called function. When parameters are passed, the called function can
receive the values from the calling function. When the function returns a
value, the calling function can receive a value from the called function.

Example:
#include<stdio.h>
int add(int a, int b);
void main()
{
int a,b,result;
printf(“enter values for a and b:”);
scanf(“%d %d”,&a,&b);
result=add(a,b); / * Function Call */
printf(“sum is:%d”,result);
}
int add(int m,int n) /* Function Header */
{
int c;
c = m+n;
return c;
}

In the above program


 In the calling function, when the function add() is called, two arguments
a and b are passed to the function add(). So, two parameters m and n
are defined in the function header.
 The values of actual parameters a and b are copied into formal
parameters m and n.
 The values of m and n are added and result is stored in c.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 12


Programming in C and Data Structures 14PDS13

 When a return statement is executed in the function, the function is


terminated immediately and control goes to the calling function.
 The function call is replaced by the value returned by the return
statement and this value is copied into result in function main

 Type of argument passing


The different ways of passing parameters to the function are:
 Call by value
 Call by reference

 Call by value:
In call by value, the values of actual parameters are copied into
formal parameters, that is formal parameters contain only a copy of the
actual parameters. So, even if the values of the formal parameters changes
in the called function, the values of the actual parameters are not changed.

The concept of call by value can be explained by considering the following


program.

Example:
#include<stdio.h>
void swap(int,int);
void main()
{
int a,b;
printf("enter values for a and b:");
scanf("%d %d",&a,&b);
printf("the values before swapping are a=%d b=%d \n",a,b);
swap(a,b);
printf("the values after swapping are a=%d b=%d \n",a,b);
getch().
}

void swap(int c,int d)

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 13


Programming in C and Data Structures 14PDS13

{
int temp;
temp=c;
c=d;
d=temp;
}

 Execution starts from function main() and we will read the values for
variables a and b, assume we are reading 10 and 20 for a and b
respectively.
 We will print the values before swapping it will print 10 and 20.
 The function swap() is called with actual parameters a and b whose
values are 10 and 20.
 In the function header of function swap(), the formal parameters c and d
receive the values 10 and 20.
 In the function swap(), the values of c and d are exchanged.
 But, the values of actual parameters a and b in function main() have
not been exchanged.

 Call by Reference
In Call by reference, when a function is called, the addresses of
actual parameters are sent. In the called function, the formal parameters
should be declared as pointers with the same type as the actual
parameters. The addresses of actual parameters are copied into formal
parameters. Using these addresses the values of the actual parameters can
be changed.
The concept of call by reference can be explained by considering the
following program.

Example:
#include<stdio.h>
void swap(int *a,int *b);
void main()
{

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 14


Programming in C and Data Structures 14PDS13

int a,b;
printf("enter values for a and b:");
scanf("%d %d",&a,&b);
printf("the values before swapping are a=%d b=%d \n",a,b);
swap(&a,&b);
printf("the values after swapping are a=%d b=%d \n",a,b);
getch();
}
void swap(int *c,int *d)
{
int temp;
temp=*c;
*c=*d;
*d=temp;
}

The sequences of operations that are carried out during the execution of the
program are:

 Execution starts from function main() and assume that we are reading
values 10 and 20 for variables a and b. We will print the values before
swapping it will print 10 and 20.
 The function swap() is called by sending the addresses of a and b.
 In the function header of function swap, the formal parameters c and d
declared using *
operator holds the addresses of a and b.
 In the function swap(), using *c and *d we can access the values of a
and b and they are exchanged.

NOTE:

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 15


Programming in C and Data Structures 14PDS13

Pointer: A pointer is a variable that is used to store the address of another variable.
Syntax: datatype *variablename;
Example: int *p;
#include<stdio.h>
void main()
{
int a ,*p;
p=&a;
}
In the above program p is a pointer variable, which is storing the address of variable a.

Differences between Call by Value and Call by reference

Call by Value Call by Reference


When a function is called the values of When a function is called the addresses of
variables are passed variables are passed
The type of formal parameters should The type of formal parameters should be same as
be same as type of actual parameters type of actual parameters, but they have to be
declared as pointers.
Formal parameters contains the values Formal parameters contain the addresses of
of actual parameters actual parameters.
Change of actual parameters in the The actual parameters are changed since the
function call will not affect the actual formal parameters indirectly manipulate the
parameters in the calling function. actual parameters

Execution is slower since all the values Execution is faster since only addresses are
have to be copied into formal copied.
parameters.

 Scope and Life time of a variable

Scope of a variable is defined as the region or boundary of the program in


which the variable is visible. There are two types
(i) Global Scope
(ii) Local Scope

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 16


Programming in C and Data Structures 14PDS13

 Global Scope:
The variables that are defined outside a block have global scope. That is
any variable defined in global area of a program is visible from its definition
until the end of the program. For Example, the variables declared before all
the functions are visible everywhere in the program and they have global
scope.

 Local Scope
The variables that are defined inside a block have local scope. They exist
only from the point of their declaration until the end of the block. They are
not visible outside the block.

 Life Span of a variable


The life span of a variable is defined as the period during which a variable
is active during execution of a program.

For Example
 The life span of a global variable is the life span of the program.
 The life span of local variables is the life span of the function, they
are created.
Differences between global variable and local variables

Global variable Local variable

Global variables are declared outside Local variables are declared inside a
all the functions function

Global variables can be accessed by all Local variables can be accessible to only
the functions or modules in the the respective module or function where
program they are declared.

If no value is assigned to global If no value is assigned to local variable it


variable, by default it assigns the value hols garbage value
0.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 17


Programming in C and Data Structures 14PDS13

The life time of the global variable will The life time of the local variables
be for the entire duration of the limited only to the exeution of the
execution of program function where they are declared.

 Storage Classes
A storage class defines the scope (visibility) and life time of variables and/or
functions within a C Program.

There are following storage classes which can be used in a C Program


o auto
o register
o static
o extern

 auto - Storage Class: auto is the default storage class for all local
variables

Example:
void main()
{
int count;
auto int count;
}

The example above defines two variables with the same storage class. auto
can only be used within functions i.e local variables.

 register - Storage Class: register is used to define local variables that


should be stored in a register instead of Primary memory. This means that
the variable has a maximum size equal to the register size (usually one
word).

Example:
MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 18
Programming in C and Data Structures 14PDS13

void main()
{
register int count;
}

Register should only be used for variables that require quick access - such
as loops.

 Static-Storage Class : The variables that are declared using the keyword
static are called static variables. The static variables can be declared
outside the function and inside the function. They have the characteristics
of both local and global variables.

Static can also be defined within a function. If this is done the variable is
initialized at run time but is not reinitialized when the function is called.
This inside a function static variable retains its value during various calls.

/**********Example **********/
#include<stdio.h>
void display();
void main()
{
int i;
for(i=1;i<=5;i++)
display();
getch();
}
void display()
{
static int n=0;
n=n+1;
printf("the value of n is:%d \n",n);
}

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 19


Programming in C and Data Structures 14PDS13

In the above example, we are calling display() function 5 times, but in the
display() function we are initializing variable n to 0. Since the variable n is
static it will be initialized only once.

 Extern –Storage Class : The variables that are declared using the keyword
extern are called extern variables. These variables are declared before all
functions in global area of the program.

 Location of functions
While writing the program making use of function the question arises as
to where the function has to be placed in the program. Generally function
placement in the program can be done in three possible ways.

1. Function to be placed first followed by the main program.


2. Function to be placed after the main program, but function prototyping has to
be done before main program.
3. Two separate files one for each function and main program and compiled
separately.

In the first case the definition of the function appears before the main program's
call to the function, so that by the time compiler reaches the function call it will
be knowing what to expect.

Header files and Global


declarations

Function definition

Main program

In the second case the function appears after the main program. In this case the
compiler priorly doesn’t know about the function exact when it encounters the
function call in the main program. To overcome this we are specifying the
function prototyping.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 20


Programming in C and Data Structures 14PDS13

Header files and Global


declarations

Function prototyping

Main program

Function definition

In the last case we maintain function in a separate file and main program in a
separate file, compile them separately and a link will be provided between the two
for communication. This is the way how exactly the library functions work.

File 1 File 2

Header file and


function prototyping Function definition

Main program

 Recursion

A function calling itself is known as Recursion. But while using recursion


programmers need to be careful to define an exit condition from the
function, otherwise it will go in infinite loop.

Example:
void recursion()
{
recursion(); /* function calling itself */
}

In the above example we have not return the exit condition, so the function
is in infinite loop. So, we need to be careful to define an exit function when
we are implementing recursive functions.

Recursive functions are used to calculate many mathematical functions


like factorial of a give number and Fibonacci series.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 21


Programming in C and Data Structures 14PDS13

Example 1.
/******* Factorial of a given number using Recursion ******/
#include<stdio.h>
int fact(int m);
void main()
{
int n,result;
printf("enter number:");
scanf("%d",&n);
result=fact(n);
printf("the factorial of a number is:%d",result);
getch();
}
int fact(int m)
{
if(m==1)
return 1;
else
return (m*fact(m-1));
}

Notice how in function factorial we included a call to itself, but only if the
argument passed was greater than 1, since otherwise the function would
perform an infinite recursive loop in which once it arrived to 0 it would
continue multiplying by all the negative numbers (probably provoking a
stack overflow error on runtime).

This function has a limitation because of the data type we used in its
design (long) for more simplicity. The results given will not be valid for
values much greater than 10! Or 15! Depending on the system you compile
it.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 22


Programming in C and Data Structures 14PDS13

Any recursive function has two elements:


a). Base case b) General case

a) Base case: The statement that solves the problem is called base case.
Every recursive function must have at least one base case. It is a special
case whose solution can be obtained without using recursion.
A base case serves two purposes:
i). It act as a terminating condition.
ii). the recursive function obtains the solution from the base case it
reaches.

In the factorial problem, the base case is 0!=1

n!=1 if n=0

b) General case: The statement that reduces the size of the problem is
called general case. This is done by calling the same function with
reduced size.

In the factorial problem, we compute 5! Initial size of the problem is 5


and then the problem is reduced to find 4!, 3!, 2!, 1! and finally we reached the
base case 0! Where 0! is 1.
5!= 5 x 4!

n!= n * (n-1)! If n != 0 In general

In general recursive function of factorial problem can be written as

1 if n=0 // Base case

fact(n)=
n* fact(n-1)! Otherwise // General case

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 23


Programming in C and Data Structures 14PDS13

Example 2:
Fibonacci numbers:
Fibonacci numbers are a series of numbers such that each number is the
sum of the two previous numbers except the first and second number.

In this problem Base case: 0, 1


General case: 1, 2, 3, 5, 8,…..

In general recursive function of Fibonacci problem can be written as

0 if n=0 // Base case


1 if n=1 // Base case
Fib(n)=
fib(n-1) + fib(n-2) if n>2 // General case

Example 3:
Tower of Hanoi:
The Tower of Hanoi (also called the Tower of Brahma or Lucas' Tower). It
consists of three rods, and a number of disks of different sizes which can
slide onto any rod. It starts with the disks in a neat stack in ascending
order of size on one rod, the smallest at the top, thus making a conical
shape.
The objective of the puzzle is to move the entire stack to another rod,
obeying the following simple rules:
1. Only one disk can be moved at a time.
2. Each move consists of taking the upper disk from one of the stacks and
placing it on top of another stack i.e. a disk can only be moved if it is the
uppermost disk on a stack.
3. No disk may be placed on top of a smaller disk.
With three disks, the puzzle can be solved in seven moves. The minimum
number of moves required to solve a Tower of Hanoi puzzle is 2n - 1, where n is
the number of disks.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 24


Programming in C and Data Structures 14PDS13

Recursive solution for tower of hanoi


A key to solving this puzzle is to recognize that it can be solved by breaking
the problem down into a collection of smaller problems and further breaking
those problems down into even smaller problems until a solution is reached.

For example:
 label the pegs A, B, C — these labels may move at different steps
 let n be the total number of discs
 number the discs from 1 (smallest, topmost) to n (largest, bottommost)

To move n discs from peg A to peg C:


1. move n−1 discs from A to B. This leaves disc n alone on peg A
2. move disc n from A to C
3. move n−1 discs from B to C so they sit on disc n
The above is a recursive algorithm, to carry out steps 1 and 3, apply the same
algorithm again for n−1. The entire procedure is a finite number of steps, since
at some point the algorithm will be required for n = 1. This step, moving a
single disc from peg A to peg B, is trivial. This approach can be given a
rigorous mathematical formalism with the theory of dynamic programming and
is often used as an example of recursion.

In our Towers of Hanoi solution, we recurse on the largest disk to be moved.


That is, we will write a recursive function that takes as a parameter the disk
that is the largest disk in the tower we want to move. Our function will also
take three parameters indicating from which peg the tower should be moved
(source), to which peg it should go (dest), and the other peg, which we can use
temporarily to make this happen (spare).

At the top level, we will want to move the entire tower, so we want to move

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 25


Programming in C and Data Structures 14PDS13

disks 5 and smaller from peg A to peg B. We can break this into three basic
steps.

 Move disks 4 and smaller from peg A (source) to peg C (spare), using peg
B (dest) as a spare. How do we do this? By recursively using the same
procedure. After finishing this, we'll have all the disks smaller than disk
4 on peg C. (Bear with me if this doesn't make sense for the moment -
we'll do an example soon.)

In pseudocode, this looks like the following. At the top level, we'll call

MoveTower with disk=5, source=A, dest=B, and spare=C.

FUNCTION MoveTower(disk, source, dest, spare):

IF disk == 0, THEN:
move disk from source to dest
ELSE:
MoveTower(disk - 1, source, spare, dest) // Step 1 above
move disk from source to dest // Step 2 above
MoveTower(disk - 1, spare, dest, source) // Step 3 above
END IF

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 26


Programming in C and Data Structures 14PDS13

Note:
1. Recursion is not possible, where are called thousands of time.
2. Function can be used in expressions, more than once; provided
functions should not have void return type.
3. Even we can identify the function in mathematical view
Ex: (6, 4), (1, 0), (2,3), (6,10) : This is not a function because here 6 is
appeared 2 times

Tips and common errors


 Several possible errors related to passing parameters:
 It is a compiler error if the types in the prototype declaration and
function definition are incompatible.
 It is a compiler error to have a different number of actual parameters
in the function call then there are in the prototype statement.
 It is logic error if you code the parameters in the wrong order. Their
meaning will be inconsistence
 In the called program.
 It is compiler error to define local variables with the same identifiers as
formal parameters.
 Using void return with a function that excepts a return value or using a
return value with a function that excepts a void return is a compiler
error.
 Each parameters type must be individually specified: you cannot use
multiple definitions like in variables.
 Forgetting the semicolon at the end or a function prototype statement is
a compiler error. Similarly, using a semicolon at the end of the header in
a function definition is a compiler error.
 It is most likely a logic error to call a function from within itself or one of
its called functions.
 It is a compiler error to attempt to define a function within the body of
another function.
 It is a run time error to code a function call without the parentheses,
even when function has no parameters.
 It is a compiler error if the type of data in the return statement does not
MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 27
Programming in C and Data Structures 14PDS13

match the function return type.


 It is logic error to call srand every time you call rand.

MR. SUKRUTH GOWDA M A, Dept., of CS&E, RLJIT 28

You might also like