functions in Python code example

Example 1: python functions

def myFunction(say): #you can add variables to the function
  print(say)

myFunction("Hello")

age = input("How old are you?")

myFunction("You are {} years old!".format(age))

#this is what you get:

Hello
How old are you?
>>11 #lol my real age actually
You are 11 years old!

Example 2: how to write a function in python

# We use the def keyword to write a function in python
# Format: def function_name():
# For example:
def Bark():
  print("Bark! Bark!")

# If we want to run the function
Bark()

Example 3: python how to make a function

#Functions can be re used in programmes and can be extremely useful. 
#You would use functions a lot when you make programmes using packages such 
#as Tkinter, of which you will in the future.
#The Format:
#def [Whatever name you want]()      
#[The "()" at the end is compulsory as we are making a function.]

#Think of it like using an inbuilt function from python like .lower()


def greet():
	print("Hello!")
    
greet() #We are recalling the function by typing out its name and brackets


#This function will have something known as a parameter aka arguement
#This example will show a non changeable arguement unless coded


#Option 1, will directly print the sum:
def math(num1, num2):
	sum = num1+num2
	print(sum)

math(1, 2) #We change the num 1 and num 2 to the one and 2, though this can't change unless progammed to.

#Option 2, will return the sum and then print upon command.
def math(num1, num2):
  sum = num1+num2
  return sum

print(math(1, 2))

#Good luck to all my future Software engineers! Hope life treats you all well!
#Inshallah! (Meaning if Allah allows it!)

Example 4: functions in python

#Function Tutoral:
def hello():
  print("hello")
"""To make a function, it needs def then nameOfFunction() and a : to 
make the function work, you don't need a closing tag, as long as there is
tabbed section."""

def add(a, b): #This time, there is two inputs for the function to prossess. 
  c = a + b 
  return c
"""What the function above does is you input 2 numbers, and then it returns 
#The Value c, Calling it is as simple as add(5, 1) 
#What return does, is it almost makes a varible. So you can do:
#70 + add(10, 20) and it will return with: 100. This is because it will
go 70 + 30, as the function returned 30 because the inputs were 10 and 20."""

"""Functions can be called by code, as long as the function has already
been defined. Hope this helped you in your python journey!"""

Example 5: functions in python

# Functions

""" All the functions are followed by the 'def' keyword in python"""

def Greet(User): # The arguments inside the brackets should be called or the function gives you an error
  print("Hello" + User + "!")
  
# Calling the function

Greet("Shivram")

# Output: 'Hello Shivram !'

Example 6: functions in python

#Functions
#Functions are followed by the 'def' keyword
#Name your function
def myfunc():
  a = 'This is a func'
  
#Calling the function  
myfunc()
print(myfunc())

Tags: