From the course: Programming Foundations: Discrete Mathematics
Unlock the full course today
Join today to access over 24,800 courses taught by industry experts.
Recursive functions
From the course: Programming Foundations: Discrete Mathematics
Recursive functions
- [Voiceover] Recursive functions are often used when dealing with strings. Some strings can be various sizes. This makes the process of working with strings more efficient. Let's write an example of a function that capitalizes all letters in a string. To start a function, we type in f-u-n to let sml know that we're writing a function. I'm going to call it capitalize. And this particular function takes in a string. So I need to let the interpreter know that, so I'm going to put double quotes, double quotes. So now it knows to expect a string. This function is going to take that value and if it's empty, it'll just return a string. Next, I tell it what to do if it actually receives something other than an empty string. And what I'm going to do is I'm going to call the capitalize function again. This time I'm going to give it the string s and I'm going to say that that is equal to the string s-t-r, parenthesis, and I'm going to use that character dot toUpper method char.toUpper and I…
Practice while you learn with exercise files
Download the files the instructor uses to teach the course. Follow along and learn by watching, listening and practicing.