If you completed any of the String exercises from the past day, you probably already have a handle on what functions are and how they are used. You should skip this lesson and work on a challenge of your choice.
Prerequisites:
You should already have downloaded and installed either the RAD Studio 30 Day FREE Trial, Delphi Community Edition, or C++ Builder Community Edition.
If you did not install the Samples directory during installation, go install them now.
Need help installing the product? Read the documentation or ask for assistance in the forums.
What Are Functions?
Functions help programs complete repetitive tasks. Here are a couple of reasons why functions are important:
- Reusability. Instead of writing the same section of code over and over again, write it once and use it when you need it
- Abstraction. You can use an existing function without really understanding (or caring) what’s going on inside it. In the C++ tutorial below, the presenter mentions that toInt() is also a function… and the sample app you build will use it, but we never go look at what it is doing behind the scenes.
- Readability. Proper use of functions can help other developers (and you) read and understand your code more quickly.
Coding a C++ Function with Windows Apps
This tutorial does a great job walking through a basic function that takes input from the user in a text box, then adds a specific value to it and displays the result. The video itself is dated by several years, but that’s okay because the fundamentals have not changed – a function still acts and behaves the same way. Let’s follow along with the tutorial’s steps using our new 10.3 version of C++Builder.
1. Open C++ Builder
2. Create a new multi-device C++ app
3. On the Form Designer, make sure your View is set to Windows Desktop to most closely align with the tutorial’s screen. Follow along with the video to add 3 buttons, 3 edit fields, and 1 label.
4. Double-click Button1 to open the code view. The tutorial walks through adding code for each button that looks like this:
The point the tutorial makes is that the above code is very repetitive because the buttons all perform the same action: they add 5 to the user’s input and then update the label.
5. The tutorial will walk you through creating a function named SaveTime which performs the math for the button.
Your code will look like this:
int SaveTime(int x) { return(x + 5); } //--------------------------------------------------------------------------- __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner) { } //--------------------------------------------------------------------------- void __fastcall TForm1::Button1Click(TObject *Sender) { Label1->Text = SaveTime(Edit1->Text.ToInt()); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button2Click(TObject *Sender) { Label1->Text = SaveTime(Edit2->Text.ToInt()); } //--------------------------------------------------------------------------- void __fastcall TForm1::Button3Click(TObject *Sender) { Label1->Text = SaveTime(Edit3->Text.ToInt()); } //---------------------------------------------------------------------------
More Practice: C++ Console App Functions
This tutorial has sample code and walks through creating different functions for a console application.
Delphi Functions
Marco Cantu walks through Delphi Functions and Procedures in this FREE Course: https://www.embarcaderoacademy.com/courses/396722/lectures/6067596
Previous #CodingResolution posts:
- Downloading and Installing the FREE C++ Builder Community Edition or Delphi Community Edition
- Getting to Know the IDE
- Programming Language History
- Build Your First Windows App
- Build Your First Mobile App
- Variables and Variable Scope
- Strings and the RegExpDemo Sample