Functions Part 3
Function with arguments and return values
main()
{
================;
================;
Variable
= function( param1, param2 , param3 , …….);Ã
calling
================;
================;
}
return_type function( type arg1,
type arg2, type arg3, ……)
{
===========;
===========;
return
value;
}
return_type depends on type of return value. In C – Language function default return type is int.
When function returning integer value, then its return type is an optional.
#include<stdio.h>
int main()
{
int a=10, b=20, c;
c = sum(a,b);
printf("\n sum value =
%d",c);
return 0;
}
sum(int x, int y)
{
int temp;
temp = x + y;
return temp;
}
#include<stdio.h>
int main()
{
int a=10, b=20, max,min;
max = maximum(a,b);
min = minimum(a,b);
printf("\n max = %d and min =
%d",max,min);
return 0;
}
maximum(int x, int y)
{
if(x>y)
{
return x;
}
return y;
}
minimum(int x, int
y)
{
if(x<y)
{
return x;
}
return y;
}
#include<stdio.h>
int main()
{
int a=10, b=15;
float avg;
avg=average(a,b);
printf("\n avg value =%f ",avg);
return 0;
}
average(int x, int y)
{
float res;
res=(x+y)/2.0;
return res;
}
Reason:
In C – Language function default returns
integer type of data.
In the above example average() terminates
0.5 and returns 12 ( integer part ).
To return data other than integer type of
data, function must have return type.
If function having return type other than
int, then its prototype must be declared.
Function prototype:
It declares name of the function, number
of arguments, type of arguments and return type of the function.
Prototype declaration of a function
main()
{
===========;
===========;
declaration à return_type function( type arg1, type arg2, type arg3,..)
; Ã here name of the arguments is an optional
===========;
===========;
Calling
----Ã variable = function( param1, param2, param3,…….) ;
===========;
===========;
}
Definition:
return_type function( type arg1, type arg2, type
arg3,……….)
{
============;
============;
return
value;
}
Correcting above example:
#include<stdio.h>
int main()
{
int a=10, b=15;
float avg;
float average(int, int);
avg=average(a,b);
printf("\n avg value =%f ",avg);
return 0;
}
float
average(int x, int y)
{
float res;
res=(x+y)/2.0;
return res;
}
Eg:
#include<stdio.h>
int main()
{
int a=10;
float b=20.5;
char ch='a';
display(a,b,ch);
return 0;
}
display(int x, float y, char z)
{
printf("\n %d %f
%c",x,y,z);
}
Reason :
In C-Language
default integer type of data passes through the function arguments. In the
above example only integer data will pass but other type of data will not pass
to function arguments.
To
pass the data other than integer also, function must have return type.
If
function is not returning a value, then
its return type is void.
void:
it is says that no type. It is an empty data type.
Correcting above example.
#include<stdio.h>
int main()
{
int a=10;
float b=20.5;
char ch='a';
void display(int, float, char);
display(a,b,ch);
return 0;
}
void
display(int x, float y, char z)
{
printf("\n %d %f
%c",x,y,z);
}
1. When function must have return type in C –
Language ?
1.if function returning value other than
integer value.
2.if passing data other than integer, then
function must have return type.
When function prototype should be
declared ?
Ans:
If function has
return type other than int.
void display()
{
==========;
==========;
==========;
return
0;
}
The above function is invalid, as void
function does not return value.
void display()
{
==========;
==========;
==========;
Return ;
}
The above function is valid, as it is not
returning any value.
Purpose of return statement: To return value and To terminate function.
A function can have multiple return statements but only one return statement executes.
Eg:
int
maximum( int x, int y)
{
if( x > y )
{
return
x;
}
return y;
}
Note: The user or viewer are free here to compile and run their projects in the below given compiler of each and every program.
0 Comments