Python Basics: Define A Function

It is easy…and it’s similar to if or for statement formats:

def functionName():
  //logics here...

And when you use a function:

def funPrintHello():
  return "Hello" < - return a value
//Or
funPrintWorld():
  print("World")
print(funPrintHello()) <- calling the function in print()
funPrintWorld() <- calling the function

Python will read the print(funPrintHello()) line first, once it detects the funPrintHello() function in the print() function, it will go back to where this function has been defined, then read the funPrintWorld() line.

The indentation level of Python is very important, it defines if a statement or a function is inside another statement or function (like the right of possession?). The indentation level is expressed in line breaks & spaces: it needs you to break the line if the statement has finished, then the second row should have one more space in front than the first row; otherwise, it will most likely report an error.
For example:

def functionName(): 
  print("Hi") <- There must be space before print()

if 4 in list: 
  return "The number 4 is in the list" <- Must have a space before the 
return key word
var=1 var2=2 <- WRONG

//The following format is correct
var=1 <- Need to break a line
var=2 

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.