{"id":6202,"date":"2025-05-30T15:15:03","date_gmt":"2025-05-30T09:45:03","guid":{"rendered":"https:\/\/namastedev.com\/blog\/?p=6202"},"modified":"2025-05-30T15:15:03","modified_gmt":"2025-05-30T09:45:03","slug":"sum-of-two-integers-function-in-javascript-c-c-java-and-python","status":"publish","type":"post","link":"https:\/\/namastedev.com\/blog\/sum-of-two-integers-function-in-javascript-c-c-java-and-python\/","title":{"rendered":"Sum of Two Integers Function in JavaScript, C, C++, Java, and Python"},"content":{"rendered":"\n\n<!-- Prism.js CSS and JS -->\n<link href=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/themes\/prism-tomorrow.css\" rel=\"stylesheet\" \/>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/prism.js\"><\/script>\n<script src=\"https:\/\/cdn.jsdelivr.net\/npm\/prismjs@1.29.0\/plugins\/autoloader\/prism-autoloader.min.js\"><\/script>\n\n\n<style>\n .wp_blog_code-tabs-container {\n   font-family: \"Segoe UI\", sans-serif !important;\n   max-width: 900px !important;\n   margin: 2rem auto !important;\n   border: 1px solid #ddd !important;\n   border-radius: 8px !important;\n   overflow: hidden !important;\n   background-color: white !important;\n }\n\n\n .wp_blog_code-tabs-header {\n   background: #f7f7f7 !important;\n   display: flex !important;\n   border-bottom: 1px solid #ddd !important;\n }\n\n\n .wp_blog_code-tab-button {\n   flex: 1 !important;\n   padding: 10px 15px !important;\n   border: none !important;\n   background: transparent !important;\n   cursor: pointer !important;\n   font-weight: bold !important;\n   transition: background 0.2s !important;\n   color: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-button.active {\n   background: white !important;\n   border-bottom: 3px solid #0073aa !important;\n }\n\n\n .wp_blog_code-tab-content {\n   display: none !important;\n   padding: 20px !important;\n   background: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-content > pre{\n   background: #242B33 !important;\n }\n\n\n .wp_blog_code-tab-content.active {\n   display: block !important;\n }\n\n\n .wp_blog_code-tab-content pre {\n   margin: 0 !important;\n   overflow-x: auto !important;\n }\n\n\n .wp_blog_explanation {\n   max-width: 900px !important;\n   margin: 2rem auto !important;\n   font-family: \"Segoe UI\", sans-serif !important;\n   line-height: 1.6 !important;\n   background: white !important;\ncolor : black !important;\n   padding: 1rem !important;\n   border-radius: 8px !important;\n }\n\n\n .wp_blog_explanation h2 {\n   color: #0073aa !important;\n   font-size: 1.5rem !important;\n   margin-bottom: 0.5rem !important;\n }\n\n\n .wp_blog_explanation code {\n   background: #f1f1f1 !important;\n   padding: 2px 6px !important;\n   border-radius: 4px !important;\n   font-family: monospace !important;\n }\n\n\n.wp_blog_explanation h1,\n.wp_blog_explanation h2,\n.wp_blog_explanation h3,\n.wp_blog_explanation h4,\n.wp_blog_explanation h5,\n.wp_blog_explanation h6,\n.wp_blog_explanation p {\n margin-top: 10px !important;\n margin-bottom: 10px !important;\n}\n<\/style>\n\n\n<div class=\"wp_blog_explanation\">\n  <p>\n    Write a program that defines a function to calculate the sum of two integers and prints the result.\n    Call this function by passing two integer values.\n  <\/p>\n\n  <h2>Explanation<\/h2>\n  <ul>\n    <li><code>sum(a, b)<\/code> is a function that takes two arguments.<\/li>\n    <li>It adds them and stores the result in a variable named <code>add<\/code>.<\/li>\n    <li>It then prints the result.<\/li>\n    <li><code>sum(x, y)<\/code> calls the function with x = 10 and y = 20, so it prints 30.<\/li>\n  <\/ul>\n\n  <h2>Dry Run<\/h2>\n  <p><strong>Input:<\/strong> a = 10, b = 20<\/p>\n  <p><strong>Step 1:<\/strong> add = 10 + 20 = 30<\/p>\n  <p><strong>Step 2:<\/strong> Print 30<\/p>\n<\/div>\n\n\n\n<div class=\"wp_blog_code-tabs-container\">\n  <div class=\"wp_blog_code-tabs-header\">\n    <button class=\"wp_blog_code-tab-button active\" data-lang=\"js\">JavaScript<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"cpp\">C++<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"c\">C<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"java\">Java<\/button>\n    <button class=\"wp_blog_code-tab-button\" data-lang=\"py\">Python<\/button>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content active\" data-lang=\"js\">\n    <pre><code class=\"language-javascript\">\nfunction sum(a, b) {\n  let add = a + b;\n  console.log(add);\n}\n\nlet x = 10;\nlet y = 20;\nsum(x, y);\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"cpp\">\n    <pre><code class=\"language-cpp\">\n#include &lt;iostream&gt;\nusing namespace std;\n\nvoid sum(int a, int b) {\n  int add = a + b;\n  cout &lt;&lt; add &lt;&lt; endl;\n}\n\nint main() {\n  int x = 10, y = 20;\n  sum(x, y);\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"c\">\n    <pre><code class=\"language-c\">\n#include &lt;stdio.h&gt;\n\nvoid sum(int a, int b) {\n  int add = a + b;\n  printf(\"%d\\n\", add);\n}\n\nint main() {\n  int x = 10, y = 20;\n  sum(x, y);\n  return 0;\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"java\">\n    <pre><code class=\"language-java\">\npublic class Main {\n  public static void sum(int a, int b) {\n    int add = a + b;\n    System.out.println(add);\n  }\n\n  public static void main(String[] args) {\n    int x = 10, y = 20;\n    sum(x, y);\n  }\n}\n    <\/code><\/pre>\n  <\/div>\n\n  <div class=\"wp_blog_code-tab-content\" data-lang=\"py\">\n    <pre><code class=\"language-python\">\ndef sum(a, b):\n  add = a + b\n  print(add)\n\nx = 10\ny = 20\nsum(x, y)\n    <\/code><\/pre>\n  <\/div>\n<\/div>\n\n<script>\n document.addEventListener('DOMContentLoaded', function () {\n   const buttons = document.querySelectorAll('.wp_blog_code-tab-button');\n   const contents = document.querySelectorAll('.wp_blog_code-tab-content');\n\n\n   buttons.forEach(button => {\n     button.addEventListener('click', () => {\n       const lang = button.getAttribute('data-lang');\n\n\n       buttons.forEach(btn => btn.classList.remove('active'));\n       button.classList.add('active');\n\n\n       contents.forEach(content => {\n         content.classList.toggle('active', content.getAttribute('data-lang') === lang);\n       });\n     });\n   });\n });\n<\/script>\n\n\n\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Write a program that defines a function to calculate the sum of two integers and prints the result. Call this function by passing two integer values. Explanation sum(a, b) is a function that takes two arguments. It adds them and stores the result in a variable named add. It then prints the result. sum(x, y)<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[175,811,810,174,172,173],"tags":[],"class_list":{"0":"post-6202","1":"post","2":"type-post","3":"status-publish","4":"format-standard","6":"category-cplusplus","7":"category-data-structures-and-algorithms","8":"category-dsa","9":"category-java","10":"category-javascript","11":"category-python"},"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6202","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/comments?post=6202"}],"version-history":[{"count":1,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6202\/revisions"}],"predecessor-version":[{"id":6203,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/posts\/6202\/revisions\/6203"}],"wp:attachment":[{"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/media?parent=6202"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/categories?post=6202"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/namastedev.com\/blog\/wp-json\/wp\/v2\/tags?post=6202"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}