Search⌘ K
AI Features

Defining a Function

Explore how to define and implement functions in C by understanding return types, parameter declarations, and function prototypes. This lesson guides you through writing functions that improve code modularity and efficiency, enabling you to organize your programs clearly and call functions effectively.

Structure of a function

A function is defined in the following way:

C
returnType functionName (param1Type param1Name, param2Type param2Name) {
function_statement1;
function_statement2;
return returnVar;
}

This seems rather abstract, but we will see a concrete example in a moment. Here’s a breakdown:

  • Return type: On the first line, we have to begin by declaring what data type the function will return once it finishes. We can define a function that doesn’t return anything by using the data type void.
...