{"id":1036,"date":"2010-08-09T09:16:16","date_gmt":"2010-08-09T08:16:16","guid":{"rendered":"https:\/\/blogs.mentor.com\/colinwalls\/?p=1036"},"modified":"2010-08-09T09:16:16","modified_gmt":"2010-08-09T08:16:16","slug":"function-parameters","status":"publish","type":"post","link":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/2010\/08\/09\/function-parameters\/","title":{"rendered":"Function parameters"},"content":{"rendered":"<p>When you use a function in C\/C++ [or most other programming languages] you are likely to pass it some parameters &#8211; data which may be processed by the function or give it information on what action is required. For the majority of programmers, outside of the world of embedded software, parameters are just a fact &#8211; no real issues. They are likely to be quite uninterested in how parameters are passed and how efficient and secure that process that might be. Embedded developers tend to want details. Efficient code is essential and avoiding possible bugs is always desirable. So, maybe a look at how parameter passing actually works might be useful &#8230;<!--more--><\/p>\n<p>When embedded systems were all programmed in assembly language, the programmer had complete freedom with parameter passing and a number of options were available:<\/p>\n<ul>\n<li>Use global variables &#8211; not generally recommended, but might be most efficient in some circumstances.<\/li>\n<li>Use registers &#8211; this can be fast and efficient; the required data might already be in a register [which is almost the same as using a global variable, so it needs care].<\/li>\n<li>Use the stack &#8211; SP-relative addressing may be efficient in many processors; this approach leads to reentrant code.<\/li>\n<li>Use a dedicated memory block &#8211; this approach may make sense, if the addressing modes of the processor are limited, but probably results in non-reentrant code.<\/li>\n<\/ul>\n<p>In C, some aspects of parameter passing are specified by the language definition; others are implementation dependent and may vary from one compiler to another. There are no specific constraints on the parameter passing mechanism. It is left to the compiler writer to choose between stack [which is most common], registers or something else. It is bad programming practice to make assumptions about how parameters are passed. You could write code like this:<\/p>\n<pre>void fun(int n)<\/pre>\n<pre>{<\/pre>\n<pre style=\"padding-left: 30px\">int *p, x;<\/pre>\n<pre style=\"padding-left: 30px\">p = &amp;n;<\/pre>\n<pre style=\"padding-left: 30px\">x = *++p;<\/pre>\n<p>This function simply takes the address of the first parameter, assumes that it is on the stack and indexes off of that address to find further parameters. Please do not do this.<\/p>\n<p>In C\/C++ parameters are normally passed by value &#8211; the value passed as a parameter is copied into the formal parameter of the function. C++ gives the option of passing by reference, which means that a called function can access variables which are passed as parameters by the calling function. To achieve this in C, you need to use pointers [which is, of course, what C++ is doing behind the scenes].<\/p>\n<p>A small quiz to test your knowledge of C parameter passing: when, in C, is parameter passing by reference the default? Please comment or email. Sorry no prizes, just fame and glory.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>When you use a function in C\/C++ [or most other programming languages] you are likely to pass it some parameters&#8230;<\/p>\n","protected":false},"author":71677,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spanish_translation":"","french_translation":"","german_translation":"","italian_translation":"","polish_translation":"","japanese_translation":"","chinese_translation":"","footnotes":""},"categories":[1],"tags":[313,383,300,340],"industry":[],"product":[],"coauthors":[],"class_list":["post-1036","post","type-post","status-publish","format-standard","hentry","category-news","tag-c","tag-edge-compiler","tag-embedded-software","tag-programming-languages"],"_links":{"self":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1036","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/users\/71677"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/comments?post=1036"}],"version-history":[{"count":0,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/posts\/1036\/revisions"}],"wp:attachment":[{"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/media?parent=1036"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/categories?post=1036"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/tags?post=1036"},{"taxonomy":"industry","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/industry?post=1036"},{"taxonomy":"product","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/product?post=1036"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/blogs.stage.sw.siemens.com\/embedded-software\/wp-json\/wp\/v2\/coauthors?post=1036"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}