Module 3
Module 3
● Introduction to functions
● Recursive functions
● Function Declaration/prototype
● Function Definition
● Function Calls
Function Declarations
In a function declaration, we must provide the function name, its return type, and the
number and type of its parameters.
Return Type: Usually, the function calculates a value, and return it
back to the main() program. This value is said to be the return value.
We need to declare what type of variable will be returned from the
function. This can be int, double, char, string, and even void if the
function does not return anything.
Function Name: Just like variables, every function has a name. This
has to be included in the function declaration. Note that the function
name should be unique, just like variable names.
Example
int sum(int a, int b);
int sum(int , int);
Function Declarations
A C function is generally defined and declared in a single step because the function
definition always starts with the function declaration so we do not need to declare it
explicitly. Here, return_type refers to the type of variable returned by the function,
function_name refers to the name of the function, and p1 and p2 are the names of
the variables being passed as a parameter.
The below example serves as both a function definition and a declaration.
return_type function_name (parameter_data_type
p1, parameter_data_type p2, ..);
{
// body of the function
}
Function Definition
Function Call
Now that we have declared and defined the function, we can execute the
code written inside it anytime by just one line in C.
function_name(p1, p2);
Here p1 and p2 are the parameters or that were previously mentioned
during the declaration of the function. By just this one line, all the code
inside the function that we defined above will be executed.
Area is 78.550000
Area of Circle Program in C using Function
Example:
int func(parameter_1,parameter_2);
The above function will return an integer value after running statements inside the function.
Note: Only one value can be returned from a C function. To return multiple values, we have to use
pointers or structures.
Function Arguments
Function Arguments (also known as Function Parameters) are the data that is passed to a
function.
Example:
There are four different aspects of function calls, based on whether a function
accepts arguments and/or returns a value. A function may accept no arguments
and return no value, accept no arguments and return a value, accept arguments
and return no value, or accept arguments and return a value namely: