Command Line Arguments: Passing Variables to Functions
When you create command line programs, you need to understand named parameters and the values that are sent to functions. With any function, you have a number of various parameters. For instance, you can have a function named “greet” that takes in a string, which is an array of characters. The following is an example of such a greet function:
function greet(string input)
You can have an “add” function that takes in two integer values. You separate each of your values with a comma. The following is an example of an add function:
function add(int x, int y)
Now, when you want to call these two functions, you use the function name with the required variables. You must pass the required values or the call will fail. For instance, if you want to call the add function, you provide to values. In this example, a call with the values 100 and 3 are used:
You can also pass other variables that contain the values you want to pass. For instance, the following code declares two integers and passes those integers to the add function:
int n1 = 100; int n2 = 3; add(n1, n2);
It does not matter what you name the past values. As long as the values are placed in the proper order, you properly pass the values, which in this case is 100 and 3.
Some functions actually return values. The returned value can then be assigned to an additional variable. You can also assign the returned function value to an existing variable. For instance, consider the following code:
int n1 = 100; int n2 = 3; n1 = add(n1, n2);
The function add runs and passes the values 100 and 3. The two numbers are added and assigned to n1. N1’s value is replaced from 100 to 103.
The best way to illustrate this example is to type the code into your own project and run it for yourself. Set a breakpoint at your variable declarations and then step through the code. Notice that n1 retains its value until the add function is executed. The add function executes and then returns the two added values and assigns the new value to the existing n1 variable.
There is also the concept of scope. When you declare a variable in a function, its value is only available within that function. You can declare the same variable in one function as another, and neither one will affect the other. When you pass the variable in a function, you pass only the value not the actual variable. You can think of the values and the variables as detached when you pass the variables in a function. The variable values are copied, sent to the function and then sent back as another copy.
Conversely, you can also pass variables by reference. Passing a variable by reference passes the actual variable memory location. Instead of passing a copy, you pass the “real” value stored in memory. When you change the value in a subsequent function, because you are dealing with physical memory space, you change the variable’s value throughout the entire code. You not only change the variable’s value in the function, the statement that calls the function also changes (if the variable is used). In the C language, this is called a pointer. In other languages, you set your variable when passing it to “by reference.”
In other words, in the above example, if you pass the n1 variable by reference, any changes made to that variable would be reflected in other areas of your code as well. If you call the n1 variable in any other part of your code, the value changes made in the function would also change the value outside of the function.
The best way to view this process is to change your function to pass by reference, and then step through your code in the debugger. As you step through the code, press F11 to step into the add function. As you change the n1 variable, go back to the calling function and take note of the new value. Again, this change is because you actually pass the memory address to the function. As you know, when you change a value in memory, it’s a physical change and not just a copy of your variable’s value.
This is a basic overview of functions and passing variables by value and by reference. Understanding functions, memory references and passing values help you better understand how your code works as it’s executed by the debugger.
Don’t stop here! Learn C in ten easy steps on Windows, Mac OS, or Linux today!
Command Line students also learn
Empower your team. Lead the industry.
Get a subscription to a library of online courses and digital learning tools for your organization with Udemy Business.