<<<    Index     Passing parameters by value >>>

1. Passing structs to functions


  • 
    struct Rectangle {
        int width;  // member variable
        int height; // member variable
        // Constructors
        Rectangle()
        {
            width = 1;
            height = 1;
        }
        Rectangle( int width_  )
        {
            width = width_;
            height = width_ ;
        }
        Rectangle( int width_ , int height_ )
        {
            width = width_;
            height = height_;
        }
        
        void set_dimensions( int width_ , int height_ )
        {
            width = width_;
            height = height_;
        }
        int area() const
        {
            return ( width * height );
        }
    };//struct Rectangle
    
    
  • 
    #include <iostream>
    void normalize( Rectangle rect )
    {
        rect.set_dimensions( 1, 1 );
    }
    int main()
    {
        Rectangle rect( 333, 222 );
        normalize( rect );
        std::cout << "rectangle width: " << rect.width << '\n';
        std::cout << "rectangle height: " << rect.height << '\n';
        std::cout << "rectangle area: " << rect.area() << '\n';
        return 0;
    }
    
    

<<<    Index     Passing parameters by value >>>