{"id":2904,"date":"2020-04-26T07:48:14","date_gmt":"2020-04-26T07:48:14","guid":{"rendered":"https:\/\/www.staging6.machinelearningplus.com\/?p=2904"},"modified":"2022-03-08T16:44:54","modified_gmt":"2022-03-08T16:44:54","slug":"function-in-julia","status":"publish","type":"post","link":"https:\/\/machinelearningplus.com\/julia\/function-in-julia\/","title":{"rendered":"Function in Julia"},"content":{"rendered":"<em>Function is a block of organized, reusable code that accepts input and returns output.<\/em>\n\n<em>All the computations you wish to do needs to be declared inside the body of the function. In order to call a function, you need to add <code>( )<\/code> along with any parameters inside it.<\/em>\n\n<h2 id=\"content\">Content<\/h2>\n\n<ol>\n    <li>Functions in Julia<\/li>\n    <li>Return keyword<\/li>\n    <li>Multi-return Functions<\/li>\n    <li>Operators as Functions<\/li>\n    <li>Practice Exercise<\/li>\n<\/ol>\n\n\n\n<h2 id=\"1introductiontofunctions\">1. Introduction to Functions<\/h2>\n\nFunction is a block of organized, reusable code that accepts input and returns output.\n\nAll the computations you wish to do needs to be declared inside the function i.e in the body of the function. It gives output only when it is called.\n\nYou must be thinking why should I write a function rather I will just write a block of code and run it.\n\nThe idea of building a function is to put all the repeated tasks together in the body of a function. Later on, call the function in just one line, rather than writing the same code again and again.\n\nLet&#8217;s see how to build a function.\n\n<h3 id=\"syntaxoffunction\">Syntax of function<\/h3>\n\n<pre><code class=\"Julia language-Julia\">function function_name(input)\n    # computations\n    return ouptut\nend\n<\/code><\/pre>\n\nLet&#8217;s see the implementation. Say you have a mathematical equation, which is going to be used multiple times in your project.\n\n<pre><code class=\" language- \">f(x,y,z) = (x*y)^z\n<\/code><\/pre>\n\nNow you wish to create a function in Julia so that you won&#8217;t have to write the code again and again.\n\n<pre><code class=\"julia language-julia\"># Create a function which serves the purpose of the above stated mathematical function\nfunction math_formula(x,y,z)\n    return (x*y)^z\nend\n\n# Call the function \nprintln(math_formula(3,4,2))\n<\/code><\/pre>\n\n<pre><code># Output : \n144\n<\/code><\/pre>\n\nAlternatively, you can define the same function in more compact form, called \u201cassignment form\u201d.\n\n<pre><code class=\"julia language-julia\"># Create the same function in assignment form\nmath_formula(x,y,z) = (x*y)^z\n\n# Call the function \nprintln(math_formula(3,4,2))\n<\/code><\/pre>\n\n<pre><code># Output : \n144\n<\/code><\/pre>\n\n<h2 id=\"2returnkeyword\">2. Return keyword<\/h2>\n\nReturn keyword is generally used at end of the function to specify the output of the function. If the return statement is not defined in Julia, the function&#8217;s output is the last variable\/computation by default.\n\nR and Python doesn&#8217;t work like this. Bit confused right? Let&#8217;s see with examples\n\n<pre><code class=\"julia language-julia\"># Create a function with return statement missing\nfunction math_formula(x,y,z)\n    output1 = (x*y)^z\n    output2 = (x+y)^z\nend\n\nprintln(math_formula(3,4,2))\n<\/code><\/pre>\n\n<pre><code># Output :\n49\n<\/code><\/pre>\n\nBy default the function has returned the last variable in body of function i.e output2 . Let&#8217;s see how to return output1\n\n<pre><code class=\"julia language-julia\"># Create a function which return statement\nfunction math_formula(x,y,z)\n    output1 = (x*y)^z\n    output2 = (x+y)^z\n    return output1\nend\n\nprintln(math_formula(3,4,2))\n<\/code><\/pre>\n\n<pre><code># Output :\n144\n<\/code><\/pre>\n\nBoth the functions are defined same except the return statement. The first one returns the last variable by default but the second one returns the variable with its return keyword.\n\nNote : Defining the return statement is a good practice in programming.\n\nReturn keyword plays a vital role when you want to define some conditional function. Let&#8217;s see it with help on an example\n\n<pre><code class=\"julia language-julia\"># Create a function to return the sign of a number wheter it's positive , negative or zero\nfunction sign(x)\n    if x &gt; 0\n        return \"positive\"\n    elseif x == 0\n        return \"zero\"\n    else\n        return \"negative\"\n    end       \nend\n\nsign(-5)\n<\/code><\/pre>\n\n<pre><code># Output :\n\"negative\"\n<\/code><\/pre>\n\n<h2 id=\"3muliplereturnfunctions\">3. Muliple return functions<\/h2>\n\nFunctions can return multiple outputs as well.\n\n<pre><code class=\"julia language-julia\"># Create a function which gives two outputs\nfunction math_formula(x,y,z)\n    output1 = (x*y)^z\n    output2 = (x+y)^z\n    return output1,output2\nend\n\nprintln(math_formula(3,4,2))\n<\/code><\/pre>\n\n<pre><code># Output :\n(144, 49)\n<\/code><\/pre>\n\n<h2 id=\"4operatorsasfunctions\">4. Operators as functions<\/h2>\n\nThe operators in Julia are also functions. Operators can also be used as a function. Let&#8217;s see it with an example\n\n<pre><code class=\"julia language-julia\"># Add 3 numbers with \"+\" operator\nx = 1 + 2 + 3\n\n# Add 3 numbers with \"+\" operator like a function\n+(1,2,3)\n<\/code><\/pre>\n\n<pre><code># Output :\n6\n<\/code><\/pre>\n\n<h2 id=\"5practiceexercises\">5. Practice Exercises<\/h2>\n\nQ1. Create a function which returns nothing\n\n<pre><code class=\"julia language-julia\">function nothing_formula()\n    return nothing\nend\n\nprintln(nothing_formula())\n<\/code><\/pre>\n\n<pre><code># Output :\nnothing\n<\/code><\/pre>\n\nQ2. Create a function which accepts the employee name, and his\/her appraisal rating. Return both the name and appraisal rating. If the rating is missing in the function call, it should automatically return the rating as &#8220;Above Expectations&#8221;\n\n<pre><code class=\"julia language-julia\">function employee_evaluation(name, rating = \"Above Expectations\")\n    output = \"Appraisal Rating of \"*name*\" is: \"*rating\nend\n\nprintln(employee_evaluation(\"Kabir\"))\n<\/code><\/pre>\n\n<pre><code># Output :\nAppraisal Rating of Kabir is: Above Expectations\n<\/code><\/pre>\n\n<h2 id=\"conclusion\">Conclusion<\/h2>\n\nSo, now you should have a fair idea of how to use functions in Julia. Next, I will see you with more Data Science oriented topics in Julia.\n\nRead more about Julia <a href=\"https:\/\/machinelearningplus.com\/julia\/\">here<\/a><!-- \/wp:post-content -->","protected":false},"excerpt":{"rendered":"<p>Function is a block of organized, reusable code that accepts input and returns output. All the computations you wish to do needs to be declared inside the body of the function. In order to call a function, you need to add ( ) along with any parameters inside it. Content Functions in Julia Return keyword [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"site-sidebar-layout":"default","site-content-layout":"default","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[101],"tags":[112,102,113],"class_list":["post-2904","post","type-post","status-publish","format-standard","hentry","category-julia","tag-function","tag-julia","tag-programming"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v28.0 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Function in Julia - machinelearningplus<\/title>\n<meta name=\"description\" content=\"Function in Julia is a block of organized, reusable code that accepts input and returns output. Output is returned only when the function is called.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/localhost:8080\/julia\/function-in-julia\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Function in Julia - machinelearningplus\" \/>\n<meta property=\"og:description\" content=\"Function in Julia is a block of organized, reusable code that accepts input and returns output. Output is returned only when the function is called.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/localhost:8080\/julia\/function-in-julia\/\" \/>\n<meta property=\"og:site_name\" content=\"machinelearningplus\" \/>\n<meta property=\"article:published_time\" content=\"2020-04-26T07:48:14+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-03-08T16:44:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2026\/03\/og-image-screenshot.png\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"630\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"Kabir\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kabir\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/\"},\"author\":{\"name\":\"Kabir\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/01b9d7b59990cde0e05068914456b5ad\"},\"headline\":\"Function in Julia\",\"datePublished\":\"2020-04-26T07:48:14+00:00\",\"dateModified\":\"2022-03-08T16:44:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/\"},\"wordCount\":498,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"keywords\":[\"Function\",\"Julia\",\"Programming\"],\"articleSection\":[\"Julia\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/\",\"url\":\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/\",\"name\":\"Function in Julia - machinelearningplus\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\"},\"datePublished\":\"2020-04-26T07:48:14+00:00\",\"dateModified\":\"2022-03-08T16:44:54+00:00\",\"description\":\"Function in Julia is a block of organized, reusable code that accepts input and returns output. Output is returned only when the function is called.\",\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/localhost:8080\\\/julia\\\/function-in-julia\\\/\"]}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#website\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"name\":\"machinelearningplus\",\"description\":\"Learn Data Science (AI \\\/ ML) Online\",\"publisher\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/machinelearningplus.com\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#organization\",\"name\":\"machinelearningplus\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/uploads\\\/2022\\\/05\\\/MachineLearningplus-logo.svg\",\"width\":348,\"height\":36,\"caption\":\"machinelearningplus\"},\"image\":{\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/#\\\/schema\\\/person\\\/01b9d7b59990cde0e05068914456b5ad\",\"name\":\"Kabir\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/5146322f528131e2446a7a18ab6020aa.jpg?ver=1783629629\",\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/5146322f528131e2446a7a18ab6020aa.jpg?ver=1783629629\",\"contentUrl\":\"https:\\\/\\\/machinelearningplus.com\\\/wp-content\\\/litespeed\\\/avatar\\\/5146322f528131e2446a7a18ab6020aa.jpg?ver=1783629629\",\"caption\":\"Kabir\"},\"url\":\"https:\\\/\\\/machinelearningplus.com\\\/author\\\/ajay\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Function in Julia - machinelearningplus","description":"Function in Julia is a block of organized, reusable code that accepts input and returns output. Output is returned only when the function is called.","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/localhost:8080\/julia\/function-in-julia\/","og_locale":"en_US","og_type":"article","og_title":"Function in Julia - machinelearningplus","og_description":"Function in Julia is a block of organized, reusable code that accepts input and returns output. Output is returned only when the function is called.","og_url":"https:\/\/localhost:8080\/julia\/function-in-julia\/","og_site_name":"machinelearningplus","article_published_time":"2020-04-26T07:48:14+00:00","article_modified_time":"2022-03-08T16:44:54+00:00","og_image":[{"width":1200,"height":630,"url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2026\/03\/og-image-screenshot.png","type":"image\/png"}],"author":"Kabir","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kabir","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/localhost:8080\/julia\/function-in-julia\/#article","isPartOf":{"@id":"https:\/\/localhost:8080\/julia\/function-in-julia\/"},"author":{"name":"Kabir","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/01b9d7b59990cde0e05068914456b5ad"},"headline":"Function in Julia","datePublished":"2020-04-26T07:48:14+00:00","dateModified":"2022-03-08T16:44:54+00:00","mainEntityOfPage":{"@id":"https:\/\/localhost:8080\/julia\/function-in-julia\/"},"wordCount":498,"commentCount":0,"publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"keywords":["Function","Julia","Programming"],"articleSection":["Julia"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/localhost:8080\/julia\/function-in-julia\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/localhost:8080\/julia\/function-in-julia\/","url":"https:\/\/localhost:8080\/julia\/function-in-julia\/","name":"Function in Julia - machinelearningplus","isPartOf":{"@id":"https:\/\/machinelearningplus.com\/#website"},"datePublished":"2020-04-26T07:48:14+00:00","dateModified":"2022-03-08T16:44:54+00:00","description":"Function in Julia is a block of organized, reusable code that accepts input and returns output. Output is returned only when the function is called.","inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/localhost:8080\/julia\/function-in-julia\/"]}]},{"@type":"WebSite","@id":"https:\/\/machinelearningplus.com\/#website","url":"https:\/\/machinelearningplus.com\/","name":"machinelearningplus","description":"Learn Data Science (AI \/ ML) Online","publisher":{"@id":"https:\/\/machinelearningplus.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/machinelearningplus.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/machinelearningplus.com\/#organization","name":"machinelearningplus","url":"https:\/\/machinelearningplus.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/","url":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/uploads\/2022\/05\/MachineLearningplus-logo.svg","width":348,"height":36,"caption":"machinelearningplus"},"image":{"@id":"https:\/\/machinelearningplus.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/machinelearningplus.com\/#\/schema\/person\/01b9d7b59990cde0e05068914456b5ad","name":"Kabir","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/5146322f528131e2446a7a18ab6020aa.jpg?ver=1783629629","url":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/5146322f528131e2446a7a18ab6020aa.jpg?ver=1783629629","contentUrl":"https:\/\/machinelearningplus.com\/wp-content\/litespeed\/avatar\/5146322f528131e2446a7a18ab6020aa.jpg?ver=1783629629","caption":"Kabir"},"url":"https:\/\/machinelearningplus.com\/author\/ajay\/"}]}},"_links":{"self":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/2904","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/comments?post=2904"}],"version-history":[{"count":0,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/posts\/2904\/revisions"}],"wp:attachment":[{"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/media?parent=2904"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/categories?post=2904"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/machinelearningplus.com\/wp-json\/wp\/v2\/tags?post=2904"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}