A function is a named, reusable block of code that performs a specific task. Instead of writing the same instructions over and over, you give them a name and call that name whenever you need them.
Built-in Functions#
Programming languages come with a set of built-in functions that are ready to use. They cover common tasks like displaying text, reading input, or performing calculations. You don’t need to know how they work inside — just how to call them.
For example, every language has a built-in function for printing text to the console:
Console.WriteLine("Welcome to Odds and Evens!");print("Welcome to Odds and Evens!")console.log("Welcome to Odds and Evens!");Calling a Function#
To use a function, you call it by writing its name followed by parentheses. Some functions need information to do their job, which you pass between the parentheses.
When the program reaches a function call, it executes the function’s instructions and then continues to the next line.
Console.WriteLine("Welcome to Odds and Evens!");print("Welcome to Odds and Evens!")console.log("Welcome to Odds and Evens!");Return Values#
Some functions do more than just run — they return a value when they finish. That value is handed back to whoever called the function.
For example, the built-in function for reading input from the console returns whatever the user typed:
string result = Console.ReadLine();result = input()let result = prompt();When the program reaches this line, it calls the function, waits for the user to type something and press Enter, and then stores the returned text in the variable result.
Not all functions return a value, some just performs an action and returns nothing.