What is function call in C
0 179
A function call in C is a way to invoke a function to execute its code from another part of the program.
It consists of the function name followed by parentheses (), optionally containing parameters.
A function call can be divided into two main parts: function definition and function calling.
Types of Functions Call in C
1Built-in Functions:
Provided by the C standard library, such as printf(), scanf().
Built-in functions in C are predefined functions provided by the C standard library to perform common tasks like input/output operations, mathematical calculations, string manipulation, and memory management.
These functions offer a standardized and efficient way to execute operations without writing the code from scratch, enhancing code readability and reducing development time.
Example:
#include<stdio.h>#include<math.h> int main() { double base = 2.0, exponent = 3.0, result; // Using pow() function to calculate power result = pow(base, exponent); printf("Result: %.2lf\n", result); return 0; }
Output:
Result: 8.00
In this example, the pow() function from the C standard library is used to calculate the power of a number.
The base is raised to the exponent, and the result is printed as 8.00.
2User-defined Functions:
Defined by the programmer to perform specific tasks, enhancing code modularity and reusability.
User-defined functions in C are functions that are created and defined by the programmer to perform specific tasks as per the program's requirements.
These functions encapsulate a block of code into a reusable unit, enhancing code modularity, reusability, and maintainability.
User-defined functions allow the programmer to break down complex tasks into smaller, manageable parts, making the code easier to understand and debug.
Function Declaration
A function declaration in C provides the compiler with information about the function's name, return type, and parameters, without specifying the actual function body.
It serves as a prototype or blueprint for the function, allowing the compiler to understand the function's interface and validate its usage throughout the program.
Functions definition
function declaration in C is a statement that informs the compiler about the function's name, return type, and parameters, without providing the actual function body.
It serves as a blueprint or prototype for the function, allowing the compiler to recognize the function and validate its usage before the function is defined or called in the program.
Syntax:
return_type function_name(parameter_type1 parameter_name1, parameter_type2 parameter_name2, ...);
return_type: Specifies the data type of the value returned by the function. It can be void if the function does not return any value.
function_name: Name of the function that uniquely identifies i
parameter_type1, parameter_type2, ...: Data types of the function parameters. If the function has no parameters, the parentheses () are left empty.
parameter_name1, parameter_name2, ...: Names of the function parameters.
Function Calling
Function calling in C refers to the process of invoking or executing a function to perform its defined task.
To call a function, you use the function's name followed by parentheses.
Example:
// Program for Function call in C #include<stdio.h>// Function Declaration void greet(); int main() { // Function Call greet(); return 0; } // Function Definition void greet() { printf("Hello, World!\n"); }
Output:
Hello, World!
In this Example:
Function Declaration:
void greet(); declares a function named greet that takes no parameters and returns void (no return value).
Function Call:
greet(); calls the greet function from within the main() function.
Function Definition:
The greet function definition defines the behavior of the function, which in this case, prints "Hello, World!" to the console.
Share:
Comments
Waiting for your comments