Home | | Computer Science 11th std | Different forms of User-defined Function declarations

C++ | Computer Science - Different forms of User-defined Function declarations | 11th Computer Science : Chapter 11 : Functions

Chapter: 11th Computer Science : Chapter 11 : Functions

Different forms of User-defined Function declarations

1. A Function without return value and without parameter 2. A Function with return value and without parameter 3. A Function without return value and with parameter 4. A Function with return value and with parameter

Different forms of User-defined Function declarations

 

1. A Function without return value and without parameter

 

The following program is an example for a function with no return and no arguments passed .

The name of the function is display(), its return data type is void and it does not have any argument.

 

Program 11.20

#include<iostream>

using namespace std;

void display()

{

      cout<<"First C++ Program with Function";

}

int main()

{

      display(); // Function calling statement//

      return(0);

}

Output :

First C++ Program with Function

 

2. A Function with return value and without parameter

 

The name of the function is display(), its return type is int and it does not have any argument. The return statement returns a value to the calling function and transfers the program control back to the calling statement.

 

Program 11.21

#include<iostream>

using namespace std;

int display()

{

      int a, b, s;

      cout<<"Enter 2 numbers: ";

      cin>>a>>b;

      s=a+b;

      return s;

}

int main()

{

      int m=display();

      cout<<"\nThe Sum="<<m;

      return(0);

}

Output :

Enter 2 numbers: 10 30

The Sum=40

 

3. A Function without return value and with parameter

 

The name of the function is display(), its return type is void and it has two parameters or arguments x and y to receive two values. The return statement returns the control back to the calling statement.

 

Program 11 .22

#include<iostream>

using namespace std;

void display(int x, int y)

{

      int s=x+y;

      cout<<"The Sum of Passed Values: "<<s;

}

int main()

{

      int a,b;

      cout<<"\nEnter the First Number         :";

      cin>>a;

      cout<<"\nEnter the Second Number :";

      cin>>b;

      display(a,b);

      return(0);

}

Output :

Enter the First Number :50

Enter the Second Number :45

The Sum of Passed Values: 95

 

4. A Function with return value and with parameter

 

The name of the function is display(), its return type is int and it has two parameters or arguments x and y to receive two values. The return statement returns the control back to the calling statement.

 

Program 11.23

#include<iostream>

using namespace std;

int display(int x, int y)

{

      int s=x+y;

      return s;

}

int main()

{

      int a,b;

      cout<<"\nEnter the First Number          :";

      cin>>a;

      cout<<"\nEnter the Second Number :";

      cin>>b;

      int s=display(a,b);

cout<<”\nExample:Function with Return Value and with Arguments”;

cout<<"\nThe Sum of Passed Values: "<<s; return(0);

}

Output :

Enter the First Number :45

Enter the Second Number :67

Example: Function with Return Value and with Arguments 

The Sum of Passed Values: 112

 

Tags : C++ | Computer Science , 11th Computer Science : Chapter 11 : Functions
Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
11th Computer Science : Chapter 11 : Functions : Different forms of User-defined Function declarations | C++ | Computer Science


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.