Functions
Functions
By
E.Pragnavi
Asst. Prof., MCA,
Dept. of CSE,
UCE (A), O.U
OUTLINE
Introduction to Functions
Properties of Functions
Advantages of using Functions
Types of functions
Intercommunication among Functions
What is a Function?
The main program can consist of a series of function calls rather than
countless lines of code.
Example of a function:
int sum (int x, int y)
{
int result;
result = x + y;
return (result);
}
Function Prototype Declaration
A function will carry out its expected action whenever it is invoked (i.e, called).
The program control passes to that of the called function.
Once the function completes its task, the program control is returned back to the
calling function.
The general form of the function call statement is
function_name(variable1, variable2,..)
or
variable_name= function_name(variable1, variable2,..)
A function will process information passed to it and return a single value.
If there are no arguments to be passed in the function, then the calling statement
would be
function_name();
or
variable_name=function_name();
Information will be passed to the function via special identifiers or expression called
argumensts or actual parameters and returned through return statement.
Example
return statement
There may be more than one return statement in a function but only
one return statement will be executed per calling to the function.
Copies of the data items are passed from the calling function
to the called function
The called function may change the values passed, but the
original values in the calling function remain unchanged.
Example of this type is function with arguments but no
return statement.
Function with arguments and no return
Upward Flow
It occurs when the called function sends data back to the called function without
receiving any data from it.
Example of this type is function with no arguments but return statement
Bi-direction Communication
Given the variables address the called function can then put the data in the calling
function. So the called function needs a special type of variable called pointer
variable, that points to the variable in the called function.
Example: function_name(&variable1, &variable2); // calling function
void function_name(int *variable1, int* variable2); //called function
The called function needs to declare that the parameter is to receive an address, we
use asterisk which signifies that the variables are address variables holding the
address.
Accessing the variable indirectly through its address stored in the parameter list
through * (asterisk) is know as indirection operator.
Example : Function that return multiple values
#include<stdio.h>
#include<conio.h>
void calc(int x, int y, int *add, int *sub)
{
*add = x+y;
*sub = x-y;
}
void main()
{
int a=20, b=11, p,q;
clrscr();
calc(a,b,&p,&q);
printf("Sum = %d, Sub = %d",p,q);
getch();
}
Example for call by reference
Recursive Functions
A recursive function is one that calls itself directly or indirectly to solve
a smaller version of its task until a final call which does not require a
self-call.
The statement that solves the problem is known as base case, the rest of
the function is known as general case.