搜尋此網誌

2025年8月26日星期二

Writing functions in C++

std::string name = "Alice";

A function prototype in C++ is a declaration of a function that tells the compiler about the function's name, return type, and parameters—without actually defining what the function does.

When we say "a function doesn't return a value" in C++, we mean that the function performs its task but doesn't give any result back to the part of the program that called it. Imagine you ask someone to turn off the lights. They do it, but they don’t hand you anything afterward—they just complete the action. That’s a void function.

string& in C++ means you're working with a reference to a std::string object.

In C++, a function is a reusable block of code that performs a specific task. When you call a function, it can either return a value or simply perform an action without returning anything.

In C++, an identifier is the name used to identify a variable, function, class, array, or any other user-defined element.

Microsoft Copilot

Free computer time

The better I got at coding, the more I wanted to do something real --- to write a program that might truly be useful to someone. It was the same urge I had a few years earlier when I'd realize that no matter how cool a picture I could draw of a bridge or a rocket, I could never build one in the real world. This was different. With a computer, I felt like anything I could image, I could create.

十三歲的比爾・蓋茲在編程方面越進步,就越想做一些真正的事情——寫一個或許能對某人真正有用的程式。他回憶道,這種欲望童年的感覺一樣,那時他意識到,無論小時候能畫出多麼有型的橋樑或火箭,都無法在現實世界中建造它們。但這次不同,有了電腦,他覺得任何他能想像的東西,都能創造出來。

meatloaf: meat, onions, etc. that are cut into very small pieces, mixed together and shaped like a loaf of bread and then baked

trivial: not important or serious; not worth considering

toll: the amount of damage or the number of deaths and injuries that are caused in a particular war, disaster, etc.

envision: to imagine what a situation will be like in the future, especially a situation you intend to work towards

airfield: an area of flat ground where military or private planes can take off and land

artillery: DJ[ɑ:ˋtiləri] large, heavy guns which are often moved on wheels

destroyer: a small fast ship used in war, for example to protect larger ships

offensive: attack

gauge: DJ[geidʒ] to measure something accurately using a special instrument

recuperate: to get back your health, strength or energy after being ill, tired, injured, etc.

fighter: a fast military plane designed to attack other aircraft

infantry: ​soldiers who fight on foot

odds: the degree to which something is likely to happen

peel off: to leave a group of vehicles, aircraft, etc. and turn to one side

curfew: a time when children must be home in the evening

wage: to begin and continue a war, a battle, etc.

demote: to move somebody/something to a lower position or rank, often as a punishment

owe: to have to pay somebody for something that you have already received or return money that you have borrowed

brag: to talk too proudly about something you own or something you have done

hardcore: highly committed in one's support for or dedication to something; denoting an extreme or intense example of something

banish: to order somebody to leave a place, especially a country, as a punishment

bypass: to ignore a rule, an official system or somebody in authority, especially in order to get something done quickly

lenient: not as strict as expected when punishing somebody or when making sure that rules are obeyed

summon: to order somebody to come to you

beard: DJ[biəd]

sternly: in a serious way that often shows that you do not approve of somebody/something; in a way that shows you expect somebody to obey you

berate: to criticize or speak angrily to somebody because you do not approve of something they have done

off-limits (to somebody) (of a place) where people are not allowed to go


Bill Gates "Source Code"

Online Dictionaries Used:

hk.dictionary.search.yahoo.com

www.oxfordlearnersdictionaries.com

www.merriam-webster.com

Translated with the help of AI and edited.

2025年8月21日星期四

Passing values to a function

It’s not that one is universally better --- pointers and references are different tools, each with its own strengths and trade‑offs. The “better” choice depends on your intent and the constraints of your code.

trade-off: the act of balancing two things that are opposed to each other

In C++, “taking an argument” usually means receiving input values into a function so it can work with them.

In C++, the return statement is how a function hands control (and optionally a value) back to its caller. Think of it as the function’s “exit door” — once return runs, the function stops executing immediately.

For void functions, you can:
- Omit return entirely, or
- Use return; to exit early without a value:

In main
- return 0; → program ended successfully
- Non‑zero values (e.g., return 1;) → indicate an error or abnormal termination to the operating system

In C++, the difference between taking arguments by address (pointers) and by reference is as follows:

By Address (Pointers):

You pass the memory address of the variable.
Syntax: Use an asterisk * in the function parameter and an ampersand & when calling the function.
Example: void swap(int *x, int *y) and called with swap(&a, &b).
Requires dereferencing inside the function using the * operator.

By Reference:

You pass the variable itself, but it acts as an alias to the original variable.
Syntax: Use an ampersand & in the function parameter.
Example: void swap(int &x, int &y) and called with swap(a, b).
No need for dereferencing inside the function.

Using references is generally safer and simpler because it avoids the need for explicit pointer manipulation.

Mainly by Microsoft Copilot

2025年8月19日星期二

fundamental code

A converted car dealership refers to a former car dealership building that has been repurposed for a different use.

pricey: expensive

Smashed Piñata Cakes only uses the highest-quality ingredients.

creep: to move with your body close to the ground; to move slowly on your hands and knees

sneak: to go somewhere secretly, trying to avoid being seen

scenic: having beautiful natural scenery

détente: an improvement in the relationship between two or more countries which have been unfriendly towards each other in the past

lenient: not as strict as expected when punishing somebody or when making sure that rules are obeyed

grateful: feeling or showing thanks because somebody has done something kind for you or has done as you asked

overstate: to say something in a way that makes it seem more important than it really is

sophomore: DJ[ˋsɔfəmɔ:]

A wartime decoy is a military tactic using deceptive devices to mislead the enemy about the location or strength of forces and equipment.

undervalue: to not recognize how good, valuable or important somebody/something really is

"Outliers: The Story of Success" is a non-fiction book written by Canadian writer Malcolm Gladwell.

deliberate: done on purpose rather than by accident

underwhelming: ​not impressing or exciting you at all

wield something: to have and use power, authority, etc.

mint: to make a coin from metal

slot machine: a machine that you put coins into to play a game in which you win money if particular pictures appear together on the screen; a similar game, played online

payout: a large amount of money that is given to somebody

aforementioned: mentioned before, in an earlier sentence

if there is a scarcity of something, there is not enough of it and it is difficult to obtain it

handful: a small number of people or things

snippet: a small piece of information or news

tantalizing: making you want something that you cannot have or do

stumble upon: to discover something/somebody unexpectedly

workaround: a way of working, especially with a piece of software, that avoids a particular problem but does not actually solve the problem

scrap: a small piece of something, especially paper, cloth, etc.

crumple: to press or crush something into folds; to become pressed, etc. into folds

dumpster: a large open container for putting old bricks, rubbish, etc. in. The Dumpster is then loaded on a lorry and taken away.

dregs: the last drops of a liquid, mixed with little pieces of solid material that are left in the bottom of a container

Polystyrene foam cups are often referred to as Styrofoam.

nimble: able to move quickly and easily

quarry: a place where large amounts of stone, etc. are dug out of the ground

terse: using few words and often not seeming polite or friendly

off-limits (to somebody) (of a place) where people are not allowed to go

laborious: taking a lot of time and effort

explicitly: clearly or directly, so that the meaning is easy to understand

novice: a person who is new and has little experience in a skill, job or situation

impenetrable: impossible to understand

Spacewar! is a space combat video game developed in 1962 by Steve Russell in collaboration with Martin Graetz, Wayne Wiitanen, Bob Saunders, Steve Piner, and others.

torpedo: a long, narrow bomb that is fired under the water from a ship or submarine and that explodes when it hits a ship, etc.

decipher: decipher something to convert something written in code into normal language

sprawl: to sit, lie or fall with your arms and legs spread out in a relaxed or careless way

Bill Gates "Source Code"

Online Dictionaries Used:

hk.dictionary.search.yahoo.com

www.oxfordlearnersdictionaries.com

2025年8月17日星期日

Functions in C++

Modularity refers to the degree to which a system's components can be separated and recombined, often for increased flexibility and variety of use. It's a concept that can be applied across various disciplines, including engineering, computer science, and biology.

A simple example of a function in C++ that returns a value:

#include <iostream>

using namespace std;

int multiply(int x, int y) {

    return x * y;

}

int main() {

    int result = multiply(4, 5);

    cout << "The result is: " << result << endl;  // Output: The result is: 20

    return 0;

}

A function is a reusable block of code that performs a specific task. It can take inputs (parameters), do something, and optionally return a value.

Parameter: a variable in the function definition that receives a value

Argument: the actual value passed to the function when it is called

Use const in parameters when you don’t want them modified

A global function is defined outside of any class and can be accessed from anywhere in the file (or other files if declared properly).

Functions defined inside a class are called member functions. They operate on the data members of the class and can be either public or private.

In C++, a function signature is the part of a function declaration that includes the function name and the parameter types (but not the return type or parameter names).

In C++, the return type of a function specifies the type of value that the function will send back to the caller when it finishes executing.

What Is a Return Type?
It’s the data type declared before the function name that tells the compiler what kind of result to expect.

A function declaration tells the compiler, “This function exists, and here’s how it should be called.”

It does not contain the body of the function—just the signature and return type.

A function definition provides the actual implementation of a function. It includes:
- The return type
- The function name
- The parameter list
- The body (code that runs when the function is called)

A function prototype is a declaration that tells the compiler, “This function exists, and here’s how it should be called.”

It’s usually placed before main() or in a header file so the compiler knows about the function before it’s used.

Microsoft Copilot

2025年8月12日星期二

Quiz

In C++, a struct (short for "structure") is a user-defined data type that groups related variables under one name. It’s similar to a class, but by default, its members are public rather than private.

In C++, double is a primitive data type used to store floating-point numbers with double precision. That means it can represent decimal numbers with more digits and greater range than a float.

The line for (Resource& r : resources) is a range-based for loop in C++, and it means:
“For each Resource object r in the container resources, do something.”

The switch statement in C++ is a control structure used to execute different blocks of code based on the value of a variable—typically an int, char, or enum. It’s a cleaner alternative to multiple if-else statements when you're checking the same variable against many values.

for (declaration : container) {
    // loop body
}

Microsoft Copilot

Ranged for loops

traverse: cross

for (initialization; condition; update) {

    // Code to execute repeatedly

}


average += laptimes[i];

average = average + laptimes[i];


for (auto x : laptimes)

“For each element x in the container laptimes, do something with x.”
  • auto: Automatically deduces the type of x (e.g., double if laptimes is a vector)
  • x: A copy of each element in laptimes
  • :: Reads as “in” — like “for each x in laptimes


Microsoft Copilot