Trending February 2024 # Return The First And Last Values In A Range # Suggested March 2024 # Top 4 Popular

You are reading the article Return The First And Last Values In A Range updated in February 2024 on the website Kientrucdochoi.com. We hope that the information we have shared is helpful to you. If you find the content interesting and meaningful, please share it with your friends and continue to follow and support us for the latest updates. Suggested March 2024 Return The First And Last Values In A Range

I’ve been asked many times how to find either the cell reference of the first or last value in a range, or even return the values from those cells, and there are many ways to do it.

As usual I’m going to share the methods I think are the best.

Note: The difference between returning the value or cell reference is subtle yet significant. You’ll see later.

Here’s our range. You can see that column C contains numbers and column D contains both numbers and text, and both columns contain blanks.

Find the First Value in a Range

Like I said, I’ve seen many ways to find the first value in a range but one formula stands out from the rest for its simplicity.

Drum roll…..

Entered with CTRL+SHIFT+ENTER as it’s an array formula.

Why it’s so special:

The INDEX function returns a reference to a cell and when used on its own, like the above example, it returns the value in that cell.

This formula works with text or numbers.

How it Works

If you’re new to the INDEX & MATCH dynamic duo take a few moments to read this tutorial first. These are two ‘must know’ power functions.

The MATCH function is the star in this formula:

If we look at how the MATCH function evaluates it looks like this:

Step 1: Evaluate the logical test and return an array of TRUE and FALSE values:

MATCH(

TRUE

,

{FALSE;FALSE;TRUE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;FALSE}

,

0

)

Step 2: Return the position number of the first ‘TRUE’ result in the array:

3

That is the first TRUE is the 3rd item in the array returned by the logical test.

If we give this result to the INDEX formula we get:

=INDEX(C$2:C$13,

3

)

Which evaluates to cell C4 and coincidently the value in C4 is 3.

That is cell C4 is the 3rd cell in the range C2:C13 and it contains the number 3.

That’s it! Done.

Find the Last Value in a Range

Unfortunately finding the last value in a range isn’t quite as straight forward as finding the first, but it’s not too tricky.

Again entered with CTRL+SHIFT+ENTER as it’s an array formula.

Let’s zoom in on the MAX(IF part of the formula:

In English:

Test the cells in the range C2:C13 for blanks, if they’re not blank i.e. TRUE then return the corresponding number from the array of values returned by the ROW function, now just tell me the MAX value.

It evaluates like so:

Step 1 – evaluate the logical test:

MAX(

IF({FALSE;TRUE;TRUE;FALSE;TRUE;TRUE;FALSE;TRUE;FALSE;FALSE;TRUE;FALSE},

ROW($A$1:$A$12)

)

)

Step 2 – evaluate the ROW function:

MAX(

IF{FALSE;FALSE;TRUE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;TRUE;FALSE;FALSE},

{1;2;3;4;5;6;7;8;9;10;11;12}

)

)

Step 3 – complete the IF formula by replacing the TRUE’s with the corresponding numbers returned by the ROW function:

MAX(

{FALSE;FALSE;

3

;

4

;FALSE;

6

;FALSE;

7

;FALSE;

10

;FALSE;FALSE}

)

Step 4 – find the MAX of the values:

10

Now we can give that 10 to INDEX and we get this:

=INDEX(C$2:C$13,

10

)

=C11, and the value in C11 is 4.

This formula works for both text and numbers.

Find the Last Number in a Range

If your range contains only numbers, you can use this formula to find the last cell containing a number in the range:

=INDEX(C$2:C$13,MATCH(1E+100,C$2:C$13,1))

Note: for a change this isn’t an array formula.

The trick with this formula is the 1E+100.

1E+100 is just a really big number. You can use any number here as long as it is bigger than any number in your range.

Side note: You might have seen 9.99999999999999E+307 used in formulas before and that’s because it’s the biggest number you can enter into Excel.

The problem with 9.999 blah, blah, blah is that I find it really hard to remember how many 9’s to enter. Whilst 1E+100 is not the biggest number, it is still big enough to work in most scenarios and comes with the bonus that it’s a whole load easier to remember.

Ok, sorry I digress… when you ask MATCH  to find a number that is bigger than any of the numbers in your range it will return the position of the last value in the range when you use 1 (for less than) as the ‘match_type’ argument.

Remember the syntax for MATCH is

MATCH(

lookup_value

,

lookup_array

,

[match_type]

)

Lookup the biggest number you can imagine, in the range C2:C13, if you can’t find it then return the position of the last number you find.

Remember this formula only works with numbers, not text.

Using INDEX to Return a Reference

“The difference between returning the value from a cell or a reference to a cell is subtle yet significant”

Well, since we know that the INDEX function returns a reference to a cell we can use the above formulas to return a range that starts with the first occupied cell and ends with the last.

To do this we simply join the two formulas together with a colon, which is the range operator:

Remember the first INDEX formula returns a reference of C4 and the second returns C11 so the above formula returns the following range:

C4

:

C11

What’s the point when you could just enter C4:C11? Well, because using INDEX gives us a dynamic range that will grow or shrink with your data.

Don’t forget if your range contains text you need to use the MAX(IF version:

In most cases you’ll need to enter a formula containing either of the above dynamic ranges using CTRL+SHIFT+ENTER, or you can set it up as a dynamic named range for use in your formulas.

Enter your email address below to download the sample workbook.

By submitting your email address you agree that we can email you our Excel newsletter.

Please enter a valid email address.

Download the Excel Workbook . Note: This is a .xlsx file please ensure your browser doesn’t change the file extension on download.

You're reading Return The First And Last Values In A Range

How To Create A Dynamic Chart Range In Excel

When you create a chart in Excel and the source data changes, you need to update the chart’s data source to make sure it reflects the new data.

In case you work with charts that are frequently updated, it’s better to create a dynamic chart range.

A dynamic chart range is a data range that updates automatically when you change the data source.

Below is an example of a chart that uses a dynamic chart range.

Note that the chart updates with the new data points for May and June as soon as the data in entered.

There are two ways to create a dynamic chart range in Excel:

Using Excel Table

Using Formulas

In most of the cases, using Excel Table is the best way to create dynamic ranges in Excel.

Let’s see how each of these methods work.

Using Excel Table is the best way to create dynamic ranges as it updates automatically when a new data point is added to it.

Excel Table feature was introduced in Excel 2007 version of Windows and if you’re versions prior to it, you won’t be able to use it (see the next section on creating dynamic chart range using formulas).

Pro Tip: To convert a range of cells to an Excel Table, select the cells and use the

To convert a range of cells to an Excel Table, select the cells and use the keyboard shortcut – Control + T (hold the Control key and press the T key).

In the example below, you can see that as soon as I add new data, the Excel Table expands to include this data as a part of the table (note that the border and formatting expand to include it in the table).

Now, we need to use this Excel table while creating the charts.

Here are the exact steps to create a dynamic line chart using the Excel table:

Select the entire Excel table.

Go to the Insert tab.

In the Charts Group, select ‘Line with Markers’ chart.

That’s it!

The above steps would insert a line chart which would automatically update when you add more data to the Excel table.

Note that while adding new data automatically updates the chart, deleting data would not completely remove the data points. For example, if you remove 2 data points, the chart will show some empty space on the right. To correct this, drag the blue mark at the bottom right of the Excel table to remove the deleted data points from the table (as shown below).

While I have taken the example of a line chart, you can also create other chart types such as column/bar charts using this technique.

As I mentioned, using Excel table is the best way to create dynamic chart ranges.

However, if you can’t use Excel table for some reason (possibly if you are using Excel 2003), there is another (slightly complicated) way to create dynamic chart ranges using Excel formulas and named ranges.

Suppose you have the data set as shown below:

To create a dynamic chart range from this data, we need to:

Create two dynamic named ranges using the OFFSET formula (one each for ‘Values’ and ‘Months’ column). Adding/deleting a data point would automatically update these named ranges.

Insert a chart that uses the named ranges as a data source.

Let me explain each step in detail now.

Below are the steps to create dynamic named ranges:

Go to the ‘Formulas’ Tab.

The above steps have created two named ranges in the Workbook – ChartValue and ChartMonth (these refer to the values and months range in the data set respectively).

If you go and update the value column by adding one more data point, the ChartValue named range would now automatically update to show the additional data point in it.

The magic is done by the OFFSET function here.

In the ‘ChartValue’ named range formula, we have specified B2 as the reference point. OFFSET formula starts there and extends to cover all the filled cells in the column.

The Same logic works in the ChartMonth named range formula as well.

Now all you need to do is insert a chart that will use the named ranges as the data source.

Here are the steps to insert a chart and use dynamic chart ranges:

Go to the Insert tab.

With the chart selected, go to the Design tab.

In the Series value field, enter

=Formula!ChartValues

(note that you need to specify the worksheet name before the named range for this to work).

In the ‘Axis Labels’ dialog box, enter

=Formula!ChartMonths

That’s it! Now your chart is using a dynamic range and will update when you add/delete data points in the chart.

A few important things to know when using named ranges with charts:

There should not be any blank cells in the chart data. If there is a blank, named range would not refer to the correct dataset (as the total count would lead to it referring to less number of cells).

You need to follow the naming convention when using the sheet name in chart source. For example, if the sheet name is a single word, such as Formula, then you can use =Formula!ChartValue. But if there is more than one word, such as Formula Chart, then you need to use =’Formula Chart’!ChartValue.

You May Also Like the Following Excel Tutorials:

Add Values In Sequence To The Previous Value With A Constant.

To add values in sequence to the previous value with a constant, we can find the cumulative sum of values and add the constant to the Output.

For Example, if we have a vector called X and we want to add values in X in sequence to the previous value with a constant say 10 then we can use the command given below −

10+cumsum(X) Example 1

Following snippet creates a sample data frame −

x1<-1:100 cumsum(x1)

The following dataframe is created

[1] 1 3 6 10 15 21 28 36 45 55 66 78 91 105 120 [16] 136 153 171 190 210 231 253 276 300 325 351 378 406 435 252 [31] 496 528 561 595 630 666 703 741 780 820 861 903 946 990 1035 [46] 1081 1128 1176 1225 1275 1326 1378 1431 1485 1540 1596 1653 1711 1770 1830 [61] 1891 1953 2024 2080 2145 2211 2278 2346 2415 2485 2556 2628 2701 2775 2850 [76] 2926 3003 3081 3160 3240 3321 3403 3486 3570 3655 3741 3828 3916 4005 4095 [91] 4186 4278 4371 4465 4560 4656 4753 4851 4950 5050

Add the following code to the above snippet −

x1<-1:100 x1_new<-1+cumsum(x1) x1_new Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 2 4 7 11 16 22 29 37 46 56 67 79 92 106 121 [16] 137 154 172 191 211 232 254 277 301 326 352 379 407 436 466 [31] 497 529 562 596 631 667 704 742 781 821 862 904 947 991 1036 [46] 1082 1129 1177 1226 1276 1327 1379 1432 1486 1541 1597 1654 1712 1771 1831 [61] 1892 1954 2023 2081 2146 2212 2279 2347 2416 2486 2557 2629 2702 2776 2851 [76] 2927 3004 3082 3161 3241 3322 3404 3487 3571 3656 3742 3829 3917 4006 4096 [91] 4187 4279 4372 4466 4561 4657 4754 4852 4951 5051 Example 2

Following snippet creates a sample data frame −

x2<-rpois(100,10) x2

The following dataframe is created

[1] 8 15 5 14 4 11 19 9 7 9 6 11 8 8 8 5 6 6 12 11 6 12 8 12 8 [26] 7 11 5 6 14 9 12 12 12 5 13 10 15 13 10 12 15 7 13 6 10 7 9 13 14 [51] 15 7 8 11 11 10 10 5 13 13 7 13 9 11 7 9 7 10 5 9 11 13 15 17 8 [76] 15 13 12 10 11 3 10 11 10 12 8 18 10 11 13 6 7 8 11 14 9 11 10 9 9

Add the following code to the above snippet −

x2<-rpois(100,10) cumsum(x2) Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 8 23 28 42 46 57 76 85 92 101 107 118 126 134 142 [16] 147 153 159 171 182 188 200 208 220 228 235 246 251 257 271 [31] 280 292 304 316 321 334 344 359 372 382 394 409 416 429 435 [46] 445 452 461 474 488 503 510 518 529 540 550 560 565 578 591 [61] 598 611 620 631 638 647 654 664 669 678 689 702 717 734 742 [76] 757 770 782 792 803 806 816 827 837 849 857 875 885 896 909 [91] 915 922 930 941 955 964 975 985 994 1003

Add the following code to the above snippet −

x2<-rpois(100,10) x2_new<-1+cumsum(x2) x2_new Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 9 24 29 43 47 58 77 86 93 102 108 119 127 135 143 [16] 148 154 160 172 183 189 201 209 221 229 236 247 252 258 272 [31] 281 293 305 317 322 335 345 360 373 383 395 410 417 430 436 [46] 446 453 462 475 489 504 511 519 530 541 551 561 566 579 592 [61] 599 612 621 632 639 648 655 665 670 679 690 703 718 735 743 [76] 758 771 783 793 804 807 817 828 838 850 858 876 886 897 910 [91] 916 923 931 942 956 965 976 986 995 1004 Example 3

Following snippet creates a sample data frame −

x3<-rpois(100,1) x3

The following dataframe is created

[1] 1 2 4 0 1 0 2 2 0 0 0 0 0 1 0 1 1 0 1 3 1 2 1 3 0 0 0 0 0 0 1 0 1 1 1 0 0 [38] 4 1 1 2 0 0 0 5 1 2 1 2 0 0 3 0 1 0 0 0 0 1 0 1 1 1 0 2 2 0 1 1 1 0 0 2 0 [75] 1 3 0 0 1 0 0 1 0 0 1 0 1 1 0 3 2 0 2 1 1 1 3 0 1 0

Add the following code to the above snippet −

x3<-rpois(100,1) x3 cumsum(x3) Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 1 3 7 7 8 8 10 12 12 12 12 12 12 13 13 14 15 15 16 19 20 22 23 26 26 [26] 26 26 26 26 26 27 27 28 29 30 30 30 34 35 36 38 38 38 38 43 44 46 47 49 49 [51] 49 52 52 53 53 53 53 53 54 54 55 56 57 57 59 61 61 62 63 64 64 64 66 66 67 [76] 70 70 70 71 71 71 72 72 72 73 73 74 75 75 78 80 80 82 83 84 85 88 88 89 89

Add the following code to the above snippet −

x3<-rpois(100,1) x3_new<-100+cumsum(x3) x3_new Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 101 103 107 107 108 108 110 112 112 112 112 112 112 113 113 114 115 115 [19] 116 119 120 122 123 126 126 126 126 126 126 126 127 127 128 129 130 130 [37] 130 134 135 136 138 138 138 138 143 144 146 147 149 149 149 152 152 153 [55] 153 153 153 153 154 154 155 156 157 157 159 161 161 162 163 164 164 164 [73] 166 166 167 170 170 170 171 171 171 172 172 172 173 173 174 175 175 178 x4

The following dataframe is created

[1] 74 24 43 40 34 97 30 46 25 66 88 45 85 63 49 52 67 69 [19] 36 71 39 86 54 70 11 60 68 91 37 3 58 65 7 61 21 26 [37] 73 82 29 64 10 22 99 13 76 84 19 1 72 78 6 18 5 42 [55] 2 57 89 98 4 8 33 12 14 94 83 100 50 44 28 79 53 31 [73] 75 77 87 56 62 20 51 90 80 41 35 48 17 81 16 32 92 55 [91] 9 47 23 15 27 93 38 96 59 95

Add the following code to the above snippet −

x4<-sample(1:100,100) cumsum(x4) Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 74 98 141 181 215 312 342 388 413 479 567 612 697 760 809 [16] 861 928 997 1033 1104 1143 1229 1283 1353 1364 1424 1492 1583 1620 1623 [31] 1681 1746 1753 1814 1835 1861 1934 2024 2045 2109 2119 2141 2240 2253 2329 [46] 2413 2432 2433 2505 2583 2589 2607 2612 2654 2656 2713 2802 2900 2904 2912 [61] 2945 2957 2971 3065 3148 3248 3298 3342 3370 3449 3502 3533 3608 3685 3772 [76] 3828 3890 3910 3961 4051 4131 4172 4207 4255 4272 4353 4369 4401 4493 4548 [91] 4557 4604 4627 4642 4669 4762 4800 4896 4955 5050

Add the following code to the above snippet −

x4<-sample(1:100,100) x4_new<-1+cumsum(x4) x4_new Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 75 99 142 182 216 313 343 389 414 480 568 613 698 761 810 [16] 862 929 998 1034 1105 1144 1230 1284 1354 1365 1425 1493 1584 1621 1624 [31] 1682 1747 1754 1815 1836 1862 1935 2023 2046 2110 2120 2142 2241 2254 2330 [46] 2414 2433 2434 2506 2584 2590 2608 2613 2655 2657 2714 2803 2901 2905 2913 [61] 2946 2958 2972 3066 3149 3249 3299 3343 3371 3450 3503 3534 3609 3686 3773 [76] 3829 3891 3911 3962 4052 4132 4173 4208 4256 4273 4354 4370 4402 4494 4549 [91] 4558 4605 4628 4643 4670 4763 4801 4897 4956 5051 Example 5

Following snippet creates a sample data frame −

x5<-round(rnorm(80),2) x5

The following dataframe is created

[1] 1.61 0.31 1.60 0.87 0.60 -0.41 -2.44 1.51 0.24 -0.15 -1.12 0.36 [13] 0.18 -1.09 0.97 -0.15 -0.15 -0.64 1.28 1.61 -0.04 0.62 0.38 1.50 [25] 0.15 2.10 0.32 0.34 0.79 1.11 0.47 1.26 -1.49 -0.13 0.58 0.73 [37] 0.71 -0.16 0.29 -0.63 -0.96 -1.80 0.43 0.21 -0.33 -0.35 -1.64 -0.59 [49] 0.10 -0.12 -0.30 -0.70 0.92 -0.80 -1.71 1.99 -0.79 0.80 -0.13 0.39 [61] 0.19 0.16 0.71 -0.28 -0.93 0.38 2.65 0.43 -1.46 2.49 0.73 -1.38 [73] -0.43 0.40 -0.74 -1.29 -0.35 0.55 -0.33 1.63

Add the following code to the above snippet −

x5<-round(rnorm(80),2) cumsum(x5) Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 1.61 1.92 3.52 4.39 4.99 4.58 2.14 3.65 3.89 3.74 2.62 2.98 [13] 3.16 2.07 3.04 2.89 2.74 2.10 3.38 4.99 4.95 5.57 5.95 7.45 [25] 7.60 9.70 10.02 10.36 11.15 12.26 12.73 13.99 12.50 12.37 12.95 13.68 [37] 14.39 14.23 14.52 13.89 12.93 11.13 11.56 11.77 11.44 11.09 9.45 8.86 [49] 8.96 8.84 8.54 7.84 8.76 7.96 6.25 8.24 7.45 8.25 8.12 8.51 [61] 8.70 8.86 9.57 9.29 8.36 8.74 11.39 11.82 10.36 12.85 13.58 12.20 [73] 11.77 12.17 11.43 10.14 9.79 10.34 10.01 11.64

Add the following code to the above snippet −

x5<-round(rnorm(80),2) x5_new<-1+cumsum(x5) x5_new Output

If you execute all the above given snippets as a single program, it generates the following Output −

[1] 2.61 2.92 4.52 5.39 5.99 5.58 3.14 4.65 4.89 4.74 3.62 3.98 [13] 4.16 3.07 4.04 3.89 3.74 3.10 4.38 5.99 5.95 6.57 6.95 8.45 [25] 8.60 10.70 11.02 11.36 12.15 13.26 13.73 14.99 13.50 13.37 13.95 14.68 [37] 15.39 15.23 15.52 14.89 13.93 12.13 12.56 12.77 12.44 12.09 10.45 9.86 [49] 9.96 9.84 9.54 8.84 9.76 8.96 7.25 9.24 8.45 9.25 9.12 9.51 [61] 9.70 9.86 10.57 10.29 9.36 9.74 12.39 12.82 11.36 13.85 14.58 13.20 [73] 12.77 13.17 12.43 11.14 10.79 11.34 11.01 12.64

Get The Values Of An Object In Javascript

There are some methods for finding the value of an object which is an object.values(), but when you are using this method the process will be lengthy.

We can find easily the value of an object with the help of the _.values() function it is a built-in method that belongs to chúng tôi a javascript library that provides versatile functions.

_.value() this method requires no for loop to execute the values, it is a direct method to find the value of the object.

Syntax _.values( object )

Parameters − This function accepts only one argument which is an object. An object is a like of array which has key-value pair but is not an array.

Example: 1

Following is an example to retrieve values from an object −

var obj = {“name”: ‘Abcd kumar’,age: 47, contact:9893444666, “Organization”:’Spacex’ } var res = JSON.stringify(_.values(obj)); document.write((res));

Example 2

In this example we are printing the object value with the help of the object.value() function.

var userDetails = {name: ‘Aman Kumar’,state:”Jharkhnad”, work:’Technical Writer’, company:”TutorialsPoint” }; for(let value of Object.values(userDetails)){ document.write(” ” + value + ” “); }

Example 3

Let us see another example for this −

var details = _.values({ Name: “Vivek”, Address: “Noida Up”, Mobile: “+91 9876452301″, });

for(let value of details){ document.write(” ” + value + ” “); }

Example 4

Here is another example where we print the contents of an object in an array format −

></script> var details = _.values({ Name: “Vivek”, Address: “Noida”, Mobile: “+91 9876452301”, });

var object = _.values({ name: “Aman Kumar”, Address: “Hyderabad”, Mobile: “+91 8252240532”, });

document.write(“[“); for (let value of details) { document.write(” ” + value + ” “); } document.write(“]”);

document.write(“[“); for (let x of object[2]) { document.write(x); } document.write(“]”);

Last Week In China – Baidu, Blogspot And A China Summer Ends

Last Week in China – Baidu, Blogger and a China Summer Ends

Some of the more noteworthy news tidbits that happened in China last week.

Baidu wins lawsuit over MP3’s

A bit more than a week ago Chinese search engine Baidu won a MP3 copyright infringement case lawsuit, brought to court by 7 of the world’s largest music companies, among them Warner and Sony.

Baidu ‘helps’ its users to find mp3 files by a specialized mp3 search engine. It’s one of the reasons why Baidu is very popular. They help you even to find the top 100 [links to translated version of the page]. Before you can download they show a pro forma screen telling you that Baidu attaches great importance to the protection of intellectual property rights.

From Interfax

The Beijing No.1 Intermediate People’s Court overseeing the case ruled that the accusations brought against the Chinese search giant do not have adequate legal support.

“This is good news for us and it will foster the cooperation between Internet companies and the music industry,” Lesley Zhang, an official with Baidu, said.

The music companies don’t see the verdict as good news and will appeal. The thing is that, unless the central government really wants intellectual property rights to be respected, it will take some time before the mp3 search will be disabled. For Baidu it’s great way to force the music labels to negotiate a deal.

Baidu Bookmarks Social

Baidu enters the Social Bookmarking frenzy.

China Web 2.0 Review writes

The Summer of …

Porn is still a sensitive issue in China, on and offline. A daring entrepreneur stepped in the Chinese online porn wasteland but has to pay for it dearly.

From P2Pnet

He started the Qingseliuyuetian (pornographic summer) website in 2004 and opened a further three porn websites, attracting more than 600,000 users.

He was tracked down after, “a hospital website had changed to become the homepage of a porn website”.

He has been jailed for life.

Google Zeitgeist China September

Google Zeitgeist China has been updated with the September data. Most of the popular searches are, as before, related to entertainment. The seasonal search is for Teacher’s Day.

Blogger unblocked again

Lastly but not least, blogspot blogs can be read again in China. Recently they unblocked Wikipedia and once the world had written about it, they made it unavailable. It’s an ongoing saga, block, unblock and it won’t stop I’m afraid.

Gemme van Hasselt is an Internet Marketing Consultant, living in Shanghai, China. His musings on life can be found on China Snippets

Haskell Program To Create A Function With Arguments And A Return Value

In this article, we are going to understand how to create a function with argument and a return value in Haskell using a user-defined function. The user-defined functions are defined that will contain the function definition with some returning value and is being called by passing desired arguments to it. These functions perform various operations as per the definition.

In all the examples, we are going to define user-defined functions to perform certain tasks that will return some value and are passed with some arguments like, add, mult, maxOfTwo and other functions.

Algorithm

Step 1 − The user defined function is defined by writing its definition with a return value.

Step 2 − Program execution will be started from main function. The main() function has whole control of the program. It is written as main = do. In the main function, the user defined function is being called by passing the desired arguments to it.

Step 3 − The result is printed to the console, after the function is being called.

Example 1

In this example, the add function takes two Int arguments a and b and returns their sum as its output. The main function demonstrates how to use the add function, by passing x and y as arguments and printing the result. The return type of the add function is Int.

add a b = a + b

main :: IO () main = do putStrLn “Sum of two numbers:” let x = 5 let y = 10 let total = add x y putStrLn (show x ++ ” plus ” ++ show y ++ ” is ” ++ show total)

Output Sum of two numbers: 5 plus 10 is 15 Example 2

In this example, the mult function takes two Int arguments a and b and returns their product as its output. The main function demonstrates how to use the mult function, by passing x and y as arguments and printing the result. The return type of the mult function is Int.

mult a b = a * b

main :: IO () main = do putStrLn “Product of two numbers:” let x = 5 let y = 10 let product = mult x y putStrLn (show x ++ ” times ” ++ show y ++ ” is ” ++ show product)

Output Product of two numbers: 5 times 10 is 50 Example 3

In this example, the maxOfTwo function takes two Int arguments a and b and returns the maximum of them as its output. The main function demonstrates how to use the maxOfTwo function, by passing x and y as arguments and printing the result. The return type of the maxOfTwo function is Int.

maxOfTwo a b = max a b

main :: IO () main = do putStrLn “Maximum of two numbers:” let x = 5 let y = 10 let maximum = maxOfTwo x y putStrLn (show x ++ ” and ” ++ show y ++ ” has a maximum of ” ++ show maximum)

Output Maximum of two numbers: 5 and 10 has a maximum of 10 Example 4

In this example, the longestString function takes list of strings as arguments and returns the length of the maximum string as its output. The main function demonstrates how to use the longestString function, by passing list of strings as arguments and printing the result. The return type of the longestString function is Int.

longestString xs = maximum (map length xs)

main :: IO () main = do putStrLn “Length of the longest string in a list:” let xs = [“hello”, “world”, “this”, “is”, “Haskell”] let longest = longestString xs putStrLn (show xs ++ ” has a longest string with a length of ” ++ show longest)

Output Length of the longest string in a list: ["hello","world","this","is","Haskell"] has a longest string with a length of 7 Example 5

In this example, the sumEven function takes list of integers as arguments and returns the length of the sum of even integers as its output. The main function demonstrates how to use the sumEven function, by passing list of integers as arguments and printing the result. The return type of the sumEven function is Int.

sumEven xs = sum (filter even xs)

main :: IO () main = do putStrLn “Sum of all even numbers in a list:” let xs = [1, 2, 3, 4, 5] let evenSum = sumEven xs putStrLn (show xs ++ ” has a sum of even numbers of ” ++ show evenSum)

Output Sum of all even numbers in a list: [1,2,3,4,5] has a sum of even numbers of 6 Conclusion

In Haskell, the user-defined functions are functions that are created by the programmer to perform specific operations. The users can define functions as per their need by passing any desired arguments and returning some value in the function definition. A user-defined function with some arguments and with a return value in Haskell is a function that takes some inputs, and returns a value as its corresponding output.

Update the detailed information about Return The First And Last Values In A Range on the Kientrucdochoi.com website. We hope the article's content will meet your needs, and we will regularly update the information to provide you with the fastest and most accurate information. Have a great day!