Whats your Function/ Define your Function
Functions are used to group a set of instructions and logic that performs a specific
task. So, we should make functions perform one specific task and choose a name that
gives us a hint about that. Functions are defined using a def
statement.
The word def is followed by the name of the function with an optional list of
parameters in parentheses, and the line ends with a colon, which indicates that the next
lines must be indented as a block or suite of instructions.
Let’s start with a function that takes no parameters:
def welcome():
print('Welcome to the Level1Techs Forum')
print('This is the Developers section of the forum')
print('Here we will cover all things Python !')
welcome()
In the previous example, we have the def
statement, which defines a function named welcome()
. The code in the block that follows the def
statement is the body of the function. This code is executed when the function is called, not when the function is first defined. The welcome()
line following the code is a function call. When the program execution reaches the welcome()
function call, it simply jumps to the top line where it is defined and execute the function. Functions also can compute a result value and let us specify parameters that serve as function inputs and may differ each time the code is run.
Coding an operation as a function makes it a generally useful tool, which we can use in a
variety of contexts. More fundamentally, functions are the alternative to programming by
cutting and pasting rather than having multiple redundant copies of an operation’s code,
we can factor it into a single function. In so doing, we reduce our future work radically.
A major purpose of functions is to group code that gets executed multiple times.
Without a function defined, you would have to copy and paste this code each time.
Q: This is propably the longest start to any of the coversations we've had. Why so serious?
A: There are many facets to functions. Functions are a nearly universal program-structuring device.
You may have come across them before in other languages, where they may have been called
subroutines or procedures. Functions serve two primary development roles:
1. Maximizing code reuse and minimizing redundancy
2. Procedural decomposition
Q: Can we cover most of this in code? Some of these post are getting long and boring...
A: I guess it's time to actually start posting code and having you get your hands dirty.
This will also mark the inaguration of the Code_Warrior Corner
Here is a sample of def()
you might find ineresting, Open up your favorite text editor
(hopefully one with a linter if possible) and follow this one called Classic Quotes:
from random import choice
def Quotes(Classic_Quote):
if Classic_Quote == 'a':
return 'Luke I am your Father !!!'
elif Classic_Quote == 'b':
return 'When nine hundred years old you reach, look as good you will not'
elif Classic_Quote == 'c':
return 'Truly wonderful, the mind of a child is.'
elif Classic_Quote == 'd':
return 'That is why you fail.'
elif Classic_Quote == 'e':
return 'A Jedi uses the Force for knowledge and defense, never for attack.'
elif Classic_Quote == 'f':
return 'Adventure. Excitement. A Jedi craves not these things.'
elif Classic_Quote == 'g':
return 'Judge me by my size, do you?'
elif Classic_Quote == 'h':
return 'Fear is the path to the dark side…fear leads to anger…anger leads to hate…hate leads to suffering'
elif Classic_Quote == 'i':
return 'Wars not make one great.'
elif Classic_Quote == 'j':
return 'Luminous beings are we…not this crude matter.'
elif Classic_Quote == 'e':
return 'Do. Or do not. There is no try.'
elif Classic_Quote == 'e':
return 'Never tell me the odds!'
ran = choice('abcdefghijkl')
txt = Quotes(ran)
print(txt)
There are sure to be questions on this post, I know many of you want to get to what we are all here for and that is CODE ! So post your simple functions in the comments section and let's start a code challenge to use your function in another program! Even better use all that you've learned in this short course and post it as well !
Code up Code_Warriors !!!