{"id":6180,"date":"2024-01-01T00:01:33","date_gmt":"2024-01-01T08:01:33","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=6180"},"modified":"2024-01-09T08:36:08","modified_gmt":"2024-01-09T16:36:08","slug":"the-swap-function","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=6180","title":{"rendered":"The <em>swap()<\/em> Function"},"content":{"rendered":"<h2>Difficulty: &#9733; &#9733; &#9733; &#9734;<\/h2>\n<p>Swapping values is common task in computer programming, often used when sorting. You need the value of variable <code>a<\/code> in variable <code>b<\/code> and vice-versa. A number of methods are available for swapping, most of which involve using a temporary variable to hold one of the values during the swap.<br \/>\n<!--more--><br \/>\nI can think of two instances where a temporary variable isn&#8217;t required.<\/p>\n<p>The first I <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=5898\">wrote about<\/a> a while back, where a third variable isn&#8217;t needed when swapping two integer values. Here&#8217;s the clever approach:<\/p>\n<p><code>b = b + a;<br \/>\na = b - a;<br \/>\nb = b - a;<\/code><\/p>\n<p>The second method I use often in Assembly language: Push two values on the stack and then pop them off in reverse order. For example:<\/p>\n<p><code>push ax<br \/>\npush bx<br \/>\npop ax<br \/>\npop bx<\/code><\/p>\n<p>A third variable isn&#8217;t required here. The stack is used as storage where values are pushed and then removed. Very nerdy.<\/p>\n<p>For this month&#8217;s Exercise, your task is to swap the values of two variables. Yes, you can use a &#8220;temp&#8221; variable or swap the two values any way you like. The restriction is that the swapping takes place in a function. This skeleton below may give you an idea of what I&#8217;m after.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2024_01_01-Lesson.c\" rel=\"noopener\" target=\"_blank\">2024_01_01-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main() {\r\n    int x = 5;\r\n    int y = 10;\r\n\r\n    printf(\"Before swap: x=%d, y=%d\\n\",x,y);\r\n    <span class=\"comments\">\/* swap function call here *\/<\/span>\r\n    printf(\"After swap: x=%d, y=%d\\n\",x,y);\r\n\r\n    return 0;\r\n}<\/pre>\n<p>Your task is to write the <em>swap()<\/em> function. It&#8217;s set in the code where the comment appears above. Yes, the limitation here is that functions in C return only a single value. Part of the challenge is how to work out this conundrum.<\/p>\n<p>Please give this Exercise a try before you view <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6195\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Difficulty: &#9733; &#9733; &#9733; &#9734; Swapping values is common task in computer programming, often used when sorting. You need the value of variable a in variable b and vice-versa. A number of methods are available for swapping, most of which &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=6180\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-6180","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6180","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6180"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6180\/revisions"}],"predecessor-version":[{"id":6212,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6180\/revisions\/6212"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6180"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6180"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6180"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}