Professional Documents
Culture Documents
Topics:-
FUNCTION
S
PRE- USER-
DEFINED DEFINED
User defined functions are written by the user and the user has the
freedom to choose the function name, number and types of arguments
and he will decide whether the function should return any value or not.
1. Predefined Functions(Library Functions)
Examples:
Printf()
Scanf()
Getchar()
Sin()
Sqrt()
Pow()
2. User defined Functions
Example
#include<stdio.h>
int add(int,int); // Function Declaration
void main()
Caller
{ Actual Argument
Function int a=3,b=4,c;
c = add(a,b); // Function Call
printf(“c=%d”,c); Formal/Dummy Argument
}
int add(int p,int q)
{
Called
Function int n;
n=p +q; Function Defination
return(n);
}
Example
Example
Categories of Functions
• Depending upon number of arguments receives and the type of value it
returns.
Categories of Function
#include <stdio.h>
#include <conio.h>
void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
clrscr();
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
getch();
return 0;
}
Output:
Instead of passing the value of a variable, we can pass the memory address of
the variable to the function. This is called as call by reference.
Here, address of the value is passed in the function, so actual and formal
arguments shares the same address space. Hence, value changed inside the
function, is reflected inside as well as outside the function.
• Let's try to understand the concept of call by reference in c language by the
example given below:
#include <stdio.h>
#include <conio.h>
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
(*num) += 100;
printf("After adding value inside function num=%d \n", *num);
}
int main() {
int x=100;
clrscr();
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
getch();
return 0;
}
Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
W.A.P. to swapping of two variable value using call by
reference
#include<stdio.h>
void swapref(int int);
void main()
{
&x &y
intx,y 1000 1002
printf(“enter two number”);
scanf(“%d %d”,&x,&y);
swapref(&x,&y);
}
void swapref(int *a, int *b)
*a *b
{ 10 20
int temp;
temp=a;
a=b;
b=temp;
printf(“%d %d”,a,b);
}
Difference between call by value and call by
reference in c
Call by value Call by reference
(1) A copy of value is passed to the (1) An address of value is passed to
function the function
(2) Changes made inside the (2) Changes made inside the
function is not reflected on other function is reflected outside the
functions function also.
(3) Actual and formal arguments (3) Actual and formal arguments
will be created in different memory will be created in same memory
location. location
Scope of Variable
• The part of program within which a variable can be accessed, is
called it’s SCOPE.
SCOPE
GLOBA
LOCAL
L
1. LOCAL
2. Global
• If a variable is defined outside the function than it can be access by whole
program.
• Variables which are declared out side the function are also known as
External variables.
Difference between Local and Global variables
LIFE TIME
AUTOMATI
STATIC DYNAMIC
C
1. Dynamic
• The memory allocation by the call of malloc is called dynamic
2. Automatic
• Automatic variable’s memory location is created when the block in
which it is declared is entered.
• These variables are exist while the block is active, and then it is
destroyed when the block is exited.
• Local variable is an automatic variable.
3. Static
• The static variable is a variable that exist from the point at which the
program begins execution and continue to exist during duration of the
program.
• storage of static variable is allocated and initialized once when the
execution begins.
• Same as global variables
Explanation of life and scope of variable
• Syntax
factorial of 5 is 120
Advantages of Recursion :
1. It solve problem in easier way than using iterative loops
OUTPUT:
enter the number:10
The value of k is:55
Write a function find out maximum out of three numbers
#include<stdio.h>
void main()
#include<conio.h>
{
intx,y,z;
int largest(inta,intb,int c)
clrscr();
{
largest(x,y,z);
printf("Enter three numbers: ");
getch();
scanf("%d %d %d", &a, &b, &c);
}
if(a>=b && a>=c)
{
OUTPUT:
printf("Largest number = %2d", a);
Enter three numbers: 23 45 12
}
Largest number = 45
else if(b>=a && b>=c)
{
printf("Largest number = %2d", b);
}
else
{
printf("Largest number = %2d", c);
}
}
Write a function prime that return 1 if it‘s argument is
prime and return 0 otherwise.
#include<stdio.h>
int prime(int n)
#include<conio.h>
{
int prime(int);
if(n%i==0)
void main()
{
{
return 0;
intn,p;
}
clrscr();
else
printf("Enter a number : ");
{
scanf("%d",&n);
return 1;
p=prime(n);
}
if(p==1)
{
}
printf("%d is prime\n",n);
Output:
}
Enter a number : 18
else
18 is not prime
{
Enter a number : 5
printf("%d is not
5 is prime
prime\n",n);
}
getch();
}
Write a calculator program(add,subtract,multiply,divide).
Prepare user defined function for each functionality
#include<stdio.h> void add(int a,int b)
#include<conio.h> {
int c;
c=a+b;
void add(inta,int b);
printf("\nAddition =%d",c);
void sub(inta,int b); }
void mul(inta,int b); void sub(int a,int b)
void div(inta,int b); {
void main() int c;
{ c=a-b;
intx,y; printf("\nSubstration =%d",c);
clrscr(); }
void mul(int a,int b)
printf("enter the value:");
{
scanf("%d %d",&x,&y); int c;
add(x,y); c=a*b;
sub(x,y); printf("\nMultiplication =%d",c);
mul(x,y); }
div(x,y); void div(int a,int b)
getch(); { int c;
} c=a/b;
printf("\nDivision =%d",c);
}
Thank You…