C programmer handles the C function using three methods - 1. Declaration of function 2. Definition of function 3. Call to the function
Functions
AU:
Dec.-18, May-19, Marks 13
If
certain set of instructions is required frequently then those instructions are
wrapped within a function.
C
programmer handles the C function using three methods -
1.
Declaration of function
2.
Definition of function
3.
Call to the function
For
example
void
main( )
{
void
sum( ); /* declaration of the function*/
sum(
); /*call to the function*/
}
void
sum( ) /*definition of the function*/
}
int
a,b,c;
printf("\n
Enter The two numbers");
scanf("%d
%d",&a,&b);
c=a+b;
printf("\n
The Additon Of two numbers is %d",c);
}
The
syntax for declaration of the function is
Return_type
Function_name(Data_type Parameter);
The
syntax for definition of the function is
Return_type
Function_name(Data_type Parameter)
eqyt
erli
zag oels so W bloy as nonut edt of 191916
{
//body
of function
}
The
syntax for call to the function is
Function_name(Parameters);
1. Parameter Passing Method
Type
1. Passing nothing and returning nothing.
void
main( )
{
void
sum ( ); /*declaration of the function*/
}
void
sum( ) /*definition of the function*/
{
int
a,b,c;
printf("\n
Enter The two numbers");
scanf("%d
%d",&a,&b);
c=a+b;
printf("\n
The Additon Of two numbers is %d",c);
}
In
above example no parameter is passed and there is no return value from the
function. Hence the data type of the above function is void. The void
data indicates that the function is returning nothing or returning NULL.
We
can also pass the argument as void to the function main. The parameter
void to function main indicates that there are no parameters to the function
main. Thus instead of void main() we can write void main(void)
Ex.
1.8.1 Write use of void data.
Solution
:
•
The void data type indicates that the function is returning nothing.
•
If the function is not returning anything from the function then its data type
is specified as void. We can also pass the argument void to the function
to indicate that there are no parameters to the function.
Type
2. Passing the parameter and returning nothing.
Here
we will see a sample C code in which the function is written with some
parameter.
main(
)
{
int
a,b;
void
sum(int a,int b);/*declaration*/
printf("\n
Enter The two numbers");
scanf("%d",&a,&b);
sum(a,b);/*call*/
}
void
sum(int x, int y)/*definition*/
{.
int
c;
c=x+y;
printf("\n
The Addition is %d",c);
}
The
parameters a and b are passed. In the definition we have interpreted them as x
and y. You can take them as a, b respectively or x, y or any other names of
your own choice. It makes no difference. It takes them as a and b only. There
are actually two methods of parameter passing.
1.
Call by Value.
The
above example which we have discussed is of parameter passing by call by value.
This is called by value because the values are passed.
2.
Call by Reference.
In
call by reference the parameters are taken by reference. Pointer variables are
passed as, parameters.
For
example
main()
{
int
a,b;
void
sum(int *,int *);
printf("\n
Enter The Two Numbers");
scanf("%d
%d",&a,&b);
sum(&a,&b);
}
void
sum(int *x,int *y)
{
int
c;
c=*x+*y;
printf("%d",c);
}
Difference
between Call by Value and Call by Reference
Type
3.
Passing
the parameters and returning from the function
In
this method the parameters are passed to the function. And this function
returns some value. If the function is returning integer value then the
data type of that function is int. Thus depending upon the type of data which
is returning from the function the data type of that function is determined. If
nothing is returned form the function then that function has a void data type.
void
main( )
{
int
a,b,c;
int
sum(int,int);/* Only mentioning of data type is allowed
for
the parameters*/
printf("\n
Enter The Two Numbers");
scanf("%d
%d",&a,&b);
c
= sum(a,b);
printf("\n
The Addition Is =%d",c);
}
int
sum()
{
c
= a+b;
return
c; /*returning c which is of int type*/
/*so
data type of sum is int*/
}
Ex.
1.8.2 Write a C program to interchange two variables without using third
variable.
Solution
:
/**************************************
Program
to interchange two variables without using third variable
***************************************/
#include
<stdio.h>
void
main( )
{
int
a=11;
int
b=20;
printf("\na=
%d b=%d",a,b);
a
= a+b;
b
= a-b;
a
= a-b;
printf("\na=
%d b=%d",a,b);
}
Ex.
1.8.3: Write a C program to get two numbers and exchange these numbers using
pass by value and pass by reference. AU: Dec.-18,
Marks 7
Sol.:
#include<stdio.h>
void
swap_by_value(int a, int b)
{
int
temp;
temp
= a;
a
= b;
b
= temp;
}
void
swap_by_ref(int *a, int *b)
{
int
temp;
temp
= *a
*a
= *b;
*b=
temp;
}
int
main( )
{
int
x, y;
printf("\nEnter
First number: ");
scanf("%d",
&x);
printf("\nEnter
Second number: ");
scanf("%d",
&y);
printf("\nBefore
Swaping x = %d and y = %d", x, y);
swap_by_value(x,y);
// Function Call - Pass By Value
printf("\nAfter
Swaping(By Value) x = %d and
y
= %d", x, y);
printf("\nBefore
Swaping x = %d and y = %d", x, y);
swap_by_ref(&x,&y);
// Function Call - Pass By Reference
printf("\nAfter
Swaping (By Reference) x = %d and
y
= %d", x, y);
}
Output
Enter
First number: 10
Enter
Second number: 20
Before
Swaping x = 10 and y = 20
After
Swaping (By Value) x = 10 and y = 20
Before
Swaping x = 10 and y = 20
After
Swaping (By Reference) x = 20 and y = 10
Review Questions
1. Explain with a suitable example, function call by reference
and function call by value.
2. Write suitable 'C' program that creates different results after
passing a parameter by reference and by values.
3. Illustrate pass by value and pass by reference in functions
with an example.
AU May-19, Marks 13
C Programming and Data Structures: Unit I: C Programming Fundamentals : Tag: : Types, Declaration, Definition, Call, Syntax, Example C program - Functions
C Programming and Data Structures
CS3353 3rd Semester EEE, ECE Dept | 2021 Regulation | 3rd Semester EEE Dept 2021 Regulation