Python Notes #6 Functions

 Hi Everyone,

Today I'm doing functions I have kept this as its own blog as this is a bit of a tougher topic and for my own memory I'd like to be able to find this easily if I ever had to review python notes.

A function is a chunk of reusable code this means that the code can be created and then passed anywhere within the code later on. This is how a function works - 

def name(first, last):
print("Please enter name: ")
print(f"Your name is: {first} {last}")
name("Iain", "H")

In this chunk of code I pass the parameters first & last within the function - from here I create a print statement asking for my name, notice how in the second print statement I am passing the parameters first and last (represented by the curly braces) and lastly I call the function name and define the values within the brackets - with the last statement now first will = 'Iain' and last will = 'H'. This code produces this output -

Please enter name: 
Your name is: Iain H

Return - an executed function is equal to the RETURN value. I will not elaborate on this more as this has taken me a while to understand and I don't want to muddle myself up. 

Print vs Return - 
Print is a function you call. Calling print will immediately make your program write out text for you to see. Use print when you want to show a value to a human.

return is a keyword. When a return statement is reached, Python will stop the execution of the current function, sending a value out to where the function was called. Use return when you want to send a value from one point in your code to another.

Using return changes the flow of the program. Using print does not.


    

Comments

  1. In the dynamic programming world, mastering a versatile language like Python can open up numerous opportunities. If you're looking for the best Python Institute in Noida to acquire comprehensive and industry-relevant knowledge, look no further than APTRON Solutions Noida. With a proven track record of excellence, APTRON Solutions Noida stands out as a premier destination for honing your Python skills.

    ReplyDelete

Post a Comment

Popular posts from this blog

Python Crash Course Chapter 1-2! #1

I PASSED! Cisco CCNA 200-301! Experience & Where I'm going from here!