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
Computer Programming for Absolute Beginners by Joakim Wassberg.mobi
Computer Programming for Absolute Beginners by Joakim Wassberg.azw3
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.
Deep Learning with Python by François Chollet(12570)
Hello! Python by Anthony Briggs(9915)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(9796)
The Mikado Method by Ola Ellnestam Daniel Brolund(9778)
Dependency Injection in .NET by Mark Seemann(9339)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(8297)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(7763)
Grails in Action by Glen Smith Peter Ledbrook(7696)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(7557)
Becoming a Dynamics 365 Finance and Supply Chain Solution Architect by Brent Dawson(7073)
Microservices with Go by Alexander Shuiskov(6839)
Practical Design Patterns for Java Developers by Miroslav Wengner(6760)
Test Automation Engineering Handbook by Manikandan Sambamurthy(6700)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(6413)
Angular Projects - Third Edition by Aristeidis Bampakos(6104)
The Art of Crafting User Stories by The Art of Crafting User Stories(5633)
NetSuite for Consultants - Second Edition by Peter Ries(5567)
Demystifying Cryptography with OpenSSL 3.0 by Alexei Khlebnikov(5373)
Kotlin in Action by Dmitry Jemerov(5062)
