+ 1

Stuck at function programming

Hey everyone! Just started with my learning about 3 weeks ago, however I hit a brick wall when it comes to the function programming. Just wondering if there are any videos or tips on how to make it seem clearer thanks!

29th Mar 2018, 9:38 PM
Arick
5 Answers
+ 3
You can search them by yourself but there aren't much :/. Just a tip, Use many functions and don't feel like its stupid to make everything a function. It makes it looks the cleanest and more comfortable in advanced stuff
29th Mar 2018, 10:06 PM
Tomer Sim
Tomer Sim - avatar
+ 3
I will try to explain functions as simple as I can. Functions are blocks of code that can be used multiple times to make the code cleaner and make it repeat less. Functions are defined by using the def keyword then the name afterwards parameters/arguments inside brackets like so... def my_func(): # some code Parameters are values a function can accept to do something with them like make calculations. Lets make an add function to add 2 numbers... def add(a, b): # a and b are the parameters/arguments a + b There, but there is a problem. How are we going to give the thing that used/called the function the result? The answer is the return keyword... def add(a, b): return a + b Now the add function will 'return' the value to whoever called it (used it) something like... def add(a, b): return a + b c = add(2, 3) # c now has the value of 5 print(c) # prints 5 In this case parameter a is 2 and parameter b is 3. There are also default function parameters which means that if the caller doesn't give a parameter a value it won't freak out and throw an error. We use the = sign to tell python that the parameter will have a default value like... def add(a=0, b=0): return a + b Now if the caller doesn't give any parameters or just one the function will still run. Example: def add(a=0, b=0): return a + b c = add(4) # c is now 4 (4+0) print(c) # prints 4 Hope this helps :)
29th Mar 2018, 10:16 PM
TurtleShell
TurtleShell - avatar
+ 2
Whe you define a function you assign a variable to a code. this makes it easier of u want to use the code multiple times. it also makes ur code easy to understand
30th Mar 2018, 3:27 AM
Kaustubh Patil
Kaustubh Patil - avatar
+ 2
Take this example, I want to create a calculator program that does multiple operations on one command. like first multiplying then adding the multiplication to the bigger of one of the numbers. a function could make this simple. to identify the input and give the desired output. def xcalc(x,y): m = x+y return m + max(x,y) i = input('The number: ') j = input('The other one: ') #lets say the above operation is multimaxx k = input('the operation: ') if k == 'multimaxx': print(xcalc(i,j)) else: print('Please type a valid operation') i can even do this multiple times with multiple operations using only a single command instead of repeating the code over
30th Mar 2018, 3:35 AM
Kaustubh Patil
Kaustubh Patil - avatar
+ 1
Whe you define a function you assign a variable to a code. this makes it easier of u want to use the code multiple times. it also makes ur code easy to understand
30th Mar 2018, 3:27 AM
Kaustubh Patil
Kaustubh Patil - avatar