C Functions
C Functions
What is function?
Example programs.
Introduction
Modular Programming
Divide and Conquer
Break a large problem into smaller pieces
Predefined
Userdefined
Predefined functions
If the functionality of a function is defined by the compiler
those functions are called predefined functions.
Users are free to define their own code for the user-defined
functions. Every user-defined function must have the
following….
Declaring User-defined functions
Syntax
Example
NOTE
Example
Calling User-defined functions
Syntax
Example
Working of Functions
Example Program
void main()
{
int a, b, c;
void addition(int , int);
printf(“We are in main…..\n”);
printf(“Enter any two numbers: ”);
scanf(“%d%d”, &a, &b);
addition(a, b);
printf(“We are again in main…..”);
}
void addition(int x, int y)
{
printf(“SUM = %d\n”, x+y);
}
Important terms in functions
void add( )
Control {
int a,b,c;
printf(Enter any two numbers: );
scanf(“%d%d”, &a, &b);
No return value
c = a + b;
printf(“Result = %d”, c);
}
In this type, there is data transfer from calling to called
function, but not from called to calling function.
void add( )
{
Control int a,b;
printf(Enter any two numbers: );
30 as return value scanf(“%d%d”, &a, &b);
return (a + b);
}
Control
void add( int a, int b)
{
30 as return value return (a + b);
}
Different ways to make a function call
There are THREE ways of making function call…
1. From main function
void function1()
{
function2();
}
void function2()
{
body of the function;
}
3. From same function
When a function calls itself until the last call is invoked till
that time the first call also remains open.