Saturday, June 17, 2023

CPP FUNCTION

 

C++ Functions

Advantage of functions

1) Code Reuse

2) Code optimization

3)Memory execution fast

Declaration of a function

syntax

return_type function_name(data_type parameter...)  

{    

//code to be executed    

}  

Function with Parameters

 

#include <iostream.h>

#include<conio.h>

 

void display(int n1, float n2) { //function definition

    cout << "The int number is " << n1;

    cout << "The double number is " << n2;

}

 

void  main() {

    

     int num1 = 5;

     double num2 = 5.5;

 

    display(num1, num2);   // function call

    getch();

}

Output

The int number is 5

The double number is 5.5

 

 Add Two Numbers

 

#include <iostream.h>

#include<conio.h>

 

int add(int a, int b) {

    return (a + b);

}

 

void main() {

    int sum;

    sum = add(100, 78);

    cout << "sum = " << sum << endl;

    getch();

}

Output

sum = 178

 

Function Overloading

Function name same argument different

#include <iostream.h>

#include<conio.h>

 

void display(int var1, double var2) {

    cout << "Integer number: " << var1;

    cout << " and double number: " << var2 << endl;

}

 

void display(double var) {

    cout << "Double number: " << var << endl;

}

 

void display(int var) {

    cout << "Integer number: " << var << endl;

}

 

void main() {

 

    int a = 5;

    double b = 5.5;

    display(a);

    display(b);

    display(a, b);

    getch();

}

Output

Integer number: 5

Float number: 5.5

Integer number: 5 and double number: 5.5

 

Inline function

It improves the execution time and speed of the program

#include <iostream.h>  

#include<conio.h>

inline int add(int a, int b)  

{  

    return(a+b);  

}  

void main()  

{  

    cout<<"Addition of 'a' and 'b' is:"<<add(2,3);

getch();  

  

}  

output

Addition of 'a' and 'b' is: 5

 

Recursion

A function that calls itself is known as a recursive function.

// Factorial of n = 1*2*3*...*n

#include <iostream.h>

#include<conio.h>

int factorial(int);

void main() {

    int n, result;

    cout << "Enter a non-negative number: \n";

    cin >> n;

    result = factorial(n);

    cout << "Factorial of " << n << " = " << result;

    getch();

}

 

int factorial(int n) {

    if (n > 1) {

        return n * factorial(n - 1);

    } else {

        return 1;

    }

}

Output

Enter a non-negative number: 4

Factorial of 4 = 24

 

Function with Default Argument

#include<iostream.h>

#include<conio.h>

int sum(int x=10,int y=20,int z=30);

void main()

{

cout<<"sum()="<<sum()<<endl;

cout<<"sum(15)="<<sum(15)<<endl;

cout<<"sum(15,25)="<<sum(15,25)<<endl;

cout<<"sum(15,25,35)="<<sum(15,25,35)<<endl;

getch();

}

int sum(int x,int y,int z)

{

return(x+y+z);

}

output

sum()=60

sum(15)=65

sum(15,25)=70

sum(15,25,35)=75

Function with Const Argument

#include<iostream.h>

#include<conio.h>

void main()

{

float cmtot(const int cm);

int cm;

cout<<"Enter the length in cm";

cin>>cm;

cout<<"Meter:"<<cmtot(cm)<<endl;

}

float cmtot(const int cm)

{

return (float)cm/100;

}

 

output

Enter the length in cm:125

Meter:  1.25

 

0 comments:

Post a Comment