CPP - Function

 

CPP

Function



Fill in the blanks

1.Function call itself called Recursive Function

2. Function overloading is function name  same,argument different.

3.Function advantage Code  Reuse

4. functionname(argument);   called function call

5.Inline function increase execution time and speed of the program

6. 4!  Factorial value =   24

 

Answer the following Question

                                                                         (4 marks)

1.Write a Program for 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();

}

 

2.Write a Program for factorial using Recursion

A Function call itself is called Recursive function

 

#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;

    }

}

3.Write a Program to add two number using Function

#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();

}

 

4. Write a Program to Multiply two number using Function

#include <iostream.h>

#include<conio.h>

 

int mul(int a, int b) {

    return (a * b);

}

 

void main() {

    int multiply;

    multiply = mul(100, 78);

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

    getch();

}

 

5.Write a Program for Default argument using Function

 

#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);

}

No comments:

Post a Comment