Write about Different Categories of Functions in C?

https://www.computersprofessor.com/2016/12/write-about-different-categories-of.html
|
A function depending on whether rays
are present or not and whether a value is returned or not, may belong to one
of the follow categories.
Categories:
2. Functions with arguments and no
return values
3. Functions with arguments and one
return value
4. Functions with no arguments but
return a value
5. Functions that return multiple
values.
|
1. No arguments and no
return values:
When a function has no arguments, it
does not receive any data from the calling function similarly, when it does
not return a value, the calling fiction does not receive any data from the
called function.
There is no data transfer but the
calling function and the called function.
Ex://Funciton declaration
void display ( void);
main ( )
{
display ( );
}
void display ( void) // contains no arguments
{
printf (“Degree College”);
}
|
Arguments but no return
values:
|
The nature of data com: but the
calling function of the called function with arguments but no return value is
shown below.
Ex:
void print ( char ch)
void value ( float p, int n)
The arguments ch, p, n are called
formal arguments.
The calling function can now send
values to these arguments using function call containing appropriate arguments
as
value ( 5.26,8)
Assign 5.26 to p and 8 to n. The
values 5.26 and 8 are called actual arguments with become the values of the
formal arguments inside the called function.
The actual and formal arguments
should match in number, type and order.
The values of actual arguments are
assigned to the formal arguments on a one to one basis, starting with the 1st
argument.
The variables used in actual arguments
must be assigned values before the function call is made.
When a function call is made, only a
copy of the values of actual arguments is passed into the called function.
What occurs inside the function will have no effect on the variables used in
the actual arguments list.
One way data communication
Ex: void printline ( char ch);
main ( )
{
printline (z);
printf ( “\n”);
printline (A);
}
void printline ( char ch)
{
int i;
for ( i =1; i < 10; i++)
printf ( “\t %c”, ch);
printf ( “\n”);
}
OP:
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz
AAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
|
Arguments with return
values:
|
However, we may not always wish to
have the result of a function displayed. We may use it in the calling in for
further processing.
Two –way data communication between
functions
Ex:
int product ( int x, int y)
{
int z;
z = x * y;
return ( z);
}
main ( )
{
int a, b, c;
printf ( “enter 2 values”);
scanf ( “%d% d”, &a,& b);
c=product ( a, b);
printf (“product is %d”c);
geteh ( );
}
C function returns a value of the
type int as the default case when no other type is specified explicitly.
If the function calculates the mean
and standard deviation of a set of values should return either float or
double. In such cases, we must specify the return type in both the function
definition and the prototype declaration.
|
No arguments but
return a value:
|
There could be occasions where we
may need to design functions that may not take any arguments but returns a
value to the calling function.
For example the get char function
has no parameters but it returns as integer type data that represents a
character.
Ex:
int get_no (void);
main ( )
{
int m = get_no ( );
printf (“%d”,m);
}
int get_no ( void)
{
int no;
scanf (“%d”,&no);
return ( no);
}
|
Functions that return
multiple values:
|
Functions that return just one value
using a return statement because a return statement can return only one
value.
|
It we want to get more info. Form a
function we can achieve this in C using the rays not only to receive info;
but also to send back information to the calling function.
The arguments that are used to send
out info one called output parameters.
The mechanism of sending back information
through arguments is achieved using what are known as the address
operator (&) and indirection
operator ( * ).
|
Example:
void mathop (int x, int y, int *s,
int *diff)
{
int x = 20, y = 10, s, d;
mathop(x, y, &s, &d);
printf(“sum=%d \n
difference=%d\n”,s,d);
}
void method ( int a, int b, int *
sum, int * diff)
{
* sum = a+b;
* diff = a-b;
}
The actual arguments x & y are
input arguments s & d are output arguments.
When the function is called the
following assignments occur:
Value of x to a
Value of y to b //
pass the values
Address of S to sum d to diff //
Pass the address where the values of s and d stored
in memory.
The value of a+b is stored in the
location printed by sum.
The value of a-b is stored in the
memory location pointed by diff, while is the same as the location d.
|
|