Pps Unit-4 Functions
Pps Unit-4 Functions
unctions Definitions
Declaration
Types of Functions
Parameter passing
Recursion
assing Arrays to functions
ibrary functions PRESENTED BY
M.YUGANDHAR
unctions and pointers
CSE DEPARTMENT
Storage classes AITAM
Functions
Definition :
• Function is a group of statements that together perform a task.
• Every C program has at least one function, which is main( ), and all the complex programs
can define additional functions.
• You can divide up your code into separate functions. How you divide up your code among
different functions is up to you.
#include <stdio.h>
int main()
{
printf("%d", sqrt(4) );
return 0;
}
•If you try to use sqrt( ) without including the math.h header file, you will get an
error.
2.User defined functions
•You can also create functions as per your need. Such functions created by the user are
known as user-defined functions.
•We need to declare a variable, before going to access it. As usual, before going to access a
function we should do the following:
i.Function declaration
ii.Function calling
iii.Function definition (Function implementation)
a)Declaration of Function
•A function declaration tells the compiler about a function's name, return type, and
parameters and how to call the function.
•Functions should declare at declaration part. i.e. after header files before main( ).
Example: int max(int num1, int num2); (or) int max (int, int);
Note: Parameter names are not important in function declaration. only their type is
required.
b)Defining a Function
•A function definition provides the actual body of the function.
•The general form of a function definition in C programming language is as follows:
void main( ) return_type function_name ( parameter list ) int sum( int a, int b)
{ { {
… body of the function return a+b;
} } }
•A function may return a value. The return_type is the data type of the value the function
returns.
•Some functions perform the desired operations without returning a value. In this case,
the return_type is the keyword void.
Function Name:
Function Body:
•The function body contains a collection of statements that define what the function
does.
c)Calling a Function
•To use a function, you have to call that function to perform the task.
• A called function performs task and it returns back to the main program.
•To call a function, you simply need to pass the required parameters along with
function name.
•Ex: return 10 ;
3) return expression: Syntax:
•The call by value method ,we are passing duplicate copies from calling function to called
function.
•In this case, any changes made inside the called function have no effect on the
argument in calling function.
Let us call the function swap() by passing actual values as in the following example:
#include <stdio.h>
void swap(int, int); /* function declaration */
int main( )
{
int a = 100, b = 200; /* local variable definition */
printf("Before swap, value of a : %d, b: %d\n", a,b );
swap(a, b); /* calling a function to swap the values
*/
printf("After swap, value of a : %d, b: %d\n", a,b );
return 0;
}
void swap( int x, int y )
{
int temp;
temp = x;
x = y;
y = temp;
}
Output:
Before swap, value of a :100, b :200
After swap, value of a :100, b :200
call by reference
•The call by reference method, we are passing address from calling function to called
function.
•Inside the function, the address is used to access the argument/parameters.
•This means that changes made in the called function will affect the calling function.
Let us call the function swap() by passing values by reference as in the following example:
#include <stdio.h>
void swap(int *x, int *y); /* function declaration */
int main( )
{
int a = 100, b = 200; /* local variable definition
*/
printf("Before swap, value of a : %d, b : %d\n", a,b );
swap(&a, &b); /* addresses of a and b */
printf("After swap, value of a : %d, b : %d\n", a,b );
return 0;
}
void swap(int *x, int *y)
{
int temp;
temp = *x;
*x = *y;
*y = temp;
}
Output:
Before swap, value of a :100, b : 200
Recursion
•When we are combining these two cases, we must pay careful attention to the logic. When
the base case reached, it must execute a return.
•The recursive solution for a problem involves a two way journey.
•First we decompose the problem from top to bottom, and then we solve it from the
bottom to top.
•The decomposition of factorial (3) is as follows:
int factorial(int n)
{
if(n==0 || n==1) // base case: gives solution some part of the problem
return n;
else
return n*factorial(n-1) ; // general case: minimizing the code
}
Non recursive program for factorial Recursive program for factorial
#include<stdio.h> #include<stdio.h>
int factorial (int); int fact(int);
void main() main()
{ {
int a , fact; int result, n;
clrscr(); clrscr();
printf("Enter a Integer value: "); printf("Enter a Integer value: ");
scanf("%d", &a); scanf("%d", &n);
fact = factorial(a); result = fact (n);
printf("Factorial value = %d", fact); printf("Factorial value = %d", result);
getch(); getch();
} }
int factorial (int x) int fact(int f)
{ {
int f=1, i; if(f==0)
for(i=x; i>=1; i--) return 1;
f = f * i; else
return (f); return f * fact(f-1);
} }
Passing Arrays to functions
•If you want to pass a single-dimension array as an argument in a function you may use
the following three ways
Way -1 Way-2 Way-3
Formal parameters as a sized Formal parameters as an Formal parameters as a pointer
array unsized array
#include<stdio.h> #include<stdio.h> #include<stdio.h>
void sum( int [] ); void sum( int [] ); void sum( int * );
void main() void main() void main()
{ { {
int a[10]; int a[10]; int a[10];
sum(a); sum(a); sum(&a);
} } }
void sum ( int a[10] ) void sum( int a[] ) void sum(int *b)
{ { {
… … …
} } }
Storage Classes in C
A storage class is used to describe the following things about the variable:
The variable scope.
The location where the variable will be stored in a computer.
The default initial value of a variable.
A lifetime of a variable.
Storage Classes in C
Storage Classes in C
ii)Lifetime:
•Lifetime of a variable is the time period for which the value of the variable is valid.
•The lifetime of a variable is the time duration for which memory is allocated to store it,
and when that memory is released.
•Simply, how long a value of a variable is existed in compute memory during program
execution.
Storage Classes in C
1.Automatic storage class
•A variable declared inside a function without any storage class specification, is by default
an automatic variable
•. They are created when a function is called and are destroyed automatically when the
function's execution is completed.
•Automatic variables can also be called local variables because they are local to a function
Syntax
extern data type variable name =
value;
2.External storage class
Example for Global variable:
#include<stdio.h>
int num; // global variable
void main()
{
num = 10;
printf("in main function num value is %d \n", num);
fun1();
fun2();
}
Void fun1()
{
num = 20;
printf(“ \n In fun1 num value is %d ", num);
}
voidfun2()
{
printf("\n In fun2 num value is %d", num);
}
Output:
In main function num value is 10
In fun1 num value is 20
In fun2 num value is 20
2.External storage class
•The extern keyword is used with a variable to inform the compiler that this variable is
declared somewhere else.
Example 1: Example 2:
void main( ) void fun( ); function declaration
{ extern int i = 1; global variable
int x = 3; declaration
extern int y ; void main( )
printf(" x = %d ", x); 3 {
printf(" y = %d ", y); 10 int i = 3;
} printf(" %d " , i); 3
y = 10 ; fun( );
}
void fun( )
{
printf(" %d ", i ); 1
}
2.External storage class
Syntax
static data type variable name =
value;
3.Static storage class
Using auto keyword Using static keyword
#include<stdio.h> #include<stdio.h>
void test(); void test();
void main() void main()
{ {
test(); test();
test(); test();
test(); test();
} }
void test() void test()
{ {
auto int a = 0; static int a = 0;
a = a + 1; a = a + 1;
printf("%d ",a); printf("%d ",a);
} }
Output: 1 1 1 Output: 1 2 3
3.Static storage class
Syntax
register data type variable name =
value;
4.Register variable
4.Register variable
: Garbage
: Local to
ogram
: Within the
Storage Classes in C