Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 5

Page- 1

Q). Passing Parameters:


When a function is called, the calling function has to pass some values to the called function.
There are two ways by to pass the parameters to the function that is.
1. Call by value. (Or) pass by value
2. Call by reference. (Or) pass by reference.

1. Call by value or Pass by value:


“When a function is called by passing values then it is called as “call by value (or) pass by value ””.
In this way any changes are made within the formal arguments,, a actual arguments will not affected.
This is because the scope of the variables restricted from one function to another function.
The following program illustrates the concept of Call by value in C.
#include<stdio.h>
#include<conio.h>
void swap(int, int);
void main()
{
int a=10,b=20;
printf("\n Before swapping %d %d", a,b);
swap(a,b);
printf("\n After swapping %d %d",a,b);
getch();
}
void swap(int a, int b)
{
int temp;
temp=a;
a=b;
b=temp;
printf("\n From swap function%d %d",a,b);
}
Output:

2. Call by Reference:
In the call by value any changes made to formal arguments will not effected to the actual
arguments. This is the one of the disadvantage of call by value. This disadvantage is overcome with call
by reference.

In call by reference the value send to the called function, reference is send to the called function.
So the changes made to the formal arguments will also effect to the actual arguments. This is because
the manipulation has been done through the address (or) via address. In this way we can avoid the
disadvantage of call by value.

The following example explains about call by reference


Aim: To write a C program that illustrates the use of call by reference in C.

#include<stdio.h>
#include<conio.h>
Page- 2

void swap(int *, int *);


void main()
{
int a=10,b=20;
printf("\n Before swapping %d %d",a,b);
swap(&a,&b);
printf("\n After swapping %d %d",a,b);
getch();
}
void swap(int *a,int *b)
{
int *temp;
*temp=*a;
*a=*b;
*b=*temp;
printf("\n\n From swap function %d%d",*a,*b);
}
Output:

Q). Functions with arrays


Explain how to pass the arrays as arguments to the functions

Some times we need to send more values at a time to the called function, in such type of cases
the arrays are suitable to send as an argument to the called function.
To use the arrays as an argument to the functions we need to send the name of the arrays as an
argument.
The following program illustrates the above concept.
#include<stdio.h>
#include<conio.h>
void ABC(int[],int);
void main()
{
int a[5]={10,20,30,40,50}
int n=5, i;
ABC(a,n);
getch();
}
void ABC(int b[], int x)
{
int i;
printf("\nThe array elements are....\n");
for(i=0;i<=x-1;i++)
{
printf("%d\t",b[i]);
}
}
Output

Q). Define a recursive function and explain.


Page- 3

Recursive function:
Recursive function is the process of defining something in terms of itself. “a function is called by
itself is known as a recursive function” and this process is known as recursion.

The recursive functions are very useful for solving mathematical problems.
They are used for calculating factorial of a number, Fibonacci series etc .

Example:

#include<stdio.h>
#include<conio.h>
int fact(int);
void main()
{
int n=5; int f;
f=fact(n);
printf("\nFactorial %d", f);
getch();
}
int fact(int n)
{
if(n==0)
return 1;
else
return n*fact(n-1);
}
Output:

There are two types of recursion functions in c.

1. Direct recursion (Call by itself)


2. Indirect recursion (A function is passed as an argument to another function).
1. Direct Recursion
If a function is call by itself then it is known as a direct recursive function.
The following program illustrates the direct recursive function.
Example:
int func(int n)
{
if(n==0)
return n;
else
return (func(n-1));
}
Output:
2. Indirect recursion
If a function is passed as an argument to another function, then it is called as indirect recursion
and this process is known as indirect recursion.
We can pass not only the values, arrays and address as an argument to a function but we can
also pass a function as an argument to another function.
The following program illustrate the use of indirect recursive function

int func1(int n)
Page- 4

{
if(n==0)
return n;
else
return fun2(n);
}
int func2(int x)
{
return func1(x-1);
}
Output

Q). Differentiate B/w Recursive and Iteration?


RECURSION ITERATIONS
1. It is a function that is partially defined 1. It a instruction based on repetition of a
by itself loop

2. It is used selection structure 2. It is used repetitions structure

3. Recursion will terminated when the 3. It is terminated when the loop is fail
case is recognized.

4. It is slower than the iteration on the 4. It is a faster than recursion.


task.

5. It will use more memory than iteration 5. It consume less memory.

6. Infinity recursion can crash the system 6. Infinity loops uses cpu cycles repeatedly.

Q). Scope of a variables


The variables declared in C programs are totally different from other languages. We can use
same variable names in the C program in separate blocks.
The area or block of the C program from where the variable can be accessed is known as the
scope of the variable. The area or scope of the variable depends on its storage class i.e., where and how
it is declared. There are four scope variables.
1. Block Scope
2. Program Scope
3. Function Scope
4. File Scope
5.
1. Block Scope(Local)

A block scope is also known as local variable. A variable that can be declared inside the block(({})
(or) inside the main() function is also called Block scope.
Ex:
void main()
{
int a=10; //Local(block) variable
printf(“%d”, a);
getch();
Page- 5

}
2. Program Scope(Global)

A Program scope is also known as global variable. A variable that are declared outside of the
block (or) main() function is also called as program scope.
Ex:
int c; //Global variable
void main()
{
int =10, b=20;
c=a+b;
printf(“%d”, c);
getch();
}

3. Function Scope

It is indicate that a variable is active and visible from the beginning to the end of a
function. In C only GOTO labes have uses the labels.
EX:
void main
{
-------
Loop: //function scope
-------
goto loop;
getch();
}

4. File Scope

When a global variable is accessible until the end of the file, the variable is said to have file
scope. The file scope variable can cave declared with the static keyword.
EX:
static int x=20;

You might also like