Python 3 Programming Tutorial – Function Parameter Defaults




In the last tutorial, we explained what function parameters were, and that we could have an infinite amount of them. With this, we explained how having a lot of them could become troublesome and tedious for the programmer using the function. Luckily, there is a solution for this. Sometimes, it can make a lot of sense to make a complex function highly customize-able. That said, some people may want to use the function in it’s simple… “default” … form. Think about it like buying a car. Some people want to just buy the base model, with all the features that come by default from the factory. Other people want to customize their car in many different ways. Luckily, we allow people who want to customize their car the option to do it, but we do not ask every buyer of every car what kind of wheels they want, what brand of tire, what screws they want, what kind of leather seats by what brand, what windshield, what steering wheel, what lights… etc. This is just too much for some people and what they want to use the car for. Same goes for functions in programming.

So we have function parameter defaults, which allow the function’s creator to set “default” values to the function parameters. This allows anyone to use a function with the default values, yet lets anyone who wishes to customize them the ability to specify different values.

When using defaults, any parameters with defaults should be the last ones listed in the function’s parameters.

Sample code for this basics series:

Python 3 Programming tutorial Playlist:

Bitcoin donations: 1GV7srgR4NJx4vrk7avCmmVQQrqmv87ty6

source

42 thoughts on “Python 3 Programming Tutorial – Function Parameter Defaults”

  1. Thank you for these tutorials because these are so far some of the best ones I have come across so far. I've tried others but they don't always explain the stuff like they should. I have also recomended these videos as well as some others of yours to other people. 🙂 Thank You.

  2. I am new to parameters, so after looking at your video, i worked out that you can do this:
    def simple_addition (number_one , number_two):
        answer = number_one + number_two
        print (number_one , "+", number_two , "=", answer)

    simple_addition (number_one = int(input ("Say a number…")), number_two = int(input("Say another number…")))

    this asks for 2 numbers and then adds them…
    (The spacing may mess up as this is in youtube comments)

  3. great content just want to verify something since im quite new to programming, what does typing 'Scrollbar=True' mean? sorry if thats a really nooby question

  4. you are really spending our time. you are speaking a lot. thank you for your effort. however there are many useless words in your tutorial. hope you eliminate your speech and be straightforward
    .

  5. I'm getting an error that doesnt let me run the program:
    "positional argument follows keyword argument"
    Do you know what it means, or any idea of why it's caused?

  6. I get an syntax error when I try to do this. This is what is looks like :

    def simple(num1,num2):
    pass
    def simple(num1,num2=5):

    SyntaxError: invalid syntax

    What am I doing wrong?

  7. we cant make the first parameter default and the second non-default, as PYthon tells me that a non-default argument cannot follow after a default argument. Why is that ?

  8. question: at 5:00 you changed bgc's value to 'b' in the calling function. now, first a function is called and then it is executed. so when it is executed, control goes to the part where function is defined. so why is bgc's value changed to 'b' when it it's default is 'w' in the defined function?

  9. seriously…your instructional videos are better than what my college professor teaches. Thank you for taking the time to do these…. these are the best PY tutorials out there.

  10. Thank you very much sentdex. Your tutorials helps a lot, you can teach very good and it makes fun to listen to you. Again, thank you!

  11. I like the way of your teaching. I have learnt python and Django only from your videos. Hoping for more videos on Django! 🙂

  12. You are the great. You are even better than my college and my private institute teacher.
    But I want you to ask what is the first function definition is for in this video tutorial.

  13. I just started learning Python and your tutorials are the easiest and simplest to understand so far! (working with python3.6). You honestly teach better than my programming teachers! Thank you for taking the time to make this, it means a lot!

  14. i want to comment on every single video but because i want to see more i skip to the next one almost immieditely without a comment xD

  15. Nice, so is it possible that when I don't want a scrollbar, I have to type (in this example):
    " basic_window(500,350,,,scrollbar=False) "

  16. hi All, can anyone tell me what I have done ? What's that "at 0x10063c320"?

    >>> def simplex(num1, num2=5):
    print (num1+num2)>>> simplex

    >> simplex
    <function simplex at 0x10063c320

  17. im confuse as to why the

    def simple(num1, num2) :
    pass

    is necessary for this program. What is purpose of the first 2 line of codes then? Cant you just keep the line of code:

    def simple(num1, num2=5) :
    print(num1,num2)

    Its confusing because you just created 2 functions with the same name

  18. Hey, I'm new to programming and I must say you are the best teacher I've ever seen!!! I've watched about ten other YouTubers that do what you do and none of them teach as good as you. I have learned all my coding from you and I have made my own games from all the things you have taught me. I really appreciate that you love to help.

Leave a Reply