-->





Translate

Showing posts with label cpp_tutorial. Show all posts
Showing posts with label cpp_tutorial. Show all posts

Differ pass by value and reference c++ example

Leave a Comment
Pass by value in C++ Programming
In this method we call a function by passing a value. A new copy of value is made and send to functions body. Where operations are applied on new copied value.




Advantages
  • New copy is made of value after passing so if changes go wrong the original data will be save
  • It increase the security of data
Disadvantage
  • If the data is huge making too much copies may cause system over head.

Pass by reference in C++ Programming
In this method we call a function by its reference using address operator. It don't make a copy of data and pass the address of value to the function. Where operations are applied on the reference of value and changes made permanently to data.

Advantages
  • When data is large it reduce the copies of data and creates less system overhead.
Disadvantage
  • Security of data can be weak because the real information is accessible and can be changed 


C++ function example of both ways pass by value and pass by reference
In this example there are two functions one calculates a cube of number with value and other calculates cube of number with reference. With value returns result and with reference not return result.

#include<iostream>
using namespace std;

int pass_Value(int);
void pass_reference(int &);
int main()
{
    int num1,num2;

    cout<<"Enter First Number: ";cin>>num1;
    cout<<"Enter Second Number: ";cin>>num2;

    cout<<"\n\n\n";

    int result=pass_Value(num1);
    cout<<"\t\tPass By Value Result: "<<result<<endl;

    pass_reference(num2);
    cout<<"\t\tPass By Reference Result: "<<num2<<endl;

    return 0;

}

int pass_Value(int number)
{
    return (number*number*number);
}

void pass_reference(int &number)
{
    number=number*number*number;
}


Input Output Example:

see also: Learn Pointers in C++ Programming with Examples


Explanation of functions

  • First function is simple we received a value calculate its cube and return simply the new result.
  • In second function we applies changes using reference of number we sent so no need to return here. As changes are made permanently.



See More Examples Here: C++ Simple Examples
Read More...

c++ program to find area of circle using functions

Leave a Comment
Cpp tutorial to calculate area of circle
  •  User enter radius or diameter of circle.
  •  Program should have two functions one which takes radius as parameter and second takes diameter as parameter.
  •  If user enters wrong option then tell users its valid selection and chose again the right option.
This Cpp Tutorial covers the concept of
  • if else if statement
  • for loop
  • function with parameter and return value
Cpp code compiler used: Codeblocks C++ Compiler

#include <iostream>
#define PI 3.14159
using namespace std;

float AreaOfCircle(float radius);
float AreaWithDiameter(float diameter);

int main()
{
    float radius,diameter,circleArea;
    char choice='0';
    cout<<"\n\t\t\tFind Area Of Circle:"<<endl;


    for(;choice!='1'&&choice!='2';)
        {
         cout<<"\nEnter 1 to Enter Radius OR
                 2 to Enter Diameter: ";
         cin>>choice;
         if(choice!='1'&&choice!='2')
         cout<<"\n\t\tEnter a VALID Option ";

        }

    if(choice=='1')
    {
    cout <<"\n\t\tEnter Radius To Find Area: ";
    cin>>radius;
    circleArea=AreaOfCircle(radius);
    }
    else if(choice=='2')
    {
    cout <<"\n\t\tEnter Diameter To Find Area: ";
    cin>>diameter;
    circleArea=AreaWithDiameter(diameter);

    }
    cout<<"\n\n\t\tArea of Circle is:->> "<<circleArea<<endl;
    return 0;
}

float AreaOfCircle(float radius)
{
    return (PI*(radius*radius));

}

float AreaWithDiameter(float diameter)
{

    return (AreaOfCircle(diameter/2));
}


output of program
calculate area of circle cpp tutorial

Program explanation
  • Program has two functions one for radius and one for diameter both called upon user choice
  • Both calculates the radius and diameter according to formula and return result value from where they have called
  • A for loop is used to prevent the wrong choice selection it will break only if user enters valid option 1 or 2 else it will continue to ask user to enter valid option
Read More...

Cpp Function to Find Largest and smallest number in array

1 comment
This Cpp tutorial contains
 Compiler used: CodeBlocks Cpp Compiler

Write a program which generates some random number and store in array after it program pass array to a function which calculates maximum and minimum number in array and display it use any Cpp Compiler to code.

How program works
  • Declare array of size 10
  • Using for loop assign array indexes with random values between 1 and thousand
  • Call the function and pass array and its size as argument
  • Function declares two integers max and min and assign both integers with arrays first index value
  • Then with in for loop there are two if condition first check is for minimum number and second check is for maximum number
  • Finally program display the output values of both integers min and max

      Cpp Code
  1.  #include <iostream>
  2. #include <stdlib.h>
  3. using namespace std;
  4. void FindMaxMin(int *array, int size)
  5. {        int min,max;
  6.          min=max=array[0];
  7.          for(int i=0;i<size;i++)
  8.          {
  9.              if(array[i]<min)
  10.                min=array[i];
  11.             else if(array[i]>max)
  12.                max=array[i];
  13.          }
  14.  cout<<"Minimum Number  = "<<min<<endl;
  15.  cout<<"Maximum Number = "<<max<<endl;
  16. }
  17. int main()
  18. {
  19.     int array[10];
  20.     for(int i=0;i<=9;i++)
  21.         {
  22.          array[i]=rand()%1000+1;
  23.          cout<<"array ["<<i<<"]"<<"= "<<array[i]<<endl;
  24.         }
  25.     FindMaxMin(array,10);
  26. return 0;
  27. }
 Sample Output
Cpp Function To Find Largest And Smallest Number in Array


Another Example without Function more simple
Find Maximum and Minium Number in Array Cpp Code

More Cpp Program Tutorials
C++ Simple Examples
Read More...