3.6 Passing arrays and strings to functions
Passing Arrays to Functions (or) Passing Array as an Argument to a Function in C : -
- When we pass an array to a function, that means we are passing the address of the first element of the array, not a copy of the entire array.
- When you pass an array to a function, we actually pass the base address (memory address of the first element) of the array, not a copy of all its elements. This means any change made to the array inside the function will affect the original array.
Syntax: -
Example: -
Example: -
Output: -
Passing Strings to Functions: -
- When we pass a string to a function, we are actually passing the address of the first element of the array.
- This is why strings in C are considered to be passed by reference in effect, even though C does not have true pass-by-reference.
Example: -
Output: -
Inline functions: -
- An inline function is a function for which the compiler attempts to insert the function’s code directly at the point of each call rather than performing a standard function call.
- This helps reduce function call overhead and can improve performance for small, frequently called functions.
Syntax: -
Example: -
Output: -
- Instead of performing a normal function call, the compiler may replace square(a) with a * a in the compiled code.
- This avoids the overhead of a function call.
- static restricts the function to the current file, so the linker does not need an external reference.