Introduction
A function is a block of code that performs a specific task. Every C program has at least one function, which is main().The main () function in C is a starting point of a program.
A function can also be referred as a method or a sub-routine or a procedure.
Advantages of user-defined function
It provides modularity to your program's structure.
It makes your code reusable. You just have to call the function by its name to use it, wherever required.
In case of large programs with thousands of code lines, debugging and editing becomes easier if you use functions.
It makes the program more readable and easy to understand.
Type of Functions
There are two types of function in C programming:
Standard library functions
User-defined functions
Standard library functions
Standard library functions are also known as built-in functions. These functions are defined in header files (files with .h extensions are called header files such as stdio.h), so we just call them whenever there is a need to use them.
For example,
The printf() is a standard library function to send formatted output to the screen (display output on the screen). This function is defined in the stdio.h header file.
Hence, to use the printf() function, we need to include the stdio.h header file using #include <stdio.h>.
The sqrt() function calculates the square root of a number. The function is defined in the math.h header file.
User defined functions
You can also create functions as per your need. Such functions created by the user are known as user-defined functions.
Example: User-defined function
Here is an example to add two integers. To perform this task, we have created an user-defined addNumbers().
#include <stdio.h>
int addNumbers(int a, int b); // function prototype
int main()
{
int n1,n2,sum;
printf("Enters two numbers: ");
scanf("%d %d",&n1,&n2);
sum = addNumbers(n1, n2); // function call
printf("sum = %d",sum);
return 0;
}
int addNumbers(int a, int b) // function definition
{
int result;
result = a+b;
return result; // return statement
}
How to create user-defined functions? :
(Function Declaration, Function Definition, Function Call)
Function Prototype (declaration )
A function prototype is simply the declaration of a function that specifies functions name, parameters and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be used in the program.
Syntax of function prototype
returnType functionName(type1 parameter1, type2 parameter2, ...);
returnType : Return type specifies the type of value(int, float, char, double) that function is expected to return to the program which called the function.
functionName : Function name is an identifier and it specifies the name of the function. The function name is any valid C identifier and therefore must follow the same naming rules like other variables in C language.
parameter list : The parameter list declares the type and number of arguments that the function expects when it is called.
The function prototype is not needed if the user-defined function is defined before the main() function.
Function definition (implementation)
Function definition contains the block of code to perform a specific task.
Syntax of function definition
returnType functionName (type1 parameter1, type2 parameter2, ...) {
// body of the function
}
A function definition in C programming consists of a function header and a function body.
The first line returnType functionName (type1 argument1, type2 argument2, ...) is known as function header and the statement(s) within curly braces is called function body.
Note: While defining a function, there is no semicolon(;) after the parenthesis in the function header, unlike while declaring the function or calling the function.
When a function is called, the control of the program is transferred to the function definition. And, the compiler starts executing the codes inside the body of a function.
Function call ( invocation)
When a program calls a function, the program control is transferred to the called function.
Syntax of function call
functionName(parameter1, parameter2, ...);
A called function performs a defined task and when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program.
Function Arguments
Arguments are the values specified during the function call.
Actual Argument: Arguments which are mentioned in the function call are known as the actual argument.
For example:
func1(12, 23);
here 12 and 23 are actual arguments.
Actual arguments can be constant, variables, expressions etc.
func1(a, b); // here actual arguments are variable
func1(a + b, b + a); // here actual arguments are expression
Formal Arguments: Arguments which are mentioned in the definition of the function is called formal arguments.
Formal arguments are very similar to local variables inside the function. Just like local variables, formal arguments are destroyed when the function ends.
int factorial(int n)
{
// write logic here
}
Here n is the formal argument.
Points to remember about actual and formal arguments-
Order, number, and type of the actual arguments in the function call must match with formal arguments of the function.
If there is a type mismatch between actual and formal arguments then the compiler will try to convert the type of actual arguments to formal arguments if it is legal, otherwise, a garbage value will be passed to the formal argument.
Changes made in the formal argument do not affect the actual arguments.
return Statement
A function may or may not return a result. But if it does, we must use the return statement to output the result.
The return statement terminates the execution of a function and returns a value to the calling function; hence it must be the last statement of any function. If you write any statement after the return statement, it won't be executed.
The program control is transferred to the calling function after the return statement.
The return statement can be used in the following two ways.
return;
return expression;
The first form of the return statement is used to terminate the function and pass the control to the calling function. No value from the called function is returned when this form of the return statement is used.
For example,
return a;
return (a+b);
The datatype of the value returned using the return statement should be same as the return type mentioned at function declaration and definition. If any of it mismatches, you will get a compilation error.
No comments:
Post a Comment