<?xml version="1.0" encoding="UTF-8"?><rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/">
  <channel>
    <title>functions &amp;mdash; Paul Sutton</title>
    <link>https://paper.wf/paulsutton/tag:functions</link>
    <description>Paul Sutton - personal blog </description>
    <pubDate>Thu, 07 May 2026 01:24:20 +0000</pubDate>
    <item>
      <title>Code Club - Python functions 2</title>
      <link>https://paper.wf/paulsutton/code-club-python-functions-2</link>
      <description>&lt;![CDATA[Code Club - Python functions 2&#xA;&#xA;So to follow the previous post,  we are going to look at passing 2 arguments to a function, adding them together, then displaying the result. &#xA;&#xA;Firstly open a new Python Trinket&#xA;&#xA;Firstly we are going to tell the interpreter to use python3. &#xA;#!/usr/bin/env python3 #use python 3&#xA;Now we create a function to take two arguments and display the sub of both&#xA;def addnumbers(x,z):&#xA;  #print(x)&#xA;  #print(z)&#xA;  add = (int(x) + int(z))&#xA;  print (&#39;Total = &#39;)&#xA;  print(add)&#xA;Get user input  &#xA;x = input(&#34;First Number&#34;) # ask for first number&#xA;z = input(&#34;Second Number&#34;) # ask for second number&#xA;Convert to integers&#xA;int(x)&#xA;int(z)&#xA;Call function and pass values to function&#xA;addnumbers(x,z)&#xA;&#xA;Tags&#xA;&#xA;#CodeClub,#Python.#Programming,#Functions,#Arguments]]&gt;</description>
      <content:encoded><![CDATA[<p>Code Club – Python functions 2</p>

<p>So to follow the previous post,  we are going to look at passing 2 arguments to a function, adding them together, then displaying the result.</p>

<p>Firstly open a <a href="https://trinket.io/library/trinkets/create?lang=python" rel="nofollow">new Python Trinket</a></p>

<p>Firstly we are going to tell the interpreter to use python3.</p>

<pre><code>#!/usr/bin/env python3 #use python 3
</code></pre>

<p>Now we create a function to take two arguments and display the sub of both</p>

<pre><code>def add_numbers(x,z):
  #print(x)
  #print(z)
  add = (int(x) + int(z))
  print (&#39;Total = &#39;)
  print(add)
</code></pre>

<p>Get user input</p>

<pre><code>x = input(&#34;First Number&#34;) # ask for first number
z = input(&#34;Second Number&#34;) # ask for second number
</code></pre>

<p>Convert to integers</p>

<pre><code>int(x)
int(z)
</code></pre>

<p>Call function and pass values to function</p>

<pre><code>add_numbers(x,z)
</code></pre>

<p><strong>Tags</strong></p>

<p><a href="/paulsutton/tag:CodeClub" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">CodeClub</span></a>,<a href="/paulsutton/tag:Python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Python</span></a>.<a href="/paulsutton/tag:Programming" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Programming</span></a>,<a href="/paulsutton/tag:Functions" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Functions</span></a>,<a href="/paulsutton/tag:Arguments" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Arguments</span></a></p>
]]></content:encoded>
      <guid>https://paper.wf/paulsutton/code-club-python-functions-2</guid>
      <pubDate>Mon, 29 May 2023 06:00:00 +0000</pubDate>
    </item>
    <item>
      <title>Code Club - Simple Python 3 Functions</title>
      <link>https://paper.wf/paulsutton/code-club-simple-python-3-functions</link>
      <description>&lt;![CDATA[Code Club - Simple Python 3 Functions&#xA;&#xA;As we are learning Python at Code Club,  I am going to make some related posts looking at the very basics of this.   We are using Trinket.io for this&#xA;&#xA;Firstly open a new Python Trinket&#xA;&#xA;Firstly we are going to tell the interpreter to use python3. &#xA;#!/usr/bin/env python3 #use python 3&#xA;&#xA;We are going to create a function called printname() and then call it.  The function will just print hello to the standard output.&#xA;&#xA;def printname():&#xA;  print (&#34;hello&#34;)&#xA;  &#xA;printname()  &#xA;&#xA;So this in does the same as what &#xA;&#xA;print (&#34;hello&#34;)&#xA;&#xA;To the next step is to run the function several times.  &#xA;&#xA;We could run&#xA;printname()  &#xA;printname() &#xA;printname() &#xA;However it is probably better to put the printname() in a loop, so it calls the function a specified number of times (in this case 4)&#xA;for x in range(0,4):&#xA;  printname()&#xA;So this will now call the printname() four times (or however many times is specified.&#xA;&#xA;Assuming this works,  we can now add some user interactivity&#xA;&#xA;y = input(&#34;iterations&#34;)&#xA;As we are dealing with numbers (integers) than y will be a string we fix this in the line for x in range(0,int(y)):  int(y) will convert string y in to integer y&#xA;&#xA;Final program looks like this:-&#xA;&#xA;def printname(y):&#xA;  print (&#34;hello&#34;)&#xA;&#xA;y = input(&#34;iterations&#34;)&#xA;for x in range(0,int(y)):&#xA;  print_name(y)&#xA;Will ask for user input, store this in the variable y, as an integer.  It run a loop which calls the function y times.   Which in turn prints hello. &#xA;&#xA;If you add the line&#xA;&#xA;print(y)&#xA;&#xA;After the &#xA;&#xA;print(&#34;hello&#34;) &#xA;Line, then the program should output a number count for each iteration.   This can be a useful too for debugging loops.    This will also make use of the fact we are passing the value of y to the function. &#xA;&#xA;Links&#xA;&#xA;Code Club&#xA;Trinket&#xA;Python&#xA;&#xA;Tags&#xA;&#xA;#CodeClub,#Programming,#Education,#Python.#Functions]]&gt;</description>
      <content:encoded><![CDATA[<p>Code Club – Simple Python 3 Functions</p>

<p>As we are learning Python at Code Club,  I am going to make some related posts looking at the very basics of this.   We are using Trinket.io for this</p>

<p>Firstly open a <a href="https://trinket.io/library/trinkets/create?lang=python" rel="nofollow">new Python Trinket</a></p>

<p>Firstly we are going to tell the interpreter to use python3.</p>

<pre><code>#!/usr/bin/env python3 #use python 3
</code></pre>

<p>We are going to create a function called print_name() and then call it.  The function will just print <em>hello</em> to the standard output.</p>

<pre><code>def print_name():
  print (&#34;hello&#34;)
  
print_name()  
</code></pre>

<p>So this in does the same as what</p>

<pre><code>print (&#34;hello&#34;)
</code></pre>

<p>To the next step is to run the function several times.</p>

<p>We could run</p>

<pre><code>print_name()  
print_name() 
print_name() 
</code></pre>

<p>However it is probably better to put the <em>print_name()</em> in a loop, so it calls the function a specified number of times (in this case 4)</p>

<pre><code>for x in range(0,4):
  print_name()
</code></pre>

<p>So this will now call the <em>print_name()</em> four times (or however many times is specified.</p>

<p>Assuming this works,  we can now add some user interactivity</p>

<pre><code>y = input(&#34;iterations&#34;)
</code></pre>

<p>As we are dealing with numbers (integers) than y will be a string we fix this in the line for x in range(0,int(y)):  int(y) will convert string y in to integer y</p>

<p>Final program looks like this:-</p>

<pre><code>def print_name(y):
  print (&#34;hello&#34;)

y = input(&#34;iterations&#34;)
for x in range(0,int(y)):
  print_name(y)
</code></pre>

<p>Will ask for user input, store this in the variable y, as an integer.  It run a loop which calls the function y times.   Which in turn prints <em>hello</em>.</p>

<p>If you add the line</p>

<pre><code>print(y)
</code></pre>

<p>After the</p>

<pre><code>print(&#34;hello&#34;) 
</code></pre>

<p>Line, then the program should output a number count for each iteration.   This can be a useful too for debugging loops.    This will also make use of the fact we are passing the value of y to the function.</p>

<p><strong>Links</strong></p>
<ul><li><a href="https://www.codeclub.org.uk" rel="nofollow">Code Club</a></li>
<li><a href="https://www.trinket.io" rel="nofollow">Trinket</a></li>
<li><a href="https://www.python.org" rel="nofollow">Python</a></li></ul>

<p><strong>Tags</strong></p>

<p><a href="/paulsutton/tag:CodeClub" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">CodeClub</span></a>,<a href="/paulsutton/tag:Programming" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Programming</span></a>,<a href="/paulsutton/tag:Education" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Education</span></a>,<a href="/paulsutton/tag:Python" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Python</span></a>.<a href="/paulsutton/tag:Functions" class="hashtag" rel="nofollow"><span>#</span><span class="p-category">Functions</span></a></p>
]]></content:encoded>
      <guid>https://paper.wf/paulsutton/code-club-simple-python-3-functions</guid>
      <pubDate>Fri, 26 May 2023 06:00:00 +0000</pubDate>
    </item>
  </channel>
</rss>