L5 Function
L5 Function
It is easy to locate and isolate a faulty function for further investigations.
3
THE FORM OF C FUNCTIONS
All functions have the form
4
RETURN VALUES AND TYPES
The return statement can take the form:
return
or
return(expression);
Eg:if(x <= 0)
return(0);
else
return(1);
5
do nothing function
do_nothing() { }
6
CALLING A FUNCTION
A function can be called by simply using the function name in the statement.
Eg:main()
{
int p;
p = multi(10,5);
printf(“%d \n”, p);
}
When the compiler executes a function call, the control is transferred to the
function multi(x,y).
7
Function Example
8
Function Declarations
9
Function Prototyping
When a program calls a function, the program control is transferred to the
called function. A called function performs a defined task and returns the
program control back to the main program.
10
Passing Function Arguments/Parameters
11
Call by Value
12
Call by Value continue…
13
Call by Reference
14
Call by Reference Example
15
CATEGORY OF FUNCTIONS
A function may belong to one of the following categories.
1) Functions with no arguments and no return values.
2) Functions with arguments and no return values.
3) Functions with arguments and return values.
A function does not receive any data from the calling function. Similarly, it
does not return any value.
16
Function with no argument and no
return value
ARGUMENTS BUT NO RETURN VALUES
The nature of data communication between the calling function and the
called function with arguments but no return values is shown in the
diagram.
18
Function with argument and no
return value
ARGUMENTS WITH RETURN VALUES
Here there is a two-way data communication between the calling and the
called function.
20
Function with argument and return
value
RECURSION
When a function in turn calls another function a process of ‘chaining’ occurs.
Recursion is a special case of this process, where a function calls
itself.
Eg:1)
main()
{
printf(“Example for recursion”);
main();
}
22
RECURSION function example 1
unsigned int gcd(unsigned int a, unsigned int b)
{
if(b==0)
return a;
else
return gcd(b, a%b);
}
int main(){
int a,b;
scanf(“%d%d”,&a,&b);
printf(“%d”,gcd(a,b));
return 0;
}
23
RECURSION function example 2
int factorial(int n){
int fact;
if(n==1)
return 1;
else
fact= n * factorial(n-1);
return (fact);
}
int main(){
int a;
scanf(“%d”,&a);
printf(“%d”,factorial(a)); return 0;
}
24
FUNCTIONS WITH ARRAYS
Example:
main () {
int a[10], int n;
----------------
----------------
array_pass(a, n); 25
}
Scope and Lifetime of variables in function
26
Four Storage classes
27
Automatic variables or Local variable
28
External variables or global variables
29
Static variable
In C programming, when static variable is used in a program, it
causes only one copy that is to be shared by all functions.
30
Register variable
1. register keyword is used to define local variable.
2. Local variable are stored in register instead of RAM.
3. As variable is stored in register, the Maximum size of variable = Maximum
Size of Register
4. This is generally used for faster access.
5. Common use is “Counter“
31
Why we need Register Variable ?
32
Thank you
33