Function in C: Presentation On
Function in C: Presentation On
FUNCTION IN C
• Syntax:
Return_type function_name(parameter);
EXAMPLE: void sum(int, int);
Function call
• This tells compiler when to execute function definition.
• When a function call is executed, the control jumps to the function definition
where actual code gets executed and returns to same function call once
completed.
• Syntax:
Function_name(parameter);
EXAMPLE: sum(x , y);
Function definition
• This provides the actual code of the function and also known as body of the
function.
• The actual task of the function is implemented in function definition.
• The code is to be written in these braces {}.
• Syntax:
Return_type function_name(parameter)
{
statement 1;
statement 2;
………….
}
Parameter
Parameter are those data values that are passes from calling function to called
function.
When function gets executed in program, control is transferred from calling
function to called function. It may carry one or more value. These values are
called parameter.
These are of two types: ACTUAL and FORMAL.
Actual parameter are specified in calling function and
Formal parameter are specified in called function.
When function gets executed, actual parameter values are copied into formal
parameter. (starts from first argument).
Both must be same in number, data type and order.
Return statement
And while coming back to called function, it may carry a single value called return
value.
Syntax:
return value or variable;
It serves two purposes:
i. On executing return statement, it immediately transfer control back to calling function.
ii. It returns value to calling function.
iii. A function return only one value at a time.
• #include<stdio.h> //header files.
EXAMPLE • #include<conio.h>
• void sum(int , int); //prototype declaration.
• void main()
• {
Return type void • int a,b;
• clrscr();
• printf("\nEnter the two integers:\n");
• scanf("%d%d",&a,&b);
• sum(a,b); //function call or calling function.
ACTUAL PARAMETER • getch();
• }
• void sum(int x, int y)
• {
FORMAL PARAMETER
• int sum; //function body or definition.
• sum=x + y;
• printf("\nSUM = %d",sum);
• }
•
Function Categories
1) Function with no parameter and no return type.
2) Function with parameter and no return type.
3) Function with no parameter and a return type.
4) Function with parameter and a return type.
Function with no parameter and no return type
OUTPUT:
Function with parameter and no return type
OUTPUT:
Enter radius:
3
OUTPUT:
Enter radius:
3
Sum = 130