Site icon i2tutorials

Return statement

vi) Return statement

 

The function is often declared four ways it is also called a function prototype:

A function prototype gives information to the compiler that the function may later be utilized within the program.

 

SYNTAX, EXAMPLE PROGRAMS OF FUNCTION DECLARATIONS (OR) FUNCTION PROTOTYPES:

 

1. Function with no argument & no return value:

 

Syntax:

 

void function_name();
main()
{
Statement1;
 function _name();
Statement2;
}
voidFunction_name()
{
Statement1;
Statement2;
}

 

Example program:

 

//1. Function with no argument & no return value
#include<stdio.h>
void fun(); //function Declaration
void main()
{ fun(); //function calling
printf("to i2tutorials");
}
void fun() //function Definition
{ printf("WELCOME ");
}

 

Output:

 

 

2. Function with no argument but return value:

 

Syntax:

 

return_typefunction_name();
main()
{
Statement1;
Statement2;
function_name();
}
return_typefunction_name()
{
Statement1;
Statement2;
}

 

Example program:

 

//2. Function with no argument but return value
#include<stdio.h>
int sum();//function Declaration
void main()
{
int x=sum();//function calling
printf("entered %d\n", x);
}
int sum() // function Definition
{
int a=5,b=4,s;
s=a+b;
return s;
}

 

Output:

 

 

Here called function is independent and is initialized. The values aren’t passed by the calling function and therefore the calling function and called function are communicated partly with one another.

 

3. Function with argument but no return value:

 

Syntax:

 

void function_name (type1 argument1, type2 argument2);
main()
{
-------
function_name();
-------
}
void function_name(type1 argument1,type2 argument2)
{
-------
-------
}

 

Example:

 

void sum(int a, int b);
main()
{
------
sum(x,y);
------
}
void sum(int a, int b)
{
-------
-------
}

 

Example program:

 

//3. Function with argument but no return value
#include<stdio.h>
void area(float rad);// function Declaration
void main()
{
float rad;
printf("enter the radius:");
scanf("%f",&rad);
area(rad); // function calling
}
void area(float rad)// function Definition
{
float a;
a=3.14*rad*rad;
printf("area of circle=%f",a);
}

 

Output:

 

 

Here the function has an argument therefore the calling function sends data to the called function but called function doesn’t return value.

 

4. Function with argument and return value:

 

Syntax:

 

return_typefunction_name (type1 argument1, type2 argument2);
main()
{
------
Function_name( actual_argument1,actual_argument2);
------
}
return_typefunction_name (type1 argument1, type2 argument2)
{
-------
-------
}

 

Example:

 

int sum(int a, int b);
main()
{
-----
Sum(x,y);
-----
}
int sum(int a, int b)
{
-----
-----
}

 

Example program:

 

//4. Function with argument and return value
#include<stdio.h>
void main()
{
int fun(int); // function Declaration
int a,num;
printf("enter value:\n");
scanf("%d",&a);
num=fun(a);// function calling
printf("number incremented to: %d",num);
}
int fun(int x) // function Definition
{
++x;
return x;
}

  

Output:

 

 

Here the calling function has the argument to pass to the called function and therefore the called function returned value to the calling function.

 

5. Function which returns multiple values (call by reference).

 

We have seen functions that return only one value using a return statement because a return statement can return only value. Suppose that we would like to urge more information from function, we will achieve this in C using arguments not only to receive information but also to send back information to the calling function. The arguments used to “send out” information are called output parameters. The mechanism for sending information by arguments is carried out using the address operator (&) and the indirection operator (*).

 

Example:

 

void operation(int x, int y, int *s, int *d); // function declaration
main()
{
int x=20, y=10,s,d;
operation(x,y,&s,*d); // function call
printf(“s=%d\n and d=%d\n”, s,d);
}
void operation(int a, int b, int *sum, int * diff) // function definition
{
*sum = a+b;
* diff = a-b;
}

 

Exit mobile version