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

functions in c language

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)
15 views

functions in c language

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/ 4

Questions: Functions:

1. What is a function (modular programming)? Explain about different types of functions?


2. Explain about actual and formal parameters?
3. Advantages or uses of functions in C language?
4. Explain different parameters passing techniques in implemented in C?
Function:
A function is a self contained block of statements which performs some specified and well defined task is called a function. It is also
known as sub program or modular program.
Any function contains four parts.
• 1.Return type- specifies which value is to be returned.
• 2.Function name-it is an identifier to the specified function.
• 3.Arguments list-carrying data from called location to calling location .
• 4.Body-Here we have to write the function body.
Function declaration:
The function name followed by semicolons is known as function declaration.
Syntax; Return type Functionname( arg list);
Function definition:
The function name followed by pair of braces is known as function definition. It can be used to perform a task while calling a
function.
Syntax: <return type><function name><argument list>
{
body
}
Advantages of using functions in C program:
 By using functions we can overwrite the code written over and over again.
 It reduces the length of the program.
 Because of using functions in a program errors will be easier to find errors and debug.
 It increases the readability and reusability of the program.
 We can also create header files which are used to implement our own library.
Types of functions:
1. Built in functions or library functions.
2. User defined functions.
1.Built in Function or library functions:
The function which is already defined and consisting in header file is known as built in functions.These functions can be used in
every program.
E.x printf(),scanf(),etc,..
2.User defined functions:
The function which is defined by the user is known as user defined function.
This function can be used only in the program where it is defined.(only in the current program).
Types of user defined functions:
1.Function without passing parameters and without returning value:
In this program no argument is passed and no value will be returned to the main function..
E.x void sum()
{
int a=20,b=25,c;
c=a+b;
printf(“sum is %d “,c);
}
void main()
{
sum();
}
2.Function without passing parameters and with returning a value.
In this program arguments are not passed but the value is returned to the main function.
E.x int sum()
{
int a=20,b=25,c;
c=a+b;
return c;
}
void main()
{
int x;
x=sum();
Printf(“sum is %d”,c);
}
3.Function with passing parameters and without returning a value.
In this program arguments are passed but the value is not returned to the main function.
E.x: int sum(int x, int y)
{
int c;
c=x+y;
printf(“sum is %d”,c);
}
void main()
{
int a=20;b=25;
sum(a,b);
}
4.Function with passing parameters and with returning a value:
In this program arguments are passed and values will return to the main program.
E.x: int sum(int x, int y)
{
int z;
z=x+y;
return z;
}
void main()
{
int a=20,b=25,c;
c=sum(a,b);
printf(“sum is %d”,c);
}
Parameters or Arguments:
The values which are passed to a function are called parameters or arguments.
It is of two types:
Actual parameters:The parameters which are used in function declaration/calling function are known as actual parameters(sending).
These are used in the main/parent function.
Formal Parameters:The Parameters which are used in function definition/called function are known as formal arguments(receiving).
These are used in the sub program.
Call by Value or Pass by value:
 We can call or pass the value of a variable from one function to another function is known as call/pass by value.
 The value of actual argument is passed to formal parameter and the operation is done on formal arguments.
 If we make any changes in actual arguments automatically formal parameters are changed but make any changes in formal
arguments can’t change in actual parameters.
Rules of calling a functions:
 In calling a functions the actual arguments and formal arguments be of the same data type.
 The order of passing the value should be same.
Call by reference or address:
 In this type instead of passing values address will be passed.
 Here the formal arguments are pointers to the actual arguments.
 If we make any changes in actual arguments automatically formal arguments will change at the same time making any
changes in formal arguments will also change the actual arguments. since both share the same memory locations (both are
refered with the same address)
Write a c program for swapping of two values ?
C a l l b y v a l u e : C a l l b y r e f e r e n c e :
v o i d m a i n ( ) v o i d m a i n ( )
{ {
int x,y; int x,y;
printf(“enter x,y values “); printf(“enter x,y values “);
scanf(“%d %d”,&x,&y); scanf(“%d%d”,&x,&y);
print(“main() x=%d y%d”,x,y); printf(“main() x=%d y=%d”,x,y);
swap(x,y); swap(&x,&y);
print(“main() x=%d y%d”,x,y); printf(“main() x=%d y=%d”,x,y);
} }
Void swap(int a,int b) Void swap(int *a ,int *b)
{ {
int c; Int c;
c=a; c=*a;
a=b; *a=*b;
b=c; *b=c;
printf(“\n swap() a=%d b=%d”,a,b); printf(“\n swap() a=%d b=%d ,*a,*b);
} }
Questions:
Explain about different storage classes? Discuss it’s uses and scope?
Explain the difference between registers and static storage classes?
Explain why registers can store only limited variables?
Storage classes:
In C program each variable has a storage class which decides the following things. It tells the compiler
 Where to store a variable.
 What is the initial value of the variable.
 Life time of the variable.
 Scope of the variable.
Syntax: <storage specifier> data type variable name;
There are four types of storage classes: auto, extern, static, register
1. auto/local auto is a key word. Default storage class is auto type.
 its default value is garbage.
 the memory will be allocated in RAM.
 The scope of the variable is within the function.
 The life of the variable is till the end of the function only.
2. Static: Static is a key word.
 its default value is zero.
 the memory will be allocated in RAM.
 The scope of the variable is Within the function.
 The life of the variable is till the end of the program.
3.Register: register is a key word.
 its default value is garbage.
 the memory will be allocated in CPU Registers.
 The scope of the variable is within the function.
 The life of the variable remains within in the function only.
 For faster accessing of a variable, it is better to go for register than auto.
 Only limited variable can be used as registers since registers size is very low. (16 bits, 32 bits, 64 bits)
4.Extern/global : extern is a key word.
 its default value is garbage.
 the memory will be allocated in RAM.
 The scope of the variable is entire the program.
 The life of the variable is till the end of the program.
Questions:
1.What is Recursion functions? What are the constraints for defining a Recursion Functions? Explain with suitable examples ?
2. What is the difference between recursion and non recursion functions? Explain demerits and merits of using these functions?
3. Write a recursion function for finding the factorial of a numbers?
Recursion Functions:
 A Function calls by itself is called a recursion.
 The recursive function is used to continuously repeated the process instead of using looping process.
 But the recursion process is slower than loops.
 The recursion process is done in stack memory.
Non-Recursion / iteration :
 The Looping Statements are known as Non-Recursion.
 By using loops we can execute set of statements until our conditions get failed.
 Loops are faster than recursive functions.
Q. write a C program to find factorial of a number using recursion and non recursion functions?
Recursion functions: Non-Recursion function:

fact(int x) fact(int x)
{ {
If(x=1) int f=1;
return 1; while(x>0)
else {
x=x*fact(x-1); f=f*x;
return x; x--;
} }
void main() return f;
{ }
int n,f; void main()
printf(“enter n number “); {
scanf(“%d”,&n); int n,f;
f=fact(n); printf(“enter n value “);
printf(“%d factorial is %d”,n,f); scanf(“%d”,&n);
} f=fact(n);
printf(“%d factorial is %d”,n,f);
}

You might also like