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

functions-unit2 (1)

The document provides an overview of functions in C programming, detailing their definition, advantages, and types, including library and user-defined functions. It explains how to declare, call, and define functions, as well as the methods of passing parameters (call by value and call by reference). Additionally, it covers variable scope, storage classes, recursion, and the Towers of Hanoi problem, illustrating concepts with code examples.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

functions-unit2 (1)

The document provides an overview of functions in C programming, detailing their definition, advantages, and types, including library and user-defined functions. It explains how to declare, call, and define functions, as well as the methods of passing parameters (call by value and call by reference). Additionally, it covers variable scope, storage classes, recursion, and the Towers of Hanoi problem, illustrating concepts with code examples.

Uploaded by

sudharani.am
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 9

UNIT-II

FUNCTIONS
Q) What is function?
 A function is a self-contained block of statements to perform a specific task in a program.
 A large C program is divided into basic building blocks called C function.
 C function contains set of instructions enclosed by “{ }” which performs specific operation in a
C program.
Q) What are advantages of Functions?
1. Program debugging is made easy.
2. The length of the source program can be reduced.
3. The same function can be used for many programs once it is written.
4. Functions may increase program execution speed.
5. Functions improve optimum utilization of memory.
6. Functions are more reliable.

Q) What are different types of C functions?


There are two types of functions in C programming
 Library functions (or) pre defined functions.
 User defined functions

Library functions:
The standard library functions are built-in functions in C programming to handle tasks such as
mathematical computations, I/O processing, string handling etc.
These functions are defined in the header file. When you include the header file, these functions are
available for use. For example printf(), scanf().

User defined functions:


C provides programmer to define their own function according to their requirement known as user
defined functions.

Q) Explain User defined functions in C?


A user defined function requires three different types of statements to be specified by the user.
They are
1. Function prototype or declaration: gives information about function name, type of arguments to
be passed and return type.
2. Function call: the main program calls the user defined function.( This calls the actual function)
3. Function definition: this contains all the statements to be executed.

C functions aspects syntax


function declaration return_type function_name (argument list);
(prototype)

function call function_name (arguments list);

function definition return_type function_name (arguments list)


{
Body of function;
return (expression);
}
return_type: It is the data type of the value that function returns.
1
function_name:It is the actual name of the function.
Example: power(a,b);
add(a,b);
Argument list: The passing values to the function are called arguments or parameters. These are 2
types.
 Actual parameter – This is the argument which is used in function call.
 Formal parameter – This is the argument which is used in function definition

return(): This is to specify the type of value being returned by the function to its parent function or
calling function.

Example1:

#include<stdio.h>
void print(); -----function prototype(or)declaration;
void main()
{
printf(); ---function calling.
getch();
}
void print()
{
int i;
for(i=1;i<=5;i++) function definition
printf(“%d”,i);
}

Example 2:
#include<stdio.h>
void print(int n); -----function prototype(or)declaration
void main()
{
int n;
clrscr();
printf(“enter n value\n”);
scanf(“%d”,&n);
printf(); ---function calling.
getch();
}
void print(int n)
{
int i;
for(i=1;i<=n;i++) function definition
printf(“%d”,i);
}

Q) How to pass parameters to the function?


2
When a function is called, the calling function may have to pass some values to the called
function.
In C there are two ways to pass parameters to called function.

1. Call by value:
 In call by value method, the value of the variable is passed to the function as parameter.
 The value of the actual parameter cannot be modified by formal parameter.
 Different Memory is allocated for both actual and formal parameters. Because, value of actual
parameter is copied to formal parameter.

#include<stdio.h>
void swap(int a, int b);
void main()
{
int m = 22, n = 44;
printf(" values before swap m = %d \n and n = %d", m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(" \nvalues after swap m = %d\n and n = %d", a, b);
}

2. CALL BY REFERENCE:
 In call by reference method, the address of the variable is passed to the function as parameter.
 The value of the actual parameter can be modified by formal parameter.
 Same memory is used for both actual and formal parameters since only address is used by both
parameters.

#include<stdio.h>
void swap(int *a, int *b);
void main()
{
int m = 22, n = 44;
printf("values before swap m = %d \n and n = %d",m,n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf("\n values after swap a = %d \n and b = %d", *a, *b);
}
Q) Write about Scope of variables?
3
Scope of a variable means the accessibility and visibility of the variables at different points in
the program.
The scope of any variable can be broadly categorized into three categories:
o Global scope: When variable is defined outside all functions. It is then available to all the
functions of the program and all the blocks program contains.
o Local scope: When variable is defined inside a function or a block, then it is locally accessible
within the block and hence it is a local variable.
o Function scope: When variable is passed as formal arguments, it is said to have function
scope.

Q) Explain different Storage classes in C?


A storage class defines the scope (visibility) and life-time of variables and/or functions within a
C Program.
Scope of a variable means the accessibility and visibility of the variables at different points in the
program.
Life Time – Life time of any variable is the time for which the particular variable outlives in memory
during running of the program.
Storage classes are classified into 4 types. They are

1) Automatic variables.
2) Static variables.
3) Register variables.
4) External variables.

1) Automatic variables: The value of the automatic variable will have effect only in a function.
Storage place: CPU Memory
Initial/default value: Garbage value
Scope: local
Life: Within the function only.
Key word: auto
ex: int a,b; (or) auto int a,b;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
auto int a; /* int a; */
void display();
clrscr();
printf("\n The Initial value of automatic variable is :%d",a);
a=5;
printf("\n The Specified value of automatic variable is :%d",a);
display();
}
void display()
{
int a;
printf("\n The value of automatic variable in another function is :%d",a);
}
2) External Variables: The value of the external variable will have effect in the entire program.
4
Storage place: CPU memory
Initial/default value: Zero
Scope: Global
Life: Till the end of the main program.
Keyword: extern

Example: extern int a;


Example:
#include<stdio.h>
#include<conio.h>
int a;
void main()
{
extern int a;
void display();
clrscr();
printf("\n The Initial value of external variable is :%d",a);
a=5;
printf("\n The Specified value of external variable is :%d",a);
display();
getch();
}
void display()
{
extern int a;
printf("\n The value of external variable in another function is :%d",a);
}

3) Static Variables:
A static variable tells the compiler to persist the variable until the end of program. Instead of creating
and destroying a variable every time when it comes into and goes out of scope, static is initialized
only once and remains into existence till the end of program. A static variable can either be internal or
external depending upon the place of declaration. Scope of internal static variable remains inside
the function in which it is defined. External static variables remain restricted to scope of file in each
they are declared.
Storage place: CPU memory
Initial/default value: Zero
Scope: local
Life: Retains the value of the variable between different function calls.
Keyword: static
Example: static int a;
Example:
#include<stdio.h>
#include<conio.h>
void main()
{
static int a;
clrscr();
printf("\n The Initial value of static variable is :%d",a);
a=5;
5
printf("\n The Specified value of static variable is :%d",a);
display();
getch();
}
void display()
{
static int a;
printf("\n The value of static variable in another function is :%d",a);
}

4) Register variables:
Register variable inform the compiler to store the variable in register instead of memory.
Register variable has faster access than normal variable.
Storage place: Register memory
Initial/default value: Garbage value
Scope: local
Life: Within the function only.
Keyword: register

Example: register int a;


Example:
#include<stdio.h>
#include<conio.h>
void main()
{
void disp();
register int a;
clrscr();
printf("\n The Initial value of register variable is :%d",a);
a=5;
printf("\n The Specified value of register variable is :%d",a);
disp();
getch();
}
void disp()
{
register int a;
printf("\n The Specified value of register variable is :%d",a);
}

Q) What is Recursion? Explain with example.

A function that calls itself is known as a recursive function. And, this technique is known as recursion.

#include<stdio.h>
int fact(int n);
void main()
{
int n,f;
clrscr();
6
printf("Enter an positive integer: ");
scanf("%d",&n);
f=fact(n);
printf("Factorial of %d”,f);
getch();
}
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}

Explanation: if n=5
=fact(5)
=5*fact(4)
=5*4*fact(3)
=5*4*3*fact(2)
=5*4*3*2*fact(1)
=5*4*3*2*1
=120.

Q) Write about Types of recursion?


 Direct Recursion
 Indirect Recursion
Direct Recursion
A function is said to be direct recursive if it calls itself directly.
int fact(int n)
{
if(n==1)
return 1;
else
return n*fact(n-1);
}
Indirect Recursion
A function is said to be indirect recursive if it calls another function and this new function calls the
first calling function again.
int func1(int n)
{
if (n<=1)
return 1;
else
return func2(n);
}
int func2(int n)
{
return func1(n);
}
Q) Recursion versus Iteration
7
i) In recursion, function calls itself until the base condition is reached.
Iteration means repetition of process until the condition fails
ii) Iterative approach involves four steps, initialization, condition, execution and updating.
In recursive function, only base condition (terminate condition) is specified.
iii) Recursion keeps your code short and simple whereas iterative approach makes your code
longer.
iv) Recursion is slower than iteration due to overhead of maintaining stack whereas iteration is faster.
v) Recursion takes more memory than iteration due to overhead of maintaining stack .

Q) Towers of Hanoi problem?


Tower of Hanoi is a mathematical puzzle which consists of three towers (pegs) and more than
one rings is as depicted −

These rings are of different sizes and stacked upon in an ascending order, i.e. the smaller one sits
over the larger one.

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.
Program:
#include<stdio.h>
void move(int,char,char,char);
void main()
{
int n;
clrscr();
printf(" enter the number of disks\n");
scanf("%d",&n);
move(n,'A','C','B');
getch();
}
void move(int n,char s,char d,char a)
{
if(n==1)
8
printf("\n move from %c to %c",s,d);
else
{
move(n-1,s,a,d);
move(1,s,d,a);
move(n-1,s,d,s);
}
}

You might also like