
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to pass optional parameters to a function in Python?
Python allows function arguments to have default values; if the function is called without the argument, the argument gets its default value. Furthermore, arguments can be specified in any order by using named arguments.
For the given code
OUTPUT
('Hello', 'Archie, Good morning! Come on in') ('Hello', 'Richie, How do you do? Come on in')
Messages msg and msg2 are optional, because they have default values defined. name is a required argument, because it has no default value. If greet is called with only one argument, msg defaults to “Good Morning” and msg2 defaults to “Come on in”. If greet is called with two arguments, msg2 still defaults to “Come on in”.
In most languages, we would need to call the function with three arguments. But in Python, arguments can be specified by name, in any order.
- Related Questions & Answers
- How to pass keyword parameters to a function in Python?
- How can I declare optional function parameters in JavaScript?
- How to pass the parameters in the PowerShell function?
- How to pass parameters to a method in C#?
- Optional Parameters in Dart Programming
- How to pass reference parameters PHP?
- What is the best way to do optional function parameters in JavaScript?
- How to pass Python function as a function argument?
- How to pass the value 'undefined' to a function with multiple parameters in JavaScript?
- How to pass pointers as parameters to methods in C#?
- How to pass parameters using param array in a C# method?
- Newman Optional Parameters & Configurations using Postman
- How to pass a dictionary as argument in Python function?
- How to pass arguments by reference in a Python function?
- How to pass arguments by value in Python function?
Advertisements