Functions

7 minutes
Share the link to this page
Copied
  Completed
You need to have access to the item to view this lesson.
One-time Fee
$69.99
List Price:  $99.99
You save:  $30
€66.45
List Price:  €94.93
You save:  €28.48
£55.32
List Price:  £79.03
You save:  £23.71
CA$98.04
List Price:  CA$140.06
You save:  CA$42.02
A$107.72
List Price:  A$153.89
You save:  A$46.17
S$94.09
List Price:  S$134.42
You save:  S$40.33
HK$544.68
List Price:  HK$778.15
You save:  HK$233.46
CHF 61.92
List Price:  CHF 88.46
You save:  CHF 26.54
NOK kr774.79
List Price:  NOK kr1,106.89
You save:  NOK kr332.10
DKK kr495.67
List Price:  DKK kr708.13
You save:  DKK kr212.46
NZ$118.82
List Price:  NZ$169.76
You save:  NZ$50.93
د.إ257.07
List Price:  د.إ367.26
You save:  د.إ110.19
৳8,339.40
List Price:  ৳11,913.94
You save:  ৳3,574.54
₹5,914.23
List Price:  ₹8,449.26
You save:  ₹2,535.03
RM311.24
List Price:  RM444.65
You save:  RM133.41
₦118,083.62
List Price:  ₦168,698.12
You save:  ₦50,614.50
₨19,391.36
List Price:  ₨27,703.14
You save:  ₨8,311.77
฿2,410.17
List Price:  ฿3,443.25
You save:  ฿1,033.08
₺2,423.71
List Price:  ₺3,462.60
You save:  ₺1,038.88
B$418.75
List Price:  B$598.25
You save:  B$179.49
R1,273.04
List Price:  R1,818.71
You save:  R545.66
Лв129.87
List Price:  Лв185.54
You save:  Лв55.66
₩97,732.12
List Price:  ₩139,623.30
You save:  ₩41,891.18
₪255.55
List Price:  ₪365.09
You save:  ₪109.53
₱4,110.61
List Price:  ₱5,872.56
You save:  ₱1,761.94
¥10,630.43
List Price:  ¥15,186.98
You save:  ¥4,556.55
MX$1,418.07
List Price:  MX$2,025.91
You save:  MX$607.83
QR254.36
List Price:  QR363.39
You save:  QR109.02
P953.37
List Price:  P1,362.02
You save:  P408.64
KSh9,063.70
List Price:  KSh12,948.70
You save:  KSh3,885
E£3,478.89
List Price:  E£4,970.06
You save:  E£1,491.16
ብር8,820.94
List Price:  ብር12,601.88
You save:  ብር3,780.94
Kz63,759.63
List Price:  Kz91,089.09
You save:  Kz27,329.46
CLP$68,404.72
List Price:  CLP$97,725.22
You save:  CLP$29,320.50
CN¥507.53
List Price:  CN¥725.08
You save:  CN¥217.54
RD$4,206.74
List Price:  RD$6,009.89
You save:  RD$1,803.14
DA9,351.13
List Price:  DA13,359.34
You save:  DA4,008.20
FJ$158.85
List Price:  FJ$226.94
You save:  FJ$68.09
Q538.41
List Price:  Q769.20
You save:  Q230.78
GY$14,600.65
List Price:  GY$20,858.96
You save:  GY$6,258.31
ISK kr9,615.92
List Price:  ISK kr13,737.62
You save:  ISK kr4,121.70
DH699.24
List Price:  DH998.96
You save:  DH299.72
L1,281.52
List Price:  L1,830.82
You save:  L549.30
ден4,084.06
List Price:  ден5,834.63
You save:  ден1,750.56
MOP$559.24
List Price:  MOP$798.95
You save:  MOP$239.71
N$1,266.15
List Price:  N$1,808.86
You save:  N$542.71
C$2,568.06
List Price:  C$3,668.81
You save:  C$1,100.75
रु9,427.87
List Price:  रु13,468.97
You save:  रु4,041.09
S/262.66
List Price:  S/375.25
You save:  S/112.58
K281.39
List Price:  K402.01
You save:  K120.61
SAR262.91
List Price:  SAR375.60
You save:  SAR112.69
ZK1,903.42
List Price:  ZK2,719.29
You save:  ZK815.86
L330.70
List Price:  L472.45
You save:  L141.75
Kč1,679.39
List Price:  Kč2,399.23
You save:  Kč719.84
Ft27,552.80
List Price:  Ft39,362.83
You save:  Ft11,810.03
SEK kr765.72
List Price:  SEK kr1,093.94
You save:  SEK kr328.21
ARS$70,549.59
List Price:  ARS$100,789.45
You save:  ARS$30,239.85
Bs482.26
List Price:  Bs688.98
You save:  Bs206.71
COP$307,907.55
List Price:  COP$439,886.78
You save:  COP$131,979.23
₡35,643.95
List Price:  ₡50,922.11
You save:  ₡15,278.16
L1,765.02
List Price:  L2,521.57
You save:  L756.54
₲545,488.80
List Price:  ₲779,303.12
You save:  ₲233,814.31
$U2,997.83
List Price:  $U4,282.79
You save:  $U1,284.96
zł286.31
List Price:  zł409.03
You save:  zł122.72
Already have an account? Log In

Transcript

Welcome back, and welcome to lesson 15. In this lesson, we're going to talk about functions. So what is a function? A function is a component that returns a value back to the part of the program that called it a group of statements that perform a specific task. Whenever you want to execute a function, you just call it. So there are two types of functions.

There are library functions. And you can write your own functions. So when we talk about library functions, what we mean is, it's a function that is just built right into the program. So just like you go to the library that contains a bunch of books, if you want to get a book, if you want to pull a premade function, there is a library that is built right into the software, you can go there and look for that function. You just use them whenever you need them. So here's a function x Random is a function.

Random is like a tiny little program. And it already has a function, right? It generates random numbers. So that can be useful if you if you want to play a game and you want random numbers to generate, there are simulation programs that use that statistical programs, even computer security because maybe you want to generate like a random password. Okay, so there are a lot of ways that you could use this, this random function. Now random is a math function, so it always returns an integer value.

If you look at the example I have over here to the right, set number equal random because that's how we call the function we just say random and then we put some arguments in parentheses. Okay, and what those arguments are We want a random number between one and 100. So that's all that means when you're putting those arguments in, you're kind of outlining the barriers of what it is that you want to return. So we could put one in 1000 there. And we could say any number between one 1000, we could change that we don't have to use one, we might want to start off at 100. Even.

So, putting those in the parentheses is called an argument. And basically, what you're doing is you're setting those values. So random with one comma 100. In parentheses, that's called the function call. The function name is random. All we had to do was put in random and in order to call it and so it's, it's just like, you know, your dog, you named your dog and you want your dog come to you call him by name, right.

I know that's a very simple example. But sometimes, when we get into to terminology, we think we're thinking So hard and so hard and, and the Epiphany just doesn't hit us because we're thinking too too hard. And really, that's all it is we're generating something to do something and then we call it when we want that something to happen. So not only do programs have the library functions that often are quite an awesome library and can save you so much time because you don't have to write them yourself. If you needed to, you can certainly write functions yourself. And it's not really it's not really very difficult once you get some practice and practice, I think is the main key for this.

So when you're writing your own functions, the first line of the function definition is called the function header. So the first thing you have to do is start the header of it. So again, we have our rules on how things are supposed to be written out in programming. So think of it that way. The first line has the data type, the parameter value And then the return values as well. So if you look at the picture over here, you see function in injure some, all right, so function is like calling your function it's the same way you're telling the compiler that you are creating a function, okay?

Integer, that's the data type and so that tells us that that returns an integer. And then some you have to give your new function that you're creating a name, so that you can call it later by its name just like we call your doggie. And then in the parameters, we have an integer because we have to say the type num one and integer num two so num one to num two are being used at per as our variables which are being used as parameters in this case. Okay, so after the function header comes before function body and these are the statements that are to be executed. So what do you want to happen within this function every time you call it, one or more statements must be returned statement. So how do you know what to return?

Well, what value is going to be returned when that function ends? If you can answer that question, you know exactly what's going to be returned. So we have in the body declare integer result, if you look at the pretty, pretty little picture I've got there for you declare integer result. So we're declaring the type integer because it's going to be a number. And the variable result we're calling it result because that's what we want to name it. Set result equal to num one plus num two, that's another statement that needs to be executed.

We're setting the variable result to the value of whatever num one is plus whatever num two is. So If we're setting that result, and then we want to see what that result is, we're going to return result. And then we have to end the function. A lot of times in your programming language and this is this is language specific, you may have to end something that you start. For example, sometimes if statements may have an end if statement function may have an end function statement. So that is just a little something that you have to get used to once you get to learning the different programs just know right now at this point, that that is a possibility and it can happen.

And what would this class be if on the last slide we didn't say Yay, now we've completed Lesson 15. It is exciting as you learn more every every single day and so have that inquisitive Mind, if you have a question, look it up look into it, you know, Google's there for a reason. And when you take these classes, and you learn what the terminology means, and then you go in and you practice, you put your hands in it, and you start understanding what the terminology is linked to when you're actually doing something, and then it all is going to start clicking. But it does take time, it does take time. And if you need to replay and replay and replay so that you can get all the stuff in your mind, do it. Absolutely.

I know that repetition is old school, but personally, I think it's still a very valid technique. We don't want it to be the only technique we use because we need to understand how to use our hands when we get in and actually do it too. But repetition, still still viable. Alright, I'll see you in the next lesson.

Sign Up

Share

Share with friends, get 20% off
Invite your friends to LearnDesk learning marketplace. For each purchase they make, you get 20% off (upto $10) on your next purchase.