New Task Structure
So far, you've been writing all of your code inside the main function. But in C++, you can create your own functions. You'll explore them in more detail later β right now, let's focus on the basics so you can start using them in upcoming exercises.
Think of a function as a box that holds a piece of code. Whenever you need that code, you simply "open the box" by calling the function. If you don't call it, the code inside won't run.
Here's a simple example:
main.cpp
1234567891011#include <iostream> // This function takes a string parameter and prints a greeting message void sayHello(std::string name) { std::cout << "Hello, " << name << "!"; } // this function doesn't return any value int main() { sayHello("Alice"); // calling your function with a parameter return 0; // main always returns 0 }
In this program, there are two functions. The first is the familiar main
, and the second is your custom function sayHello
.
Notice the return 0;
at the end of main
. This means the function returns the number 0. Later, you'll learn that functions can return other types of values as well.
The sayHello
function now takes a parameter called name
. This means that when you call the function, you pass a value inside the parentheses β for example, "Alice".
By calling sayHello("Alice")
inside main
, you run the function and see the output "Hello, Alice!".
Try removing the call to the sayHello
function from main
and you'll see that the code inside sayHello
doesn't run.
Function Parameters
In C++, a function can have more than one parameter. You separate them with commas in the function definition. Each parameter acts like a variable that holds a value passed into the function when it is called.
For example:
main.cpp
1234567891011121314#include <iostream> // This function prints a greeting with a name and an age void greet(std::string name, int age) { std::cout << "Hello, " << name << "! You are " << age << " years old."; } int main() { greet("Alice", 25); std::cout << std::endl; greet("Bob", 30); return 0; }
The function greet
has two parameters, name
and age
. When you call greet("Alice", 25)
, the value "Alice" is assigned to name
and 25 to age
. Inside the function, you can use these variables just like any other to customize the message. The output changes depending on the values you pass, making the function flexible and reusable for different inputs.
The key thing to remember: most of the time, you'll write your code inside a prepared function like sayHello
, while the main function will already be pre-filled.
1. What happens if you don't call a function in your program?
2. What does return 0;
in the main
function mean?
3. How do you call a function named sayHello
?
Thanks for your feedback!
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat
Awesome!
Completion rate improved to 3.85
New Task Structure
Swipe to show menu
So far, you've been writing all of your code inside the main function. But in C++, you can create your own functions. You'll explore them in more detail later β right now, let's focus on the basics so you can start using them in upcoming exercises.
Think of a function as a box that holds a piece of code. Whenever you need that code, you simply "open the box" by calling the function. If you don't call it, the code inside won't run.
Here's a simple example:
main.cpp
1234567891011#include <iostream> // This function takes a string parameter and prints a greeting message void sayHello(std::string name) { std::cout << "Hello, " << name << "!"; } // this function doesn't return any value int main() { sayHello("Alice"); // calling your function with a parameter return 0; // main always returns 0 }
In this program, there are two functions. The first is the familiar main
, and the second is your custom function sayHello
.
Notice the return 0;
at the end of main
. This means the function returns the number 0. Later, you'll learn that functions can return other types of values as well.
The sayHello
function now takes a parameter called name
. This means that when you call the function, you pass a value inside the parentheses β for example, "Alice".
By calling sayHello("Alice")
inside main
, you run the function and see the output "Hello, Alice!".
Try removing the call to the sayHello
function from main
and you'll see that the code inside sayHello
doesn't run.
Function Parameters
In C++, a function can have more than one parameter. You separate them with commas in the function definition. Each parameter acts like a variable that holds a value passed into the function when it is called.
For example:
main.cpp
1234567891011121314#include <iostream> // This function prints a greeting with a name and an age void greet(std::string name, int age) { std::cout << "Hello, " << name << "! You are " << age << " years old."; } int main() { greet("Alice", 25); std::cout << std::endl; greet("Bob", 30); return 0; }
The function greet
has two parameters, name
and age
. When you call greet("Alice", 25)
, the value "Alice" is assigned to name
and 25 to age
. Inside the function, you can use these variables just like any other to customize the message. The output changes depending on the values you pass, making the function flexible and reusable for different inputs.
The key thing to remember: most of the time, you'll write your code inside a prepared function like sayHello
, while the main function will already be pre-filled.
1. What happens if you don't call a function in your program?
2. What does return 0;
in the main
function mean?
3. How do you call a function named sayHello
?
Thanks for your feedback!