Definition of function in c msbte | Declaration of function in c msbte | Function call in c msbte
Definition Of Function
A block of code which is intended to perform a specific task is called as function.
Elements of function:-
Elements of function:-
- In C programming a function definition will have following six elements.
- These elements are combined into two parts Functions :
- Function header: Function header is made up of return type, function name, and list of formal parameter enclosed in parentheses (element 1, 2 and 3).
- Function body : Function body consists of local variables declaration statements, function statements, and a return statement (clement 4, 5 and 6).
- Elements of Function
- Return_type
- Function_name
- List of formal parameters
- Function body
- Return_type : A function can or can not return a value. The return type is the data type of the value which the function retums. By default a function returns integer value so the return type is int by default. If a function doesn't return any value, then the return type will be void.
- Function_name : It is compulsory and it indicates the name of the function. Function name is any valid C identifier Function name starts with a letter or underscore and then followed by any combination of letters, digits or underscores. Function name doesn't allow spacebar character.
- List of formal parameters :
- The formal parameter list is optional, it may be present or absent depending on the need of the function.
- Formal parameter list declares the variables which hold the values sent by calling function. The formal parameters in the list are separated by comma and the list is enclosed in parentheses. The list consists of type of the parameters and name of the parameters.
- The function signature is formed by the function name and the list of formal parameters. Here order, type and number of the formal parameter matters.
- The function body include a set of statements that define operation of the function. Function body is enclosed in { and } braces. It consists three parts:
- Declaration of local variables needed by function
- Executable statements that performs task of function.
- Returned value as the output of function.
- Local variables are variables that are defined within function and their life is only till that function and cannot be used outside the functions.
- The return statement returns the expression or the needed. Absence of the return statement in function body indicates that no vale is being returned to the calling function.
- Syntax Of Function :
- return_type Function_name(list of formal parameters)
- {
- local variable declaration;
- statement1;
- statement2;
- ............
- ............
- return statement;
- }
- Example Of Function :
- int div(int a,int b) //Function body
- {
- int result;
- if (b == 0)
- printf("Error! can't divided by zero"); //Function header
- else
- result = a/b;
- return result;
- }
- Here div is the function name. It performs division, div returns an integer value. The result is local variable.The variables a and b are two parameters of div. a and bare called as formal parameters.
- C function can take any number of formal parameters but it can return at most one value per call.
Declaration of Function
- Like variable, before using a function in program it can be declared. The declaration of function is called as function prototyping.
- The function declaration follows following format.
- return_type function_name (list of formal parameters);
- Function declaration also referred as function prototype. It has four parts as listed.
- Return_type
- Function_name
- List of formal parameters
- Semicolon
- These elements are same as the function header in function definition except the semicolon.
- It is possible to have different parameter names in function prototype from the names having in function definition.
- But it is mandatory to match return type in function prototype with return type in function definition.
- Also type, order, and number of the formal parameters of list in function prototype should match with type, order and number of formal parameters in function definition.
- Function declaration is done in global section i.e. after including the header files and above all the functions including main ().
- Globally declared functions are available to all the functions in the program. Also we can declare a function inside a function this is called local declaration so its life is limited to the function in which it is declared. A good programmer place function prototype in global section.
- Example of Function Declaration:
- #include <stdio.h>
- #include<conio.h>
- int div (int a, int b); //Function Prototype
- int main()
- {
- ...........
- ...........
- }
- If return type is absent then it is assumed that the function returns int value. Parameters names are optional in function prototyping. We can declare function div in following way:
- int div (int a, int b);
- or
- div (int a, int b);
- or
- div (int,int );
- These all declarations are valid.
- The prototype of a function which doesn't accept any parameters and doesn't return any value is written as follows:
- void add(void);
- Prototype declaration is not essential but if it is not declared before it is used, then compiler assumes that details of that functions will be available at linking time.
Function Call
- We have learned how to define the function. To use defined function we must call it.
- Syntax:
- Function_name (list of actual parameters);
- Before calling any function it should be either declared or defined first and then function call can be made, otherwise compiler will generate error.
- Calling function and called function
- The function which calls another function is referred as calling function and the function which gets called is referred as called function.
- Actual and formal parameters
- Parameters used in function call are referred as actual parameters whereas parameters used in the function definition and function declaration referred as formal parameters.
- Program:
- #include<stdio.h>
- #include<conio.h>
- void greater(int , int ); //Function Prototype
- int main()
- {
- int x=4 ,y=5;
- greater(x,y); //Function Call
- getch(); //To hold the output screen
- }
- void greater(int a, int b)
- {
- if (a>b)
- {
- printf("%d is greater number", a);
- } //Function Defination
- else
- {
- printf("%d is greater number", b);
- }
- }
- output :
- 5 is greater number
- Explanation:
- In this program main() is the calling function and greater() is called function. While the function greater () gets called by main() further execution of main() is suspended and the control goes to the definition of greater() function and the values of x and y are stored to a and b respectively.
- After finding the greater number the control goes back to the calling function i.e. main () again and continues its further execution.
Post a Comment