Course contentsShow
← Computer Science
Lesson 186 of 2,8725. Functions and ModularityFree lesson

Function Declaration Syntax

Learning the basic syntax for declaring functions including return type, name, and parameter list.

Function Declaration Syntax

What you'll learn: How to formally declare a function by specifying its return type, name, and parameters.

The Anatomy of a Function Declaration

You already know what a function is — a reusable block of code that performs a specific task. Now let's learn how to properly declare one so the computer understands what you're creating.

Every function declaration has three essential parts:

1. Return Type

This tells the computer what type of value the function will give back when it finishes. It can be an integer, floating-point number, boolean, character, or even nothing (often called void).

2. Function Name

A descriptive identifier that follows the same naming conventions as variables. Choose names that clearly describe what the function does.

3. Parameter List

The inputs the function needs, enclosed in parentheses. Each parameter has its own type and name. If the function needs no inputs, you use empty parentheses.

The Basic Pattern

Think of a function declaration like a contract or blueprint. Before you build the function's body (what it does), you declare its signature (what it is):

returnType functionName(parameterType1 param1, parameterType2 param2)

For example:

int add(int a, int b)
float calculateAverage(float x, float y)
bool isEven(int number)
void printMessage()

The first declares a function named add that takes two integers and returns an integer. The last declares printMessage that takes nothing and returns nothing.

Key Takeaway: A function declaration specifies the return type first, then the function name, followed by a parenthesized list of typed parameters — this signature tells both the programmer and computer exactly how to use the function.