Sunteți pe pagina 1din 16

Functions

Functions
Definition: Function is a self contain block of
statements, to perform a desired task.
 Functions are classified into two types

1. System defined functions

ex: printf(),scanf() …..etc

2. User defined functions

ex: a(), display(), sum()….etc


Steps to create user defined functions

 Three steps are required

1. Prototype of function

2. Function call

3. Definition of function (called function)


Prototype of function
• Before the function is defined in the program, it should be declared.

• This declaration should be above the main function

• This prototype provides information to compiler about the function


such as function name, number of arguments and its datatype.
syntax:
returntype function_name(argument list);
Example:
int add(int a, int b);

Note: The prototype of the function is optional in c-language


Function call
• A function can be called on the name of the
function and such statement is called a function
call
syntax:
function_name(argument list);
Example:
add(a, b);
Definition of function (called function)

• It contains the body of the function that performs the operation of function.
Syntax:
returntype function_name(argument list)
{
…………
statements;
…………
}

Example:
int add(int a, int b)
{
…………
}
Basic program on functions
void display( void); //prototype of function
main()
{
printf(“\n This is main function”);
display(); // Function call
}
Void display (void) // definition of function
or called function
{
printf(“\n This is display function”);
}
Basic function designs
• We classify the four basic function designs by
their return values and their parameter lists

1. void functions without parameters


2. void functions with parameters

3.Non-void functions without parameters

4. Non-void functions with parameters


1. Void function with out parameters
void add();
main()
{
add();
}
void add()
{
int a,b,c;
printf(“\n Enter two numbers”);
scanf(“%d%d”, &a,&b);
c=a+b;
printf(“\n addition=%d”, c);
}
2. Void function with parameters
void add(int, int);
main()
{
int a,b;
printf(“\n Enter two numbers”);
scanf(“%d%d”, &a,&b);
add(a,b);
}
void add( int x, int y)
{
int z;
z=x+y;
printf(“\n addition=%d”, z);
}
3. Non void function with out parameters
int add();
main()
{
int m;
m=add();
printf(“\n addition=%d”, m);
}
int add()
{
int a,b,c;
printf(“\n Enter two numbers”);
scanf(“%d%d”, &a,&b);
c=a+b;
return c;
}
4. Non void function with parameters
int add(int, int);
main()
{
int a,b,m;
printf(“\n Enter two numbers”);
scanf(“%d%d”, &a,&b);
m= add(a,b);
printf(“\n addition=%d”, m);
}
int add( int x, int y)
{
int z;
z=x+y;
return z;
}
4-4 Inter-Function Communication

• Although the calling and called functions are two


separate entities, they need to communicate to exchange
data.
• The data flow between the calling and called functions
can be divided into three strategies: a downward flow, an
upward flow, and a bi-directional flow.
FIGURE 4-16 Data Flow Strategies

14
The C language uses only pass by value and return to
achieve three types of communications between
a calling and a called function.

15
Formal and Actual Parameters

• Formal parameters are variables that are declared


in the header of the function definition.

•Actual parameters are the expressions in the


calling statement.

•Formal and actual parameters must match exactly


in
type, order, and number.

•Their names, however, do not need to match.

S-ar putea să vă placă și