Introduction To Function
Introduction To Function
Function Structure
Function
Function signature Function name Function body
parameters
Function Signature: The function signature includes the return type, the function name, and the
parameters (if any). The return type specifies the type of value the function will return, if any.
The function name is a unique identifier for the function within the program. The parameters
(also called arguments) are values that can be passed to the function for it to work with.
Function Body: This is the block of code enclosed within curly braces {}. It contains the
statements and instructions necessary to perform the desired task. When the function is called,
the code within the function body is executed.
ANATOMY OF A C FUNCTION
1. Function Signature
Return Type: The return type of a function specifies the type of value that the function will
return when it completes its execution. It can be any valid C data type, such as int, float, char,
void, or a user-defined data type. If the function doesn't return a value, you use the void
keyword as the return type.
Function Name: The function name is a unique identifier for the function within your program.
It follows the rules of C identifier naming, meaning it can include letters, digits, and underscores
but must start with a letter or underscore.
Parameters (if any): Parameters, also known as function arguments, are variables or values
that you pass to the function for it to work with. They are enclosed within parentheses ()
following the function name. Parameters allow you to send data to the function, which the
function can then use for its operations. If a function doesn't require any parameters, you leave
the parentheses empty.
ANATOMY OF A C FUNCTION
2. Function Body
The function body is enclosed within curly braces {} and contains the actual code that defines
the function's behavior. It includes a sequence of statements, declarations, and control
structures that perform a specific task or set of tasks.
The function body is where you write the logic of the function, specifying what it should do. It
can use the parameters passed to it, perform calculations, make decisions with control
structures (such as if statements and loops), and return a value using the return statement (if
the function has a non-void return type).
CALLING A FUNCTION
1. Calling a Function:
• To call a function in C, you simply use the function's name followed by a pair of parentheses
(). For example, if you have a function named add, you would call it by writing add().
2. Passing Arguments:
• When calling a function, you may need to pass arguments to it. Arguments are values or
variables that the function expects to receive and use for its operations. These arguments
are enclosed within the parentheses when calling the function.
• The number and type of arguments must match the function's parameter list as defined in
its signature. If a function expects two integer arguments, you should provide two integer
values when calling it.
CALLING A FUNCTION
3. Receiving the Result:
• If a function has a return type other than void, it will produce a result that can be used in
your code. To capture and store this result, you assign it to a variable.
• When you call a function that returns a value, you can use the assignment operator (=) to
assign the function call's result to a variable.
ROLE OF FUNCTIONS
Functions allow you to divide a large and complex program into smaller,
more manageable modules. Each function can encapsulate a specific set of
Modularity
tasks, making the code more organized and understandable. This modular
approach simplifies the development and maintenance of the program.