Passing Arguments to Function and Return in C++

Passing arguments to Function in C++ programming and using Return keyword to return value. Functions are used to divide a large program into sub-programs for easy control.

Program Passing Arguments and Returning

Program

#include<iostream.h>
#include<conio.h>
int fun(int, int); //defining Integer function with parameters.
int fun(int a, int b) //this is function which Return type is Integer,
{                     //and passing here arguments to parameter.
int c = a + b; //adding 'a' and 'b' value and assigning to 'c'.
return c; //returning 'c' value, U also write like 'return (c);'.
}
void main()
{     //calling Function and passing arguments to parameters and
cout<<"The Returned value is: "<<fun(6, 5); //printing Return value.
 getch();
}
Output
The Returned value is: 11

Program Passing Argument and Returning Another Method

Program

#include<iostream.h>
#include<conio.h>
int fun(int); //defining function with parameter which Return
void main()   //type is Integer.
{
int a, b;
a = 5;
b = fun(a); //calling and passing argument to parameter and
           //assigning Return value to 'b'.
cout<<"The Returned value is: "<<b; //and printing 'b' value.
getch();
}
 int fun(int z) //passing argument here to parameter 'z'.
 {
  int x = z + 5; //adding 5 to 'z' value and assigning to 'x'.
  return x; //Returning 'x' value, U also write like 'return(x);'.
 }
Output
The Returned value is: 10

Program Passing Argument and Returning Another Method

Program

#include<iostream.h>
#include<conio.h>
int fun(int); //defining function with parameter which Return
void main()   //type is Integer.
{
int a = 5;
       //calling and passing argument to parameter and printing
cout<<"The Returned value is: "<<fun(a); //Returned value.
getch();
}
 int fun(int z) //passing argument here to parameter 'z'.
 {
  int x = z + 5; //adding 5 to 'z' value and assigning to 'x'.
  return x; //Returning 'x' value, U also write like 'return(x);'.
 }
Output
The Returned value is: 10