FUNCTION:-
A function is an independent block of statements that perform a specified (logical) task. The specified task is repeated each time the program call the function.
Function can transfer data among themselves through arguments/return values and global variables. Function definitions can occur anywhere in the program, but not inside another function definition.
Any function can call any function, including itself. The calling function is free to ignore the return value if it wishes.
Application of Function:
Function is basically used to break down large computing tasks to smaller ones. They are used for modular development of programs. The use of functions saves a lot of effort because same function can be called from different parts of the program.
Functions are basically used to break down large computing tasks to smaller ones. They are used for modular development of programs.
Type of Function:-
1. Library Function
2. User Defined Function
1. Library Function:-
These functions are not required to be written by the user because they are provided by “C” & can be include in any program by including the header file.
E.g. printf(); , scanf(); , clrscr(); , getch(); .
2. User Defined Function :-
It has to be developed by the user at the time of writing a program.
The user defined function:
i. Needs to be declared
ii. Needs a body
iii. Needs to be called.
Syntax :
function_name(argument list)
{
Local variable declaration;
Statement’s;
.
.
return(expression);
}
CALL BY VALUE:-
In the call by value method the called function creates a new set of variable in stack & copies the values of the arguments into them.
Q. Write a program to swap the two number using function.
#include<stdio.h>
#include<conio.h>
void swap(int x,int y);
void main()
{
int a10,b=20;
clrscr();
printf(“Value of A before function call=%d”,a”);
printf(“Value of A before function call=%d”,b”);
swap(a,b);
printf(“Value of A after function call=%d”,a”);
printf(“Value of A after function call=%d”,b”);
getch();
}
void swap(int x,int y)
{
int tem;
temp=x;
x=y;
y=temp;
printf(“Value of A during function=%d”,x”);
printf(“Value of A during function=%d”,y”);
}
OUTPUT:-
Value of A before function call=10
Value of B before function call=20
Value of A during function=20
Value of B during function=10
Value of A after function call=10
Value of B after function call=20
CALL BY REFERENCE:-
In the call by reference instead of passing values to the function being called references/ pointers to the original variable are passed.
Q. Write a program to swap the two number using function.
#include<stdio.h>
#include<conio.h>
void swap(int *x,int *y);
void main()
{
int a10,b=20;
clrscr();
printf(“Value of A before function call=%d”,a”);
printf(“Value of A before function call=%d”,b”);
swap(&a,&b);
printf(“Value of A after function call=%d”,a”);
printf(“Value of A after function call=%d”,b”);
getch();
}
void swap(int *x,int *y)
{
int temp;
temp=*x;
*x=*y;
*y=temp;
printf(“Value of A during function=%d”,*x”);
printf(“Value of A during function=%d”,*y”);
}
OUTPUT:-
Value of A before function call=10
Value of B before function call=20
Value of A during function=20
Value of B during function=10
Value of A after function call=20
Value of B after function call=10
0 comments:
Post a Comment