Functions in C++

 

Functions in C++

Hello! Everyone in the previous blog we have discussed about classes and object in c++ and how to create and how to access these classes and its member function. But in this tutorial we will discuss about function and its overloading in c++

What is function?

As you listened word function and overloading of function first question that will come to your mind that what is this function and why we are studying This.

So i am giving few points about function, so that you can get some idea about function:

1.function is a Self contained block of statements that performs that performs a coherent task of some kind.

2.Every C++ program can be thought of the collection of functions.

3.main() is also a function.

Type of functions

There are mainly two type of functions in c++.

1.Library function

2.user defined functions: that means programmer can create their own function to perform a specific task.

Uses of functions

1.Writting functions avoids rewriting of the same code again and again in the program.

2.Using function large program can be reduced to smaller ones. It is easy to debug and find out error in it.

3.using a function It becomes easier to write program to keep track of what they are doing.

Function prototype

1.A protype statements help the compiler to check the return type an argument type of function.

2.A prototype function consist of the function return type, name and argument list.

3.Example

 1. function returning the max between two numbers

int max(int num1, int num2)

 {

   // local variable declaration

   int result;

   if (num1 > num2)

      result = num1;

   else

      result = num2;

   return result;

}


2. Display a Text

#include <iostream>

using namespace std;

                     // declaring a function

void greet()

{

    cout << "Hello there!";

}

int main()

{

    // calling the function

    greet();

    return 0;

}

Categories of functions

1.A function with no parameter and no return value

2.A function with parameter and no return value

3.A function with parameter and return value

4.A function without parameter and return value

Example1: A function with no parameter and no return value.

#include <iostream>

using namespace std;

void prime();

int main()

{

    // No argument is passed to prime()

    prime();

    return 0;

}

// Return type of function is void because value is not returned.

void prime()

{

    int num, i, flag = 0;

    cout << "Enter a positive integer enter to check: ";

    cin >> num;

    for(i = 2; i <= num/2; ++i)

    {

        if(num % i == 0)

        {

            flag = 1;

            break;

        }

    }

 

    if (flag == 1)

    {

        cout << num << " is not a prime number.";

    }

    else

    {

        cout << num << " is a prime number.";

    }

}

2. A function with parameter and no return value

#include <iostream>

using namespace std;

void prime(int n);

int main()

{

    int num;

    cout << "Enter a positive integer to check: ";

    cin >> num;

   

    // Argument num is passed to the function prime()

    prime(num);

    return 0;

}

// There is no return value to calling function. Hence, return type of function is void. */

void prime(int n)

{

    int i, flag = 0;

    for (i = 2; i <= n/2; ++i)

    {

        if (n%i == 0)

        {

            flag = 1;

            break;

        }

    }

 

    if (flag == 1)

    {

        cout << n << " is not a prime number.";

    }

    else {

        cout << n << " is a prime number.";

    }

}

 

3.A function with parameter and return value

#include <iostream>

using namespace std;

int prime(int n);

int main()

{

    int num, flag = 0;

    cout << "Enter positive integer to check: ";

    cin >> num;

    // Argument num is passed to check() function

    flag = prime(num);

 

    if(flag == 1)

        cout << num << " is not a prime number.";

    else

        cout<< num << " is a prime number.";

    return 0;

}

/* This function returns integer value.  */

int prime(int n)

{

    int i;

    for(i = 2; i <= n/2; ++i)

    {

        if(n % i == 0)

            return 1;

    }

    return 0;

}

 

 

4.A function without parameter and return value

#include <iostream>

using namespace std;

int prime();

int main()

{

    int num, i, flag = 0;

    // No argument is passed to prime()

    num = prime();

    for (i = 2; i <= num/2; ++i)

    {

        if (num%i == 0)

        {

            flag = 1;

            break;

        }

    }

    if (flag == 1)

    {

        cout<<num<<" is not a prime number.";

    }

    else

    {

        cout<<num<<" is a prime number.";

    }

    return 0;

}

// Return type of function is int

int prime()

{

    int n;

    printf("Enter a positive integer to check: ");

    cin >> n;

    return n;

}


I hope this tutorial help you in understanding the concept of function in C++. In next tutorial we will discuss about overloading of function....

read more about my previous tutorial about classes and object.......

Comments

Popular posts from this blog

Decision making with if statement and else..if

Switch statement in C language

Call by value and by reference in function