Course Content
C Basics
C Basics
Structure of the Function
A function is a named subroutine designed to carry out a specific task. Believe it or not, you engage with functions daily, both mentally and in the world around you.
Consider the simple act of opening a door. Your brain operates on a specific "open the door" routine: the brain signals the hand → the hand muscles contract, turning the doorknob → the hand pushes the door → the door opens.
Every skill or ability a person possesses can be thought of as a function they can "invoke" when needed.
Our daily lives are filled with functions: pressing an elevator button, illuminating a car's speedometer, scanning a product's barcode, processing a signal from a space rover, sending texts, deleting photos from a smartphone — the list goes on.
The Main Function
Here's a surprise: throughout this course, you've been writing code within one significant function in C—the main
function. Why is it that C programs revolve around this single function?
The main
function in C serves as the program's starting point. When you compile and run a C program, the operating system initiates its execution with the main
function, meaning that the code within the main
function gets executed in sequence.
The use of main
as the principal function in C is a longstanding tradition, a convention chosen by the creators of the C language to signify a program's entry point.
Anatomy of Functions
Every function is structured around five core components:
- Function type;
- Function name;
- Arguments;
- Function body;
- Return value.
Note
We'll delve deeper into each component in subsequent lessons.
A general function looks something like this:
Thanks for your feedback!