C user defining function
C defines programming function. In this chapter you will learn how to create a user defined function in C programming.
The function is the block of code that performs a specific task.
C programming allows you to define functions according to your requirement. These functions are known as User Defined Functions. For example:
Suppose, to create a graphics program, requires a square, a circle and a color that depends on the length, radius and color of the user respectively. You can use three functions to solve this problem.
Class function
Circle function
Color function
Examples: User Defined Function
In the example below, two integer number multiplication programs were shown. MultiplyNumbers () defining a user defined function for performing this task.
#include <stdio.h>
int multiplyNumbers (int a, int b); // function prototype
int main ()
{
int num1, num2, multiplication;
printf ("Enters two numbers:");
scanf ("% d% d", & num1, & num2);
multiplication = multiplyNumbers (num1, num2); // function call
printf ("multiplication =% d", multiplication);
return 0;
}
int multiplyNumbers (int a, int b) // function definition
{
int result;
result = a * b;
return result; // return statement
}
Function Prototype
Function prototype usually refers to function declarations, so that the function names, parameters, and return types are defined. The function body does not have function definition.
Function prototype gives advance message to the compiler that this function can be used later on in the program.
Function Prototype Syntax
returnType functionName (type1 argument1, type2 argument2, ...);
Int multiplyNumbers (int a, int b) in the above program; Is a function prototype that gives the compiler the following information:
multiplyNumbers () is the function name
int is the return type of function
The int type two arguments will be passed through the function.
Function prototype is not required when user defining function is prefixed with main ().
Call the function
By making calls, the program's control is transferred to the user defining function.
Function call syntax
functionName (argument1, argument2, ...);
In the above example, multiplyNumbers (n1, n2) from the main () function; The function has been called using the statement.
Function Definition
Function Definition for certain tasks is composed of some code blocks. For example, some code has been used to multiply two numbers between function type return and function definition in the example above.
Function Definition syntax
returnType functionName (type1 argument1, type2 argument2, ...)
{
// function body
}
When the function is called, the program's control function is converted to deflection. The compiler starts executing the codes inside the function body.
Execute arguments via function
The argument in programming means variables that pass through the function. During the function call in the above example, num1 and num2 variables cross between two functions.
Function Definition Parameters accepts two and the passed arguments. These arguments are called formal parameters of function.
Execute the argument through the function
The argument passed through the function and the normal parameter must be either or the compiler will throw error.

If num1 is type of integer then a must be an integer type. If num2 is double type then b variable must be double type.
Function can be called in addition to passing arguments through functions.
Return statement
Return statement function completes the execution and returns the value to the call function. After the return statement, the program's control is transferred to the calling function
In the above example, the result returns the variables' value of the main () function to the multiplication variables.
Function return statement

Return statement syntax
return;
For example
return sum;
return (x + y);
Return value from function and function type of function prototype and function definition must be exactly the same.

0 Comments