Computer Programming for Absolute Beginners by Joakim Wassberg

Computer Programming for Absolute Beginners by Joakim Wassberg

Author:Joakim Wassberg
Language: eng
Format: epub, mobi, azw3
Publisher: PACKT Publishing LTD.
Published: 2020-07-30T00:00:00+00:00


Function arguments

Often, we want our functions to be somewhat flexible, so they don't do exactly the same thing every time we call them. Consider the following two functions:

function add_2_and_3()

return 2 + 3

end_function

function add_5_and 9()

return 5 + 9

end_function

These two functions add two numbers and return the result. The first one adds 2 and 3, and the second one does the same but with 5 and 9. Now, these are just two pairs of numbers. If we would like to have a function that could add any numbers, we would need to create an endless number of functions.

But if we look at what the functions do, we can see that they are actually doing the same thing. They add two numbers and return the result. The only thing that changes are the numbers that are used in the addition operation.

What we want is to be able to pass the numbers we want to be added to the function so it can use them in the calculation, and by that, only have one function that can add any two numbers.

We can pass data to functions. We say that the function can accept arguments. For a function that can add two numbers, this can look like this:

function add(number1, number2)

return number1 + number2

end_function

Here, we can see that we now have made use of the parenthesis that has followed the function name. This is where we can specify variables that will receive the data we want to pass to the function. Now we can use this function like this:

result = add(73, 8)

print(result)

result = add(2, 3)

print result

result = add(5, 9)

print result

The output, when running this program, will be this:

81

5

14

As we can see, we can now serve any two numbers using only one single function. Let's examine this a bit closer to really understand how this works.

Let's remove two of the calls to the function and only focus on what happens when we call the function the first time. We will have this code:

function add(number1, number2)

return number1 + number2

end_function

result = add(73, 8)

print result

When we make the call to the function, we are passing in the values, 73 and 8. These are called arguments. On the receiving side, that is, in the add function, these two values will be assigned to two variables, number1 and number2. These are called the parameters of the function. The first argument that we pass in, the value, 73, is assigned to the first parameter, number1, and the second argument, the value, 8, will be assigned to the parameter called number2.

When we enter the function body, we now have these two variables and they are assigned the values that were passed to the function. So, this time, the variables will have the values 73 and 8 respectively.

When we call the function with the values, 2 and 3, then number1 will get 2 and number2 will get 3. This means that the parameters in the function will receive data in the same order as the paramters are passed to the function:

add(4,



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.