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 — you pass that between the parentheses.
When the program reaches a function call, it executes the function’s instructions and then continues to the next line.
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 perform an action and return nothing.
Common Mistakes#
Forgetting the parentheses
A function call requires parentheses after the name, even when there’s nothing to pass in. Writing Console.WriteLine without () doesn’t call the function — it just references it. The parentheses are what trigger execution.
Confusing a function with its return value
Calling a function and getting its return value are two different things. Console.WriteLine("Hello") runs the function and returns nothing. Console.ReadLine() runs the function and returns a string. Don’t assume every function hands something back.
Passing the wrong type of value Functions expect specific types of input. Passing a number to a function that expects text, or leaving out a required value entirely, will cause an error. Read what a function expects before calling it.