<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: Igor</title>
    <description>The latest articles on Forem by Igor (@igorcastilhos).</description>
    <link>https://forem.com/igorcastilhos</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F1132738%2F478b79b7-ea1d-48b7-8689-9dff2c53faab.png</url>
      <title>Forem: Igor</title>
      <link>https://forem.com/igorcastilhos</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/igorcastilhos"/>
    <language>en</language>
    <item>
      <title>Connecting a React Frontend with a Node.js Express Backend</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Tue, 04 Feb 2025 16:36:56 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/connecting-a-react-frontend-with-a-nodejs-express-backend-4d82</link>
      <guid>https://forem.com/igorcastilhos/connecting-a-react-frontend-with-a-nodejs-express-backend-4d82</guid>
      <description>&lt;p&gt;In this project, you will learn how to establish communication between a React-based frontend and a Node.js backend using Express.&lt;/p&gt;

&lt;h2&gt;
  
  
  Project Setup
&lt;/h2&gt;

&lt;p&gt;First, create a folder named &lt;strong&gt;&lt;code&gt;simple_project&lt;/code&gt;&lt;/strong&gt; in your preferred location and open it in your favorite IDE.&lt;/p&gt;

&lt;p&gt;Now, let's create the project structure. It will be simple:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;frontend&lt;/code&gt;&lt;/strong&gt; folder (React app)&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;backend&lt;/code&gt;&lt;/strong&gt; folder (Node.js API)&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Setting Up the Frontend
&lt;/h3&gt;

&lt;p&gt;Inside the &lt;strong&gt;&lt;code&gt;frontend&lt;/code&gt;&lt;/strong&gt; folder, we will use Vite to create our React application along with Tailwind CSS. Navigate into the folder before running the command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm create vite@latest
Project name: &lt;span class="nb"&gt;.&lt;/span&gt;
Select a framework: React
Select a variant: JavaScript
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, install the dependencies:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Next, install Tailwind CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;tailwindcss @tailwindcss/vite
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Your &lt;strong&gt;&lt;code&gt;frontend&lt;/code&gt;&lt;/strong&gt; folder should now have the following structure:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;node_modules
public
src
  ├── App.jsx
  ├── index.css
  ├── main.jsx
.gitignore
...
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Configuring Vite
&lt;/h3&gt;

&lt;p&gt;Modify &lt;strong&gt;&lt;code&gt;vite.config.js&lt;/code&gt;&lt;/strong&gt; to include Tailwind CSS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;defineConfig&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;vite&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;tailwindcss&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;@tailwindcss/vite&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;react&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="s1"&gt;@vitejs/plugin-react&lt;/span&gt;&lt;span class="dl"&gt;'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nf"&gt;defineConfig&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
  &lt;span class="na"&gt;plugins&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nf"&gt;react&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="nf"&gt;tailwindcss&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Creating the App Component
&lt;/h3&gt;

&lt;p&gt;Edit &lt;strong&gt;&lt;code&gt;App.jsx&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"min-h-screen w-full bg-gray-100 flex items-center justify-center flex-col gap-10"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-5xl font-bold text-gray-800"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Backend with Express and Node
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"rounded-2xl shadow-lg bg-white space-y-3"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  Setting Up the Backend
&lt;/h2&gt;

&lt;p&gt;To create the backend, navigate to the &lt;strong&gt;&lt;code&gt;backend&lt;/code&gt;&lt;/strong&gt; folder:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nb"&gt;cd &lt;/span&gt;backend
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Initialize a &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; file:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm init &lt;span class="nt"&gt;-y&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Modify &lt;strong&gt;&lt;code&gt;package.json&lt;/code&gt;&lt;/strong&gt; to set &lt;strong&gt;&lt;code&gt;main&lt;/code&gt;&lt;/strong&gt; as &lt;strong&gt;&lt;code&gt;server.js&lt;/code&gt;&lt;/strong&gt; and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="nl"&gt;"type"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"module"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, install Express and CORS:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;express cors
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Create a file named &lt;strong&gt;&lt;code&gt;server.js&lt;/code&gt;&lt;/strong&gt; inside the &lt;strong&gt;&lt;code&gt;backend&lt;/code&gt;&lt;/strong&gt; folder and add the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;express&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;cors&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;cors&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;express&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;CORS_OPTIONS&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="na"&gt;origin&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:5173&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;};&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;use&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;cors&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CORS_OPTIONS&lt;/span&gt;&lt;span class="p"&gt;));&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;/&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;req&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;json&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
    &lt;span class="na"&gt;blogPosts&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;Neymar's Return to Santos and Its Impact on Grêmio&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Grêmio will face tougher competition now that Neymar is back at Santos.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;},&lt;/span&gt;
      &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="na"&gt;title&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;The Importance of Digital Marketing for Your Business&lt;/span&gt;&lt;span class="se"&gt;\"&lt;/span&gt;&lt;span class="s2"&gt;,&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
        &lt;span class="na"&gt;content&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Digital marketing is a powerful tool to boost your business growth.&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;
      &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;]&lt;/span&gt;
  &lt;span class="p"&gt;});&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;

&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;listen&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;8080&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;log&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;Server is running on port 8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Since the frontend and backend run on different ports (&lt;strong&gt;5173&lt;/strong&gt; and &lt;strong&gt;8080&lt;/strong&gt;), we use &lt;strong&gt;CORS&lt;/strong&gt; to allow communication between them.&lt;/p&gt;




&lt;h2&gt;
  
  
  Fetching Data from the Frontend
&lt;/h2&gt;

&lt;p&gt;To fetch blog posts from the backend, navigate back to the &lt;strong&gt;&lt;code&gt;frontend&lt;/code&gt;&lt;/strong&gt; folder and install &lt;strong&gt;axios&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;npm &lt;span class="nb"&gt;install &lt;/span&gt;axios
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Update &lt;strong&gt;&lt;code&gt;App.jsx&lt;/code&gt;&lt;/strong&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight jsx"&gt;&lt;code&gt;&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;axios&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="k"&gt;import&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;useState&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;from&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;react&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;App&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;useState&lt;/span&gt;&lt;span class="p"&gt;([]);&lt;/span&gt;

  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fetchData&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;async &lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;response&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nx"&gt;axios&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;get&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;http://localhost:8080&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nf"&gt;setData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;response&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;blogPosts&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
  &lt;span class="p"&gt;};&lt;/span&gt;

  &lt;span class="nf"&gt;useEffect&lt;/span&gt;&lt;span class="p"&gt;(()&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nf"&gt;fetchData&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
  &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="p"&gt;[]);&lt;/span&gt;

  &lt;span class="k"&gt;return &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"min-h-screen w-full bg-gray-950 flex items-center justify-center flex-col gap-10"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-5xl font-bold text-gray-100"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        Backend with Express and &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-green-400"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt; Node&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;span&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"rounded-2xl shadow-lg p-5 bg-white space-y-3"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;map&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt; &lt;span class="na"&gt;key&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;index&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"bg-sky-100 p-4 rounded-2xl hover:scale-105 transition-transform"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-xl font-semibold text-gray-800"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;title&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
            &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt; &lt;span class="na"&gt;className&lt;/span&gt;&lt;span class="p"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"text-sm text-gray-600"&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&lt;span class="si"&gt;{&lt;/span&gt;&lt;span class="nx"&gt;post&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;content&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
          &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
        &lt;span class="p"&gt;))&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;
      &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
    &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;export&lt;/span&gt; &lt;span class="k"&gt;default&lt;/span&gt; &lt;span class="nx"&gt;App&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;strong&gt;&lt;code&gt;useState&lt;/code&gt;&lt;/strong&gt; hook stores the fetched data, and &lt;strong&gt;&lt;code&gt;useEffect&lt;/code&gt;&lt;/strong&gt; ensures the data is retrieved when the component mounts.&lt;/p&gt;




&lt;h2&gt;
  
  
  Running the Application
&lt;/h2&gt;

&lt;p&gt;Start both the frontend and backend simultaneously. Open two terminal windows and run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="c"&gt;# In the frontend folder&lt;/span&gt;
npm run dev

&lt;span class="c"&gt;# In the backend folder&lt;/span&gt;
node server.js
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, open &lt;strong&gt;&lt;code&gt;http://localhost:5173/&lt;/code&gt;&lt;/strong&gt; in your browser. You should see the blog posts displayed on the screen.&lt;/p&gt;

&lt;p&gt;Congratulations! You have successfully connected a React frontend with a Node.js backend using Express.&lt;/p&gt;

</description>
      <category>react</category>
      <category>node</category>
      <category>express</category>
      <category>fullstack</category>
    </item>
    <item>
      <title>Go history</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Mon, 22 Apr 2024 01:31:01 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/go-history-5184</link>
      <guid>https://forem.com/igorcastilhos/go-history-5184</guid>
      <description>&lt;p&gt;Go was conceived in September 2007 by Robert Griesemer, Rob Pike, and Ken Thompson, all at Google, and was announced in November 2009. &lt;/p&gt;

&lt;p&gt;Go is especially suited for building infrastructure like networked servers, and tools and system for programmers.&lt;/p&gt;

&lt;p&gt;Go is an open-source project, so source code for its compiler, libraries, and tools is freely available to anyone.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzo1ttmr2nwkls85xill2.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzo1ttmr2nwkls85xill2.png" alt="Go history tree" width="800" height="675"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Go is described as a “C-like language”. From C, Go inherited its expression syntax, control-flow statements, basic data types, call-by-value parameter passing, pointers, and above all, C’s emphasis on programs that compile to efficient machine code. &lt;/p&gt;

&lt;p&gt;Modula-2 inspired the package concept. Oberon eliminated the distinction between module interface files and module implementation files. Oberon-2 influenced the syntax for packages, imports, and declarations, and Object Oberon provided the syntax for method declarations.&lt;/p&gt;

&lt;p&gt;In CSP (or &lt;strong&gt;&lt;em&gt;Communicating sequential process&lt;/em&gt;&lt;/strong&gt;), a program is a parallel composition (simultaneous execution of computations) of processes that have no shared state; the process communicate and synchronize using channels. Hoare’s CSP was a formal language for describing the fundamental concepts of concurrency, not a programming language for writing executable programs.&lt;/p&gt;

&lt;p&gt;Pike and others began to experiment with CSP implementations as actual languages. They created Squeak, which provided a language for handling mouse and keyboards events, with statically created channels. It was a purely functional language with garbage collection. Channels became first-class values, dynamically created and storable in variables.&lt;/p&gt;

&lt;p&gt;Go’s innovative slices provide dynamic arrays with efficient random access but also permit sophisticated sharing arrangements reminiscent of linked lists.&lt;/p&gt;

&lt;p&gt;It has Garbage Collection, a package system, first-class functions, lexical scope, a system call interface, and immutable string in which text is generally encoded in UTF-8.&lt;/p&gt;

&lt;p&gt;Its built-in data types and most library data structures are crafted to work naturally without explicit initialization or implicit constructors, so relatively few memory allocations and memory writes are hidden in the code. Go’s aggregate types (structs and arrays) hold their elements directly, requiring less storage and fewer allocations and pointer indirections than languages that use indirect fields. Go has concurrency features based on CSP. The variable size stacks of Go’s lightweight threads or goroutines are initially small enough that creating one is cheap and creating a million is practical.&lt;/p&gt;

</description>
      <category>go</category>
      <category>backend</category>
      <category>history</category>
    </item>
    <item>
      <title>Sistemas Operacionais - Parte 2</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Thu, 04 Apr 2024 17:31:54 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/sistemas-operacionais-parte-2-bng</link>
      <guid>https://forem.com/igorcastilhos/sistemas-operacionais-parte-2-bng</guid>
      <description>&lt;p&gt;O que é um Sistema Operacional?&lt;br&gt;
É difícil cravar o que um SO (Sistema Operacional) é além dizer que é um software que roda no modo Kernel - e até isso às vezes não é verdade. Parte do problema é que SOs executam duas funções essenciais não-relacionadas:&lt;br&gt;
Fornecer um conjunto abstrato e objetivo de recursos ao invés da bagunça vinda do hardware e gerenciar esses recursos.&lt;/p&gt;




&lt;p&gt;A arquitetura (conjunto de instruções, organização da memória, I/O e estrutura de barramento) da maioria dos computadores no nível de linguagem da máquina é primitivo e estranho de programar, especialmente a parte de Entrada/Saída.&lt;br&gt;
Todos os SOs fornecem uma camada de abstração para usar discos: os arquivos.&lt;br&gt;
Usando essa abstração, programas podem criar, escrever e ler arquivos, sem precisar lidar com os detalhes de como o hardware realmente funciona.&lt;br&gt;
Essa abstração é a chave para gerenciar toda essa complexidade. Boas abstrações tornam uma tarefa impossível em duas gerenciáveis.&lt;br&gt;
A primeira é definir e implementar as abstrações.&lt;br&gt;
A segunda é usar essas abstrações para resolver o problema atual.&lt;/p&gt;




&lt;p&gt;O "arquivo" é uma abstração muito conhecida e muito útil. Um arquivo é como uma foto digital, mensagens salvas de e-mails, músicas ou páginas web.&lt;br&gt;
É muito mais fácil lidar com fotos, emails, músicas e páginas web do que com detalhes de SATA (ou outros) discos.&lt;br&gt;
O trabalho do SO é criar boas abstrações e, então, implementar e gerenciar os objetos abstratos assim criados.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Aprendizados sobre sistemas operacionais – Parte 1</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Sun, 17 Mar 2024 16:21:34 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/aprendizados-sobre-sistemas-operacionais-parte-1-1i38</link>
      <guid>https://forem.com/igorcastilhos/aprendizados-sobre-sistemas-operacionais-parte-1-1i38</guid>
      <description>&lt;p&gt;&lt;em&gt;Neste artigo falarei sobre conceitos que eu aprendi lendo os livros “Modern Operating Systems” do Tanenbaum e “Understanding the Linux Kernel” de Daniel Bovet &amp;amp; Marco Cesati.&lt;/em&gt;&lt;br&gt;
Vamos para o que interessa:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Um computador moderno consiste em um ou mais processadores, alguma quantidade de memória principal (RAM), discos rígidos (HDD/SSD), impressoras, um teclado, um mouse, um monitor, interfaces de rede e vários outros dispositivos de entrada/saída (I/O).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrq3o3sj19xjymq3u52p.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdrq3o3sj19xjymq3u52p.png" alt="Imagem representando a comunicação entre software e hardware" width="531" height="315"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; O hardware consiste de chips, placas, discos, um teclado e outros objetos físicos. Em cima do hardware fica o software.&lt;/li&gt;
&lt;li&gt;A maioria dos computadores possuem 2 modos de operação: o modo Kernel e o modo Usuário. O sistema operacional, a peça de software mais fundamental (como Windows, Linux, MacOS), executa no modo Kernel (ou modo de supervisor). Nesse modo, ele tem acesso completo a todo o hardware e pode executar qualquer instrução que a máquina seja capaz de executar. O resto do software roda no modo de usuário, em que somente um conjunto menor de instruções de máquina estão disponíveis. Em particular, as instruções que afetam o controle da máquina ou que realizam I/O são proibidas no modo usuário (programas de aplicações).&lt;/li&gt;
&lt;li&gt;O  “User Interface Program”, shell ou GUI (Graphical User Interface), é o software de modo usuário de nível mais baixo e eles permitem o usuário iniciar outros programas.&lt;/li&gt;
&lt;li&gt;Uma distinção importante entre o SO e softwares normais é que se um usuário não gostar de um certo programa de email, ele é livre para usar um diferente ou escrever o seu próprio; ele não é livre para escrever o seu próprio clock interrupt handler, que faz parte do SO e é protegido pelo hardware contra tentativas do usuário de modifica-lo.&lt;/li&gt;
&lt;li&gt;Alguns sistemas embarcados (embedded software) podem não conter um modo kernel.&lt;/li&gt;
&lt;li&gt;Existem programas que permitem o usuário trocar de senha, ou seja, eles possuem funções privilegiadas. Eles não fazem parte do SO e não são executados no modo Kernel, portanto, devem ser protegidos de uma forma especial.&lt;/li&gt;
&lt;li&gt;O código fonte de sistemas operacionais chegam a 5 milhões de linhas de código ou mais.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>How to choose the best data structure to solve a problem.</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Mon, 05 Feb 2024 00:20:32 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/how-to-choose-the-best-data-structure-to-solve-a-problem-4ndf</link>
      <guid>https://forem.com/igorcastilhos/how-to-choose-the-best-data-structure-to-solve-a-problem-4ndf</guid>
      <description>&lt;ol&gt;
&lt;li&gt;Analyse your problem to determine the operations that must be suported. Examples may include inserting a data item into the data structure, deleting, and finding a specified data item.&lt;/li&gt;
&lt;li&gt;Quantify the resource constraints for each operation.&lt;/li&gt;
&lt;li&gt;Select the data structure that best meets these requirements.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;This three-step approach to selecting a dsa operationalizes a data-centered view of the design process. The &lt;strong&gt;first concern&lt;/strong&gt; is for the data and the operations to be performed on them, the &lt;strong&gt;next concern&lt;/strong&gt; is the representation for those data, and the &lt;strong&gt;final concern&lt;/strong&gt; is the implementation of that representation.&lt;br&gt;
Resource constraints on certain key operations, such as search, inserting data records, and deleting data records, normally drive the data structure selection process.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Are all data items inserted into the data structure at the beginning, or are insertions interspersed with other operations?&lt;/li&gt;
&lt;li&gt;Can data items be deleted? If so, this will probably make the implementation more complicated.&lt;/li&gt;
&lt;li&gt;Are all data items processed in some well-defined order, or is search for specific data items allowed? "Random access" search generally requires more complex data structures.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;&lt;strong&gt;Example&lt;/strong&gt;: A bank must support many types of transactions with its customers. From a database perspectice, we see that ATM transaction do not modify the database significantly. For simplicity, assume that if money is added or removed, this transaction simply changes the valued stored in an account record. Adding a new account to the database is allowed to take several minutes. Deleting an account need have no time constraint, because from the customer's point of view all that matters is that all the money be returned (equivalent to a withdrawal). Typically they are not willing to wait more than a brief time for individual account transactions such as a deposit or withdrawal.&lt;br&gt;
When considering the choice of data structure to use in the database system that manages customer accounts, we see that a data structure that has little concern for the &lt;strong&gt;cost of deletion&lt;/strong&gt;, but is highly efficient &lt;strong&gt;for search&lt;/strong&gt; and moderately efficient for &lt;strong&gt;insertion&lt;/strong&gt;, should meet the resource constraints imposed by this problem. Records are accessible by unique account number. One data structure that meets these requirements is the &lt;strong&gt;hash table&lt;/strong&gt;. &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hash Tables allow for &lt;strong&gt;extremely fast exact-match search&lt;/strong&gt;. A record can be modified quickly when the modification does not affect its space requirements.&lt;/li&gt;
&lt;li&gt;Hash Tables also support efficient insertion of &lt;strong&gt;new records.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;While **deletions **can also be supported efficiently, too many deletions lead to some degradation in performance for the remaining operations.&lt;/li&gt;
&lt;li&gt;However, the hash table &lt;strong&gt;can be reorganized periodically to restore the system to peak efficiency&lt;/strong&gt; (search for &lt;strong&gt;load factor&lt;/strong&gt;. Such reorganization can occur offline so as not to affect ATM transactions.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Overloading Functions</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Fri, 01 Dec 2023 16:28:45 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/overloading-functions-4186</link>
      <guid>https://forem.com/igorcastilhos/overloading-functions-4186</guid>
      <description>&lt;ul&gt;
&lt;li&gt;We can have functions that have different parameter lists that have the same name&lt;/li&gt;
&lt;li&gt;This is a great use of abstraction since as a developer all i need to think is display and pass in whatever information i need. I don’t have to keep track of dozens of different function names.&lt;/li&gt;
&lt;li&gt;In SWE, we have a principal called polymorphism, which means many forms for the same concept. This is an example of polymorphism.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;           &lt;span class="c1"&gt;// add ints&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;  &lt;span class="c1"&gt;// add doubles&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;main&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mi"&gt;20&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;       &lt;span class="c1"&gt;// integer    &lt;/span&gt;
    &lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;add_numbers&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;10.0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="mf"&gt;20.0&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;   &lt;span class="c1"&gt;// double&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;The parameter list for these functions must be different so the compiler can tell them apart. Once these functions are implemented, I can call display and pass in my data. The compiler will check the type of the argument in the function and match it to one of the available overloaded display functions.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;s&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="kt"&gt;void&lt;/span&gt; &lt;span class="nf"&gt;display&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;vector&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="n"&gt;string&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;If it can’t match it or if it can’t convert the argument to one that matches, then we get a compiler error.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Important -&lt;/strong&gt; There is one restriction to function overloading. The return type is not considered when the compiler is trying to determine which function to call.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;get_value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nf"&gt;get_value&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;

&lt;span class="c1"&gt;// Error&lt;/span&gt;
&lt;span class="n"&gt;cout&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="n"&gt;get_value&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&amp;lt;&lt;/span&gt; &lt;span class="sc"&gt;'\n'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;  &lt;span class="c1"&gt;// which one?&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Here we have two overloaded functions, both called &lt;code&gt;get_value&lt;/code&gt; and both expect no parameters. The only difference is that one returns an integer and the other a double. This will produce a compiler &lt;strong&gt;error&lt;/strong&gt; since the only difference is the &lt;strong&gt;return type.&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Overloading function is used extensively in object-oriented design.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Vectors in C++</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Thu, 23 Nov 2023 18:37:06 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/vectors-in-c-28dd</link>
      <guid>https://forem.com/igorcastilhos/vectors-in-c-28dd</guid>
      <description>&lt;ul&gt;
&lt;li&gt;
&lt;p&gt;Vectors&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What they are&lt;/li&gt;
&lt;li&gt;Advantages vs arrays&lt;/li&gt;
&lt;li&gt;Declaration and initialization&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;&lt;p&gt;Suppose we want to store test scores for my school&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;I have no way of knowing how many students will register next year&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;

&lt;p&gt;Options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Pick a size that you are not likely to exceed and use static arrays&lt;/li&gt;
&lt;li&gt;Use a dynamic array such as vector&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;h3&gt;
  
  
  What is a vector?
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;A &lt;strong&gt;vector&lt;/strong&gt; is a sequence of elements that you can access by an index. It is a container in the C++ Standard Template Library (STL)&lt;/li&gt;
&lt;li&gt;An array that can grow and shrink in size at &lt;strong&gt;execution&lt;/strong&gt; time&lt;/li&gt;
&lt;li&gt;Provides similar &lt;strong&gt;semantics&lt;/strong&gt; and &lt;strong&gt;syntax&lt;/strong&gt; as arrays&lt;/li&gt;
&lt;li&gt;Very &lt;strong&gt;efficient&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Can provide &lt;strong&gt;bounds checking&lt;/strong&gt;
&lt;/li&gt;
&lt;li&gt;Can use lots of functions like sort, reverse, find, and more.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Declaring
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="cp"&gt;#include&lt;/span&gt; &lt;span class="cpf"&gt;&amp;lt;vector&amp;gt;&lt;/span&gt;&lt;span class="cp"&gt;
&lt;/span&gt;&lt;span class="k"&gt;using&lt;/span&gt; &lt;span class="k"&gt;namespace&lt;/span&gt; &lt;span class="n"&gt;std&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vowels&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;test_scores&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;// Using a constructor initialization syntax&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vowels&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;test_scores&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Unlike arrays, the 10 integers will automatically be set to 0.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Initializing
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;char&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;vowels&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="sc"&gt;'a'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'e'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'i'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'o'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="sc"&gt;'u'&lt;/span&gt; &lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;test_scores&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;79&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;93&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt;
&lt;span class="n"&gt;vector&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;double&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;hi_temperatures&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;365&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;85.0&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// (initial_size, initial_value)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Characteristics
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Dynamic size&lt;/li&gt;
&lt;li&gt;Elements are all the same type&lt;/li&gt;
&lt;li&gt;Stored contiguously in memory&lt;/li&gt;
&lt;li&gt;Individual elements can be accessed by their position or index&lt;/li&gt;
&lt;li&gt;First element is at index 0&lt;/li&gt;
&lt;li&gt;Last element is at index size-1&lt;/li&gt;
&lt;li&gt;[ ] - no checking to see if you are out of bounds&lt;/li&gt;
&lt;li&gt;Provides many useful function that do bounds check&lt;/li&gt;
&lt;li&gt;Elements initialized to zero&lt;/li&gt;
&lt;li&gt;Very efficient&lt;/li&gt;
&lt;li&gt;Iteration (looping) is often used to process&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>C++ Primitive Data Types</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Tue, 21 Nov 2023 18:56:31 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/c-primitive-data-types-1b5</link>
      <guid>https://forem.com/igorcastilhos/c-primitive-data-types-1b5</guid>
      <description>&lt;ul&gt;
&lt;li&gt;Fundamental data types implemented directly by the C++ language&lt;/li&gt;
&lt;li&gt;Character types&lt;/li&gt;
&lt;li&gt;Integer types

&lt;ul&gt;
&lt;li&gt;signed and unsigned&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Floating-point types&lt;/li&gt;

&lt;li&gt;Boolean type&lt;/li&gt;

&lt;li&gt;Size and precision is often compiler-dependent

&lt;ul&gt;
&lt;li&gt;#include &lt;code&gt;&amp;lt;climits&amp;gt;&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Type Sizes
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Expressed in bits&lt;/li&gt;
&lt;li&gt;The more bits the more values that can be represented&lt;/li&gt;
&lt;li&gt;The more bits the more storage required&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Size (in bits)&lt;/th&gt;
&lt;th&gt;Representable Values&lt;/th&gt;
&lt;th&gt;&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;8&lt;/td&gt;
&lt;td&gt;256&lt;/td&gt;
&lt;td&gt;2^8&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;16&lt;/td&gt;
&lt;td&gt;65,536&lt;/td&gt;
&lt;td&gt;2^16&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;32&lt;/td&gt;
&lt;td&gt;4,294,967,296&lt;/td&gt;
&lt;td&gt;2^32&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;64&lt;/td&gt;
&lt;td&gt;18,446,744,073,709,551,615&lt;/td&gt;
&lt;td&gt;2^64&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Character Types
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Used to represent single characters, ‘A’, ‘X’, ‘@’&lt;/li&gt;
&lt;li&gt;Wider types are used to represent wide character sets&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type Name&lt;/th&gt;
&lt;th&gt;Size / Precision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;char&lt;/td&gt;
&lt;td&gt;Exactly one byte. At least 8 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;char16_t&lt;/td&gt;
&lt;td&gt;At least 16 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;char32_t&lt;/td&gt;
&lt;td&gt;At least 32 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;wchar_t&lt;/td&gt;
&lt;td&gt;Can represent the largest available character set&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Integer Types
&lt;/h3&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type Name&lt;/th&gt;
&lt;th&gt;Size / Precision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;signed short int&lt;/td&gt;
&lt;td&gt;At least 16 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;signed int&lt;/td&gt;
&lt;td&gt;At least 16 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;signed long int&lt;/td&gt;
&lt;td&gt;At least 32 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;signed long long int&lt;/td&gt;
&lt;td&gt;At least 64 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type Name&lt;/th&gt;
&lt;th&gt;Size / Precision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;unsigned short int&lt;/td&gt;
&lt;td&gt;At least 16 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unsigned int&lt;/td&gt;
&lt;td&gt;At least 16 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unsigned long int&lt;/td&gt;
&lt;td&gt;At least 32 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;unsigned long long int&lt;/td&gt;
&lt;td&gt;At least 64 bits.&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;ul&gt;
&lt;li&gt;In addition to those shown in the table, it’s possible to store both &lt;strong&gt;signed&lt;/strong&gt; and &lt;strong&gt;unsigned&lt;/strong&gt; integers in the character data type&lt;/li&gt;
&lt;li&gt;If you want to declare a signed integer you don’t need to use the &lt;strong&gt;signed&lt;/strong&gt; keyword, since by default integers are signed.&lt;/li&gt;
&lt;li&gt;Similarly, if you want an integer type that stores very large numbers, you can simply declare the type as &lt;strong&gt;long long.&lt;/strong&gt; And you’ll get a signed long long integer.&lt;/li&gt;
&lt;li&gt;If you want &lt;strong&gt;&lt;em&gt;unsigned&lt;/em&gt;&lt;/strong&gt; integers, that is integers that are 0 or positive values only, then you’re required to use the &lt;strong&gt;&lt;em&gt;unsigned&lt;/em&gt;&lt;/strong&gt; keyword.&lt;/li&gt;
&lt;li&gt;By default, all integers are &lt;strong&gt;signed&lt;/strong&gt;.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Floating-point Type
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Used to represent non-integer numbers&lt;/li&gt;
&lt;li&gt;Represented by mantissa and exponent (scientific notation)&lt;/li&gt;
&lt;li&gt;Precision is the number of digits in the mantissa&lt;/li&gt;
&lt;li&gt;Precision and size are compiler dependent&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type Name&lt;/th&gt;
&lt;th&gt;Size / Typical Precision&lt;/th&gt;
&lt;th&gt;Typical Range&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;float&lt;/td&gt;
&lt;td&gt;/ 7 decimal digits&lt;/td&gt;
&lt;td&gt;1.2 x 10^-38 to 3.4 x 10^38&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;double&lt;/td&gt;
&lt;td&gt;No less than float / 15 decimal digits&lt;/td&gt;
&lt;td&gt;2.2 x 10^-308 to 1.8 x 10^308&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;long double&lt;/td&gt;
&lt;td&gt;No less than double/ 19 decimal digits&lt;/td&gt;
&lt;td&gt;3.3 x 10^-4932 to 1.2 x 10^4932&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Boolean Type
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Used to represent true and false&lt;/li&gt;
&lt;li&gt;Zero is false&lt;/li&gt;
&lt;li&gt;Non-zero is true&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Type Name&lt;/th&gt;
&lt;th&gt;Size / Precision&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;bool&lt;/td&gt;
&lt;td&gt;Usually 8 bits true or false&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

</description>
    </item>
    <item>
      <title>Variables in C++</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Tue, 21 Nov 2023 16:56:02 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/variables-in-c-3dlj</link>
      <guid>https://forem.com/igorcastilhos/variables-in-c-3dlj</guid>
      <description>&lt;ul&gt;
&lt;li&gt;If we had to program using the specific memory locations of a Random Memory Access (RAM), it wouldn’t be a lot of fun, and we’d likely have a lot of programmer errors.&lt;/li&gt;
&lt;li&gt;A variable is an abstraction for a memory location&lt;/li&gt;
&lt;li&gt;Allow programmers to use meaningful names and not memory addresses&lt;/li&gt;
&lt;li&gt;Variables have

&lt;ul&gt;
&lt;li&gt;Type - their category (integer, real number, string, Person, Account…)&lt;/li&gt;
&lt;li&gt;Value - the contents (10, 3.14, “Tim”…)&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Variables &lt;strong&gt;must&lt;/strong&gt; be declared before they are used&lt;/li&gt;

&lt;li&gt;A variables value may change
&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;        &lt;span class="c1"&gt;// Compiler error&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;





&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Declaring Variables
&lt;/h3&gt;

&lt;p&gt;💡 VariableType VariableName&lt;/p&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="n"&gt;rate&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;string&lt;/span&gt; &lt;span class="n"&gt;name&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="n"&gt;Account&lt;/span&gt; &lt;span class="n"&gt;franks_account&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="n"&gt;Person&lt;/span&gt; &lt;span class="n"&gt;james&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Naming Variables
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Can contain letters, numbers, and underscores&lt;/li&gt;
&lt;li&gt;Must begin with a letter or underscore (_)

&lt;ul&gt;
&lt;li&gt;cannot begin with a number&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Cannot use C++ reserved keywords&lt;/li&gt;

&lt;li&gt;Cannot redeclare a name in the same scope

&lt;ul&gt;
&lt;li&gt;C++ is case sensitive&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;/ul&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;Legal&lt;/th&gt;
&lt;th&gt;Illegal&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Age&lt;/td&gt;
&lt;td&gt;int&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;age&lt;/td&gt;
&lt;td&gt;$age&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;_age&lt;/td&gt;
&lt;td&gt;2022_age&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;My_age&lt;/td&gt;
&lt;td&gt;My age&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;your_age_in_2000&lt;/td&gt;
&lt;td&gt;Age+1&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;




&lt;h3&gt;
  
  
  Style and Best Practices
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Be consistent with your naming conventions

&lt;ul&gt;
&lt;li&gt;myVariableName vs my_variable_name&lt;/li&gt;
&lt;li&gt;avoid beginning names with underscores&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Use meaningful names

&lt;ul&gt;
&lt;li&gt;not too long and not too short&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;

&lt;li&gt;Never use variables before initializing them&lt;/li&gt;

&lt;li&gt;Declare variables close to when you need them in your code&lt;/li&gt;

&lt;/ul&gt;




&lt;h3&gt;
  
  
  Initializing Variables
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight cpp"&gt;&lt;code&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;      &lt;span class="c1"&gt;// uninitialized&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="c1"&gt;// C-like initialization&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="nf"&gt;age&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// Constructor initialization&lt;/span&gt;

&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;age&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="mi"&gt;22&lt;/span&gt;&lt;span class="p"&gt;};&lt;/span&gt; &lt;span class="c1"&gt;// C++11 list initialization syntax&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Global Variables
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Variables declared within the curly braces of the main function are called Local Variables because their scope or visibility is limited to the statements in the main function that follow the declaration of the variable. This is generally what you want to do.&lt;/li&gt;
&lt;li&gt;However, sometimes you see variables declared outside of a function, these variables are called Global Variables and they can be accessed from any part of your program.&lt;/li&gt;
&lt;li&gt;Unlike local variables, global variables are automatically initialized to zero.&lt;/li&gt;
&lt;li&gt;Since global variables can be accessed by any part of the program, this means they can potentially be changed from any part of the program. This can make finding errors and debugging more difficult.&lt;/li&gt;
&lt;/ul&gt;

</description>
    </item>
    <item>
      <title>Programming Paradigms</title>
      <dc:creator>Igor</dc:creator>
      <pubDate>Mon, 20 Nov 2023 19:12:08 +0000</pubDate>
      <link>https://forem.com/igorcastilhos/programming-paradigms-4ocd</link>
      <guid>https://forem.com/igorcastilhos/programming-paradigms-4ocd</guid>
      <description>&lt;p&gt;&lt;strong&gt;A programming paradigm is a fundamental style or approach to solving problems&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Different programming paradigms provide different ways of organizing and structuring code, and have different strengths and weaknesses. Some of the most common programming paradigms include:&lt;/p&gt;

&lt;h3&gt;
  
  
  Imperative
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Is one of the oldest programming paradigm. It is based on Von Neumann architecture. The programmer instructs the machine how to change its state. They state the order in which operations occur, with constructs that explicitly control that order, and &lt;strong&gt;&lt;em&gt;they allow side effects&lt;/em&gt;&lt;/strong&gt;, in which state can be modified at one point in time, within one unit of code, and then later read at a different point in time, inside a different unit of code. The communication between the units of code is &lt;strong&gt;not explicit&lt;/strong&gt;.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;    Examples of Imperative programming paradigm:

    C : developed by Dennis Ritchie and Ken Thompson
    Fortran : developed by John Backus for IBM
    Basic : developed by John G Kemeny and Thomas E Kurtz

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h3&gt;
  
  
  Procedural [Imperative]
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Focus in dividing a problem in a sequence of steps or procedures well defined. It emphasizes the reutilization through functions or sub routines and is characterized by it’s linear flux of execution.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Examples of Procedural paradigms:

C
C++
Java
ColdFusion
Pascal
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Object-Oriented Programming [Imperative]
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;OOP organizes the code in classes and objects, allowing encapsulation, inheritance and polymorphism. It promotes the modeling of real-world objects and their interactions, improving code modularity and maintainability.
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Examples of Object Oriented paradigm:

Simula
Java
C++
Objective C
VB .NET
Python
Ruby
Smalltalk
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Concurrent / Parallel
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Involves dealing with multiple tasks or processes that can be executed simultaneously. Parallel programming goes further, taking advantage of multiple processors or cores to execute tasks concurrently, thus improving performance.&lt;/li&gt;
&lt;/ul&gt;




&lt;h3&gt;
  
  
  Declarative
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It focuses on describing what needs to be done rather than how to do it. It abstracts low-level implementation details, resulting in more readable and expressive code. SQL is a typical example.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Functional [Declarative]
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Functional programming treats computation as the evaluation of mathematical functions. It emphasizes immutability, first-class functions, and higher-order functions, resulting in more concise, predictable, and parallelizable code.&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  Logic [Declarative]
&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;It involves specifying a set of logical rules and facts to derive conclusions or solve problems. It is especially suitable for tasks such as knowledge representation, artificial intelligence, and theorem proving.&lt;/li&gt;
&lt;/ul&gt;




&lt;p&gt;Sub Routine = Block of code that runs a specific task and can be called from different parts of the program. It’s a form of structuring and organizing the code, making it modular and reusable.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
