Functions and Pointers
Functions and Pointers
Functions and Pointers
NOTES :
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:
1. Built-in / Library function.
Example: printf(), scanf(), getch(), exit(), etc.
2. User defined function.
3.
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 a small functions.
Note: The function with return value must be the data type, that is return type and return value
must be of same data type.
Two types:
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.
Note: Function with more than one return value will not have return type and return statement in
the function definition.
Syntax:
void f_name(); //function declaration
void f_name (void) //function definition
{
local variables;
statements;
}
void main()
{
f_name(); //function call
Syntax:
void f_name(int x, int y ); //Function declaration
void f_name (int x, int y) //Function Definition //Formal Parameters
{
local variables;
statements;
}
void main()
{
//variable declaration
//input statement
f_name(c, d); //Function call //Actual Parameters
}
Syntax:
int f_name (int x, int y); //Function declaration
int f_name (int x, int y) //Function definition //Formal Parameters
{
local variables;
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.
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
}
System Output:
Enter two nos 12 34
Before swapping :12 34
After swapping : 34 12
Library Functions:
C language provides built-in-functions called library function compiler itself evaluates these
functions.
List of Functions
Sqrt(x) (x)0.5
Log(x)
Exp(x)
Pow(x,y)
Sin(x)
Cos(x)
Rand(x)-> generating a positive random integer.
4.3 Recursion in C:
Recursion is calling function by itself again and again until some specified condition has been
satisfied.
4.4 POINTERS
Definition:
C Pointer is a variable that stores/points the address of the another variable.
C Pointer is used to allocate memory dynamically i.e. at run time.
The variable might be any of the data type such as int, float, char, double, short etc.
Syntax : data_type *var_name;
Example : int *p; char *p;
Where, * is used to denote that “p” is pointer variable and not a normal variable.
Note: It is always a good practice to assign pointer to a variable rather than 0 or NULL.
Pointer Assignments:
We can use a pointer on the right-hand side of an assignment to assign its value to another
variable.
Example:
int main()
{
int var=50;
int *p1, *p2;
p1=&var;
p2=p1;
}
Manipulation of Pointers
We can manipulate a pointer with the indirection operator „*‟, which is known as dereference
operator. With this operator, we can indirectly access the data variable content.
Syntax:
*ptr_var;
Example:
#include<stdio.h>
void main()
{
int a=10, *ptr;
ptr=&a;
printf(”\n The value of a is ”,a);
*ptr=(*ptr)/2;
printf(”The value of a is.”,(*ptr));
}
Output:
The value of a is: 10
The value of a is: 5