Explanation: This program contains a user-defined function named message ( ). It requires
no arguments and returns nothing. It
displays only a message when called.
Without arguments and but with return values:
/* program to receive values from the user defined function without
passing any value
through main (
)*/
void main( )
{
int z;
z=add ( ); /* FUNCTION CALL*/
print(“sum = %d”,z);
}
add ( ) /* FUNCTION DEFINITION*/
{
int a=2,
b=3, y;
y=a+b;
return (y);
}
Calling function
|
Analysis
|
Called function
|
main()
{
int z;
z=add();
printf(“sum =
%d”,z);
}
|
ßNo
arguments
are
passed.
No values are
sent back à
|
add ( )
{
int a=2,b=3,y;
y=a+b;
return(y);
}
|
In this type of function no arguments are passed through the main ( ) function (calling
function). But the called function (add ( )) returns the values. Here the called function is independent. It re data from the keyboard or generates form the initialization and returns the values. In this type both the calling function and called function are partly communicated
with each other.
With arguments
but without return values
In this type of functions arguments are passed to the calling function. The called function
operates
on
the
values. But
no
result is sent
back. This called
function
is
also partly dependent
on the calling function. The
result obtained
is utilized in the called function only.
Ex: /* program to
calculate sum of two numbers
using user-defined function*/
void main ( )
{
void add (int,
int); /* FUNCTION PROTOTYPE
*/
int a,
b;
clrscr( );
printf(“enter
the values of a and
b”);
scanf(“%d%d”,
&a,&b);
add(a,b); /* FUNCTION CALL
*/
getch( );
}
add (int x,int y)
/* FUN DEFINITION*/
{
int z; /*Local
variable declaration*/
z = x+y;
printf(“sum = %d”,z);
}
6
Explanation: in this program two values are passed to add ( ) function. The add ( ) function receives argument from main
( )
and displays sum
of a and b. But it
returns nothing.
Calling function
|
Analysis
|
Called function
|
void main()
{
int a,b;
printf(“enter the
value of
a & b”);
scanf(“%d %d”, &a, &b);
add (a,b );
}
|
ßArguments are passed.
No values are sent back à
|
add (int x, int y )
{
int z; //Local variable
z = x + y;
printf(“ sum = %d”,z);
}
|
With arguments
and return values:
In this type of functions a copy
of actual argument is passed to the formal argument from the
calling function to the called function. Called function operates on those data values and it
will return the result to the calling function. Here data is transferred between calling and called
functions.
Ex: /* program to
calculate sum of two numbers
using user-defined function
*/
void main()
{
void add(int,
int);
/*Function prototype declaration */
int a, b, z;
clrscr( );
printf(“enter the values of
a and
b”);
scanf(“%d %d”, &a, &b);
z=add (a,b); /* Function CALL*/
printf(“sum = %d”, z);
getch();
}
add (int x, int y) /* Function Definition */
{
return (x+y);
}
Calling function
|
Analysis
|
Called function
|
void main()
{
int a, b,
z;
printf(“enter the value of a &
b”);
scanf(“%d %d”, &a, &b);
z = add (a,b );
printf(“ sum =
%d”, z);
getch( );
}
|
ßArguments are
passed. values are send back à
|
add (int x, int y )
{
return ( x
+ y);
}
|
Local variables
& Global variables:-
These are two
types of variables.
a) Local
Variables 7
b) Global
Variables.
a) Local Variables:-
The variables declared inside a function are known as local variables. The values of these
variables
cannot
be accessed
by
any other function.
Example:-
void sum (int a,
int b)
{
int z; //local variable
z=a+b;
return (z);
}
Here z is a Local Variable which is declared in the function sum. So, the value of z is restricted to the function
sum.
Example Program:-
void main()
{
int a=1,b=2;
//local variables
clrscr();
printf(“in main( ), a=%d,b=%d”,a,b);
fun( );
}
void fun()
{
int a=6,b=5;
//local variables
printf(“in fun(
),a=%d,b=%d”,a,b);
}
b) Global
Variables:-
The variables declared outside the main( ) function are known as global variables. These
variables
can
be accessed by all the
functions
of the program.
Example Program:-
int a=3,b=4; /global variables
void main( )
{
clrscr( );
printf(“in main(), a=%d,b=%d”,a,b);
a++; b++; fun(
);
}
void fun( )
{
printf(“in fun(
),a=%d,b=%d”,a,b);
8
Distinguish between global
variables and local variables
Local Variables
|
Global variables
|
1.Local variables
can be used only inside the
function or
the block in which
they are declared
|
1. Global variables can used throughout the
program.
|
2. Local variables get initialized each
time
the function or block containing their declaration is entered.
|
2. Global variables get initialized only once,
before the program starts executing.
|
3. A local variable can contain variables in its
initialize.
|
3. The initial
value that you supply for a
global variable must be constant.
|
4. A local variable loses
its value, the
moment
the function/block containing it is exited. So
we cannot expect a local
variable to remain the value deposited
in it, the
previous time the function/block was
entered.
|
4.Global variables retain their values
throughout their execution.
|
5.Local variables do not get initialized to any
specific value, when a value is not provided. Thus a local variable begins with an
unknown value, which may be different each time.
|
5. All Global variables,
in the absence of
explicit initialization, are automatically initialized. A global
int variable begins
with the value 0, a global
float gets
initialized to
0.0, a global char
holds the ASCII null byte,
and a global pointer
points to NULL.
|
Data transfer between the functions / Parameters Passing:
The technique of passing data from one function to another is known as parameters passing. Parameter passing
can be done in two
ways:
1. Call
by value
2. Call
by reference
Call by value:
In this type value of actual arguments are passed to the formal arguments and the operation is
done on the formal arguments. Any change made in the formal arguments does not affect the actual arguments
because formal arguments are photocopy of actual arguments.
Changes made in the formal arguments are local to the block of the called function. Once the control
returns back to the calling function
the changes
made
vanish.
/* Example
program to send values
by call by
value*/
void main( )
{
int a=2,b=3;
void display(int,
int);
clrscr( );
display(a,
b);
printf(“\n\n In main \t a=%d \t b=%d”,
a, b);
9
getch(
);
}
void display (int x,
int y)
{
printf(“\n In
display( ) before change \t
a=%d \t
b=%d\n”,
x,y);
x=10;
y=12;
printf(“\n In
display( ) after change \t a=%d \t
b=%d \n”,
x, y);
}
/* Example
program on swapping
of
2 numbers by using call by
value */
void swap(int,int);
void main(
)
{
int a,b;
printf(“enter
any two values\n”);
scanf(“%d%d”,&a,&b);
swap(a,b);
}
void swap(int x,int y)
{
int t; t=x; x=y;
y=t;
printf(“after
interchange\n”);
printf(“%d
%d”,x,y);
}
Call by reference:
In this type instead of passing values, addresses (reference) are passed. Function operates on
addresses rather than values. Here the formal arguments are pointers to the actual arguments.
Formal arguments point to the actual
arguments. Hence the changes made in the arguments
are
permanent. i.e., the changes made in formal arguments
will affect the
actual arguments.
/* Example program to send values
by call by reference*/
void main(
)
{
int a=2,b=3;
void display(int*, int*);
clrscr();
display(&a, &b);
printf(“\n\n
in main \t
a=%d \t b=%d”,
a, b);
getch();
}
void display (int *x,
int *y)
{
printf(“\n
in display( ) before change \t a=%d \t b=%d\n”, *x,
*y);
10
*x=10;
*y=12;
printf(“\n in display( ) after
change \t a=%d \t b=%d \n”, *x,
*y);
}
/* Ex 2: Swapping of 2 numbers */
#include<stdio.h>
#include<conio.h> void swap(int *,int *);
void main( )
{
int a,b;
printf(“enter
any two values\n”);
scanf(“%d%d”,&a,&b);
swap(&a,&b);
printf(“after
interchange\n”);
printf(“%d %d”,a,b);
getch(
);
}
void swap(int *x,int *y)
{
int t;
t=*x;
*x=*y;
*y=t;
}
Function returning more values:
We know that a function can return only one value per call. But we can force the function to
return more
than one value per call by using
call by reference.
void main( )
{
int x,y, add,
sub, change (int*,int*,int*,int*);
clrscr();
printf(“\n enter values
of x&y”);
scanf(“%d %d”, &x,
&y); change(&x, &y, &add, &sub);
printf(“\n Addition=%d \n
Subtraction=%d”, add, sub);
getch();
}
change(int *a,
int *b, int *c, int *d)
{
*c=*a+*b;
*d=*a-*b;
}
Explanation: In this program return statement is not used. Still function returns more than one
value. Actually, no values are returned. Once the addresses of the variables are available,
we can directly access
them and modify their
contents.
11
Note:
1. The
memory address
of any variable is unique.
2. If
we
declare the same variable for actual
and
formal arguments, their memory
addresses will be different
from each other.
STORAGE CLASSES:
Storage Classes:- Not only data type is required to declare a variable but its storage class
also has to be mentioned. (or)
The variables declared in C programs are totally different from other languages. We can use
the same variable names in the C program in
separate blocks.
When
we declare a variable it is
available only to specific part or block of the program. Remaining block or other function cannot get access to
the variable.
The area or block of the C program from where the variable can be accessed is known as the
scope of 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
in C.
1.
Function
2.
File
3.
Block
4.
Function prototype
A storage class of variable tells us four things:
(i) Where the
variable would be
stores.
(ii)
The Scope of the variable i.e.,
in which region of the program
the
value of variable
is actually available for
us active.
(iii) Life of the
variable i.e., how long the variable i.e., how
long the variable would be
active
in the program(longevity or alive).
(iv) The initial
value of the variable if
it is not initialized.
Any variable declared
in C
can have any one of the four storage classes:
1. Automatic variables.
2.
External variables.
3.
Static variables.
4.
Register
variables.
The variables
may
also be broadly categorized,
depending on the place of their
declaration, as
1.
Internal variables (local)
2.
External variables (global)
1) Automatic variables are defined inside a function. A variable declared inside a function without storage class name,
by
default is an auto variable.
The features
of automatic variables are:-
(i)
|
Storage :
|
Memory
|
|
(ii)
|
Initial
value :
|
Garbage (or)
unpredictable
|
|
(iii)
|
Scope :
|
Within the function
|
0 comments:
Post a Comment