Unit III Functions and Pointers
Unit III Functions and Pointers
FUNCTION:
§
Functions are created when the same process or an algorithm to be repeated several times in
various places in the program.
§
Function has a self-contained block of code, that executes certain task. A function has a name,
a list of arguments which it takes when called, and the block of code it executes when called.
§
Functions are two types:
ü
Built-in / Library function.
ex:) printf(), scanf(), getch(), exit(), etc.
ü
User defined function.
User-Defined Functions
Functions defined by the users according to their requirements are called user-defined functions.
These functions are used to break down a large program into small functions.
Advantage of User defined function:
1. Reduce the source code
2. Easy to maintain and modify
3. It can be called anywhere in the program.
Body of user defined function:
return_type f_name (argument1,argument 2)
{
local variables; statements; return_type;
}
The body of user-defined shows that an user-defined functions has following
characteristics:
1. Return type
2. Function name
3. Parameter list of arguments
4. Local variables
5. Statements
6. Return value
S.no C function aspects : syntax
1 function definition
return_type function_name ( arguments list )
{ Body of function; }
2 function call
function_name ( arguments list );
3 function declaration
return_type function_name ( argument list );
Note: The function with return value must be the data type, that is return type and return
value must be of same data type.
User defined function has three parts:
1. Declaration part
ret_type f_name (arguments)
2. Calling part
f_name(arguments);
3. Definition part
Function Parameters:
Parameters provide the data communication between calling function and called function.
Two types:
§
Actual parameters:
These are the parameters transferred from the calling function[main function] to the called
function[user defined function].
§
Formal parameters:
Passing the parameters from the called functions[user defined function] to the calling
functions[main function].
Note:
§
Actual parameter – This is the argument which is used in function call.
Formal parameter – This is the argument which is used in function definition.
}
The above example can be rewritten by creating function int f_mult();
int f_mult(void )
{
int x,y,z; scanf(“%d%d”, &x,&y); z=x* y;
printf(“The result is %d”, z); return(z); }
void main()
{
int c; c=f_mult(); getch();
}
4. Function with return value and with argument
In this prototype, the data is transferred between the calling function and called function.
The called function receives some data from the calling function and sends back a value return to
the calling function.
Syntax:
int f_name (int x, int y); //Function declaration
int f_name (int x, int y) //Function definition //Formal Parameters
{
local variables;
statements;
return int;
}
void main()
{
//variable declaration //Input Statement
ret_value=f_mult(a, b); //Function Call //Actual Parameters
}
The above example can be rewritten by creating function: int f_mult(int x, int y);
int f_mult(int x, int y)
{
int z; z=x* y;
printf(“The result is %d”, z); return(z);
}
void main()
{
int a,b,c;
printf(“Enter any two value:”); scanf(“%d%d”, &a, &b); c=f_mult(a, b);
}
Note:
§
If the return data type of a function is “void”, then, it can‟t return any values to the calling
function.
§
If the return data type of the function is other than void such as “int, float, double etc”,
then, it can return values to the calling function.
Return statement:
It is used to return the information from the function to the calling portion of the
program.
Syntax:
return;
return();
return(constant);
return(variable);
return(exp); return(condn_exp);
By default, all the functions return int data type.
Do you know how many values can be return from C functions?
§
Always, only one value can be returned from a function.
§
If you try to return more than one values from a function, only one value will be returned
that appears at the right most place of the return statement.
§
For example, if you use “return a,b,c” in your function, value for c only will be returned
and values a, b won‟t be returned to the program.
§
In case, if you want to return more than one values, pointers can be used to directly change
the values in address instead of returning those values to the function.
Function Call:
A function can be called by specifying the function_name in the source program with
parameters, if presence within the paranthesis.
Syntax:
Fn_name();
Fn_name(parameters);
Ret_value=Fn_name(parameters);
Example:
#include<stdio.h>
#include<conio.h>
int add(int a, int b); //function declaration
void main()
{
int x,y,z;
printf(“\n Enter the two values:”);
scanf(“%d%d”,&x,&y);
z=add(x,y); //Function call(Actual parameters)
printf(“The sum is .%d”, z);
}
int add(int a, int b) //Function definition(Formal parameters)
{
int c;
c=a+b;
return(c); //return statement
}
2 Parameter Passing Methods/Argument Passing Methods
Call by Value/Pass by Value:
When the value is passed directly to the function it is called call by value. In call by value only
a copy of the variable is only passed so any changes made to the variable does not reflects in the
calling function.
Example:
#include<stdio.h>
#include<conio.h>
swap(int,int); void main()
{
int x,y;
printf("Enter two nos"); scanf("%d %d",&x,&y);
printf("\nBefore swapping : x=%d y=%d",x,y); swap(x,y);
getch();
}
swap(int a,int b)
{
int t; t=a; a=b; b=t;
printf("\nAfter swapping :x=%d y=%d",a,b);
}
System Output:
Enter two nos 12 34
Before swapping :12 34
After swapping : 34 12
Call by Reference/Pass by Reference:
When the address of the value is passed to the function it is called call by reference. In
call by reference since the address of the value is passed any changes made to the value reflects
in the calling function.
Example:
#include<stdio.h>
#include<conio.h> swap(int *, int *); void main()
{
int x,y;
printf("Enter two nos"); scanf("%d %d",&x,&y);
printf("\nBefore swapping:x=%d y=%d",x,y); swap(&x,&y);
printf("\nAfter swapping :x=%d y=%d",x,y); getch();
}
swap(int *a,int *b)
{
int t; t=*a; *a=*b; *b=t;
}
System Output:
Enter two nos 12 34
Before swapping :12 34
After swapping : 34 12
Call by Value:
This is the usual method to call a function in which only the value of the variable is passed as
an argument
Any alternation in the value of the argument passed does not affect the function.
Memory location occupied by formal and actual arguments is different
Since a new location is created, this method is slow
There is no possibility of wrong data manipulation since the arguments are directly used in an
application.
Call by Reference:
In this method, the address of the variable is passed as an argument
Any alternation in the value of the argument passed affects the function.
Memory location occupied by formal and actual arguments is same and there is a saving of
memory location.
Since the existing memory location is used through its address, this method is fast
There is a possibility of wrong data manipulation since the addresses are used in an expression.
A good skill of programming is required here
Library Functions:
C language provides built-in-functions called library function compiler itself evaluates these
functions.
List of Functions:
0.5
Sqrt(x) -- > (x)
Log(x)
Exp(x)
Pow(x,y)
Sin(x)
Cos(x)
Rand(x)-> generating a positive random integer.
3 Recursion in C:
Recursion is calling function by itself again and again until some specified condition has been
satisfied.
Syntax:
int f_name (int x)
{
local variables;
f_name(y); // this is recursion statements;
}
Example_1: Factorial using Recursion
#include<stdio.h>
#include<conio.h>
Int factorial (int n);
void main()
{int res,x;