A function is a named block of code. You can call it as many times as you want.
You can use other people's functions.
# A simple example.
def greeter():
print("Hello!")
greeter()
Hello!
An argument is a piece of information that you send to a function.
# Example, with one argument.
def greet_person(name):
print(f"Hello {name}!")
greet_person('philip')
Hello philip!
def greet_person(name):
print(f"Hello {name}!")
names = ['eric', 'evan', 'devin', 'philip', 'barack obama', 'abraham', 'psalm']
for name in names:
greet_person(name)
Hello eric! Hello evan! Hello devin! Hello philip! Hello barack obama! Hello abraham! Hello psalm!
# Text to speech.
from gtts import gTTS
import os
tts = gTTS(text='Good morning, gandhi.', lang='en')
tts.save("hello.mp3")
os.system("start hello.mp3")
from gtts import gTTS
import os
def say_message(message):
tts = gTTS(text=message, lang='en')
tts.save("message.mp3")
os.system("start message.mp3")
say_message('oink oink!')
names = ['eric', 'evan', 'devin', 'philip', 'barack obama', 'abraham', 'psalm']
for name in names:
say_message(f"Hello, {name}!")
You can write a function that does a bunch of work, and then returns something to the line that called the function:
def get_full_name(f_name, l_name):
"""Return a full name."""
full_name = f"{f_name} {l_name}"
return full_name
full_name = get_full_name('alex', 'honnold')
print(full_name)
alex honnold