Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Fork of FunctionExample by
main.cpp@1:8a2fb22f43f3, 2017-10-03 (annotated)
- Committer:
- CSTritt
- Date:
- Tue Oct 03 18:05:17 2017 +0000
- Revision:
- 1:8a2fb22f43f3
- Parent:
- 0:27aceb51cedb
- Child:
- 2:c0ac34a64be1
Add forgot final c value display.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
CSTritt | 0:27aceb51cedb | 1 | /* |
CSTritt | 0:27aceb51cedb | 2 | Project: FunctionExample |
CSTritt | 0:27aceb51cedb | 3 | File: main.cpp |
CSTritt | 0:27aceb51cedb | 4 | |
CSTritt | 0:27aceb51cedb | 5 | This simple program demonstrates that C passes arguments by value. |
CSTritt | 0:27aceb51cedb | 6 | See http://www.csie.ntu.edu.tw/~r92094/c++/VT100.html for escape sequences. |
CSTritt | 0:27aceb51cedb | 7 | |
CSTritt | 0:27aceb51cedb | 8 | Written by: Dr. C. S. Tritt; Last revised 10/3/17 (v. 1.0) |
CSTritt | 0:27aceb51cedb | 9 | */ |
CSTritt | 0:27aceb51cedb | 10 | #include "mbed.h" |
CSTritt | 0:27aceb51cedb | 11 | |
CSTritt | 0:27aceb51cedb | 12 | int myFunc(int x, int y); // Function declaration required. |
CSTritt | 0:27aceb51cedb | 13 | |
CSTritt | 0:27aceb51cedb | 14 | int main() { |
CSTritt | 0:27aceb51cedb | 15 | while(true) { |
CSTritt | 1:8a2fb22f43f3 | 16 | const char ESC=27; // Optional - Define escape for escape sequence. |
CSTritt | 1:8a2fb22f43f3 | 17 | printf("%c[2J", ESC); // Optional - VT100 clear screen escape sequence. |
CSTritt | 0:27aceb51cedb | 18 | printf("In function demo main...\n"); |
CSTritt | 0:27aceb51cedb | 19 | int a = 5; // Create and initialize a. |
CSTritt | 0:27aceb51cedb | 20 | printf("a = %d\n", a); // Display a. |
CSTritt | 0:27aceb51cedb | 21 | int b = 6; // Create and initialize b. |
CSTritt | 0:27aceb51cedb | 22 | printf("b = %d\n", b); // Display b. |
CSTritt | 0:27aceb51cedb | 23 | printf("About to call my function.\n"); |
CSTritt | 0:27aceb51cedb | 24 | int c = myFunc(a, b); // Call my function. |
CSTritt | 0:27aceb51cedb | 25 | printf("Function has returned.\n"); |
CSTritt | 0:27aceb51cedb | 26 | printf("a = %d\n", a); // Redisplay a. |
CSTritt | 0:27aceb51cedb | 27 | printf("b = %d\n", b); // Redisplay b. |
CSTritt | 0:27aceb51cedb | 28 | printf("c = %d\n", c); // Display b. |
CSTritt | 0:27aceb51cedb | 29 | wait(2.0f); // Pause for a second. |
CSTritt | 0:27aceb51cedb | 30 | } |
CSTritt | 0:27aceb51cedb | 31 | } |
CSTritt | 0:27aceb51cedb | 32 | |
CSTritt | 0:27aceb51cedb | 33 | int myFunc(int x, int y) { // Function definition. Often in seperate files! |
CSTritt | 0:27aceb51cedb | 34 | int z = x + y; |
CSTritt | 0:27aceb51cedb | 35 | // Changing parameter values here won't change argument values in main! |
CSTritt | 0:27aceb51cedb | 36 | x = 50; |
CSTritt | 0:27aceb51cedb | 37 | printf("x = %d\n", x); // Generally don't put I/O in regular functions. |
CSTritt | 0:27aceb51cedb | 38 | y = 100; |
CSTritt | 0:27aceb51cedb | 39 | printf("y = %d\n", y); // Generally don't put I/O in regular functions. |
CSTritt | 0:27aceb51cedb | 40 | return z; // Explicit return is required in C/C++. |
CSTritt | 0:27aceb51cedb | 41 | } |