A function is a reusable block of code that is used to perform a single action and is executed only when called.
You can pass data, known as parameters, into a function.
Creating a Function
To start the definition of a function, use the def keyword.
Return value from a function
The return keyword is used when You want to return a value.
The return statement ends the execution of the function call and returnsthe result.
def hello(): return "Hello Webpedia" print(hello()) //Hello Webpedia
def hello(): return "Hello" print(hello(), "Webpedia") //Hello Webpedia
The return keyword can be anywhere inside a function body, although it is the last line of the function.
A function can have multiple return statements.
Exercises and examples
def hello(lang): if lang == 'en': return "Hello Webpedia" elif lang == 'fr': return "Bonjour Webpedia" else: return "Hi Webpedia" print(hello('en')) //Hello Webpedia print(hello('xyz')) //Hi Webpedia
A function can have more than one parameter.
def sum(a,b): c = a + b return c sum = sum(1,4) print(sum) //5
Hello there!I hope you find this post useful!
I'm Mihai, a programmer and online marketing specialist, very passionate about everything that means online marketing, focused on eCommerce.
If you have a collaboration proposal or need helps with your projects feel free to contact me. I will always be glad to help you!