What is Argument in Programming?

An argument is a statement that explains why something should happen. When applied to programming, an argument can take the form of a function call, or it could be in the form of data passed into a function. Arguments are often used as input for functions so that they will execute properly.

An argument can also be shortened to arg, and it is a data value that is passed by the user into a command, routine, or function.

Types of Arguments

In programming, there are different types of arguments that can be used based on the specific needs of the task at hand. Let’s explore some common types:

  • Required Arguments: These are the essential arguments that must be provided for a function or method to execute properly. Without these arguments, the program would encounter errors or fail to produce the expected results.
  • Default Arguments: Default arguments are optional values that are automatically assigned if no corresponding argument is provided. They allow programmers to define sensible fallback values, making the code more robust and less prone to errors.
  • Keyword Arguments: Keyword arguments are passed into a function or method using their corresponding parameter names. Unlike positional arguments (where the order matters), keyword arguments are identified by their names, which makes the code more readable and self-explanatory.
  • Variable Arguments: Sometimes, the number of arguments needed for a function may vary. In such cases, variable arguments come to the rescue. These arguments allow programmers to pass an arbitrary number of values, enabling greater flexibility and adaptability.

The Art of Argumentation

To make the most out of arguments in programming, it is crucial to choose meaningful and descriptive names for both the arguments and the functions themselves. This practice enhances code readability and makes it easier for other developers to understand and maintain the codebase.

Furthermore, carefully considering the order of arguments and their respective data types can contribute to writing cleaner and more efficient code. By following established conventions and best practices, you can ensure that your code is consistent and easier to work with for yourself and others.

Argument Examples

Example in Python

The following code will print “Hello World” to the console. To do this, there is one argument that is passed into a function called `print`. Within the brackets of the call to `print`, it states what should be printed which is simply ‘hello’.

//print out a string
print('Hello World')

Example in PHP

The example shows how to use a function to display a number as float with 2 decimal.

function f($source, $decimal = 2){        
    return number_format(floatval($source), $decimal, '.', ',');
}

echo f('1000'); //1000.00

Leave a Comment

Your email address will not be published. Required fields are marked *


Scroll to Top

By continuing to use the site, you agree to the use of cookies. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this.

Close