Professional Documents
Culture Documents
Chap8 C Function Feclaration and Definition
Chap8 C Function Feclaration and Definition
You just learned from the previous chapters that you can create and call a function in the
following way:
Example
// Create a function
void myFunction() {
printf("I just got executed!");
}
int main() {
myFunction(); // call the function
return 0;
}
Declaration: the function's name, return type, and parameters (if any)
Definition: the body of the function (code to be executed)
For code optimization, it is recommended to separate the declaration and the definition of the
function.
You will often see C programs that have function declaration above main(), and function
definition below main(). This will make the code better organized and easier to read:
Example
// Function declaration
void myFunction();
1
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
// Function definition
void myFunction() {
printf("I just got executed!");
}
Another Example
If we use the example from the previous chapter regarding function parameters and return
values:
Example
int myFunction(int x, int y) {
return x + y;
}
int main() {
int result = myFunction(5, 3);
printf("Result is = %d", result);
return 0;
}
// Outputs 8 (5 + 3)
Example
// Function declaration
int myFunction(int, int);
// Function definition
int myFunction(int x, int y) {
return x + y;
}
2
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
2-Types of Functions
1. Library Functions
3
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
1. Library Function
exists that contains these functions, each of which has a specific meaning and is included
in the package. Built-in functions have the advantage of being directly usable without
being defined, whereas user-defined functions must be declared and defined before being
used.
For Example:
1. C Library functions are easy to use and optimized for better performance.
Functions that the programmer creates are known as User-Defined functions or “tailor-
made functions”. User-defined functions can be improved and modified according to the
need of the programmer. Whenever we write a function that is case-specific and is not
defined in any header file, we need to declare and define our own functions according to
the syntax.
4
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
Example:
The data passed when the function is being invoked is known as the Actual parameters. In
the below program, 10 and 30 are known as actual parameters. Formal Parameters are
the variable and the data type as mentioned in the function declaration. In the below
5
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
1. Pass by Value
2. Pass by Reference
1. Pass by Value
Parameter passing in this method copies values from actual parameters into formal
function parameters. As a result, any changes made inside the functions do not reflect in
6
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
Example:
Output
2. Pass by Reference
The caller’s actual parameters and the function’s actual parameters refer to the same
locations, so any changes made inside the function are reflected in the caller’s actual
parameters.
7
Algorithmic and DYNAMIC data structure Chapter 8: C Function Declaration and Definition
Example:
Output
Advantages of Functions in C
1. The function can reduce the repetition of the same statements in the program.
3. There is no fixed number of calling functions it can be called as many times as you want.
5. Once the function is declared you can just use it without thinking about the internal
working of the function.