functions-unit2 (1)
functions-unit2 (1)
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.
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().
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);
}
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.
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
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
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.
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: