SlideShare a Scribd company logo
1 of 16
Download to read offline
!!!!
""""
Function
Function is self-contained block of program that perform specific
task and return certain values.
Syntax: <return type><function name> (argument list);
- Return type is either void or int types
Every “C” program start with main () function which is user defined
functions.
- C language supports two types of functions.
- I) Library function
- II) User defined functions
Library Function: these are pre-defined functions in different
header file.
e.g: printf (),scanf(),gets(),sqrt(),pow() etc.
e.g:
#include<stdio.h>
#include<math.h>
Void main ()
{
int no, r;
printf (“Enter a number”);
scanf (“%d”,&no);
r=sqrt(no);
printf(“%d”,r);
}
User defined function:
They are defined by the user according user requirement. A user
defined function has three things.
i) Function prototype
ii) Function call
iii) Function definition
!!!!
""""
Why use function: if we want to perform a task repeatedly then it
is not necessary to re-write the particular block of the program
again and again.
-Shift the particular block of statement in a user defined
function. the function defined can be used for any number of times
to perform the task.
- Use the function can be reduced to smaller one.
- It is easy to debug and find out the error.
- It also increases readability.
How it works: whenever a function is called control passes to the
called function and working of the calling function is stopped.
- When the execution of function is completed, control returns
to the calling function and execute the next statement.
- The value of actual argument passed by the calling function
and received by the formal argument of the called function.
The no of actual argument and formal argument are same.
- The function operates on formal argument and sends back the
result to the calling functions, the return () statement
performs this task.
Syntax to Declare the Calling Function:
<return type> <function name> (argument list with its data type);
Argumnent –declaration;
{
Statment1;
Statment2;
---------------
----------------
Return(values);
}
Actual argument: the argument calling function are actual
argument.
!!!!
""""
Formal argument: the argument of called function is formal
argument.
Argument List: the argument list means various names enclosed
with in parenthesis. They must separated by comma.
Function Prototype:
- By default the function can pass integer type of data and
return integer type.
- Other than type of data, the function cannot pass and return.
In such cases we declare the function. Such definition of the
function is known as function prototype.
- The prototype of these function are given in header file. Those
we including using # include directives.
- In c while defining is defined function it must declare the
prototype.
- A prototype declaration consist of function return type ,name,
and arguments list.
- When programmers define the function of the function is same
as prototype declaration.
- If the programmer makes do any mistake, the compiler shows
the error message. The function prototype statement always
ends with semicolon.
Syntax:
Void main ()
{
Variable declaration/function declaration;
Value initialization;
Calling function
Printing result;
}
Function definition/return type function name (argument list)
!!!!
""""
{
Formal argument declaration;
Operation;
Return result;
}
Function declaration is four types:
1) Function with return type with arguments.
2) Function without return type with arguments
3) Function with return type without arguments
4) Function without return type without arguments.
Example: W.A.P add two numbers.
1) Function with return type with arguments.
Void main()
{
Int add(int,int);
Int a,b,c; Calling function
Printf(“Enter two numbers a & b”);
Scanf(“%d%d”,&a,&b);
C=add(a,b);
}
Int add(int x,int y)
{
Int z; Called function
Z=x+y;
Return(z);
}
Note: In this example a & b are the actual arguments
x, y are the formal arguments.
!!!!
""""
2) Function without return type with arguments.
Void main()
{
void add(int,int);
Int a,b; Calling function
Printf(“Enter two numbers a & b”);
Scanf(“%d%d”,&a,&b);
add(a,b);
}
Int add(int x,int y)
{
Int z; Called function
Z=x+y;
Printf (“%d”,z);
}
Note: In this example a & b are the actual arguments
x, y are the formal arguments.
3) Function with return type without arguments.
Void main()
{
int add( void ); calling function
add(a,b);
}
Int add(void)
{
Int x,y,z;
Printf(“Enter two numbers x & y”);
Scanf(“%d%d”,&x,&y); Called function
Z=x+y;
Return (z);
}
Note: Void means null/no arguments
!!!!
""""
4) Function without return type without arguments.
Void main()
{
void add( void ); calling
function
add(a,b);
}
void add(void)
{
Int x,y,z;
Printf(“Enter two numbers x & y”);
Scanf(“%d%d”,&x,&y); Called function
Z=x+y;
Printf (“%d”,z);
}
Note: Void means null/no arguments
Return Statements:
It is jump control statement, when it executes then the control
return to calling function.
Syntax:
Return; it means that it returns statement without values.
Return (value); it means that return a statement with a value.
- Return statement assign value.
- There is more than one return statement present in a function
body with conditions.
!!!!
""""
Program: W.A.P input Two no’s and then swap them.
#include<stdio.h>
#include<conio.h>
void main()
{
void swap(int,int);
int a,b;
clrscr();
printf("Enter two no");
scanf("%d%d",&a,&b);
swap(a,b);
//printf("%d%d",a,b);
getch();
}
void swap(int x,int y)
{
int z;
z=x;
x=y;
y=z;
printf("x=%d y=%d",x,y);
}
Program: W.A.P input a no and then reverses it.
#include<stdio.h>
#include<conio.h>
void main()
{
int rev(int);
int no,r;
clrscr();
printf("Enter a no");
!!!!
""""
scanf("%d",&no);
r=rev(no);
printf("%d",r);
getch();
}
int rev(int no1)
{
int r1=0,dg;
while(no1>0)
{
dg=no1%10;
r1=r1*10+dg;
no1=no1/10;
}
return(r1);
}
Program: W.A.P input a no and then check palindrome or not.
#include<stdio.h>
#include<conio.h>
void main()
{
int pali(int);
int no,r;
clrscr();
printf("Enter a no");
scanf("%d",&no);
r=pali(no);
if(r==no)
printf("palindrome");
else
printf("Not palindrome");
getch();
!!!!
""""
}
int pali(int no1)
{
int r1=0,dg;
while(no1>0)
{
dg=no1%10;
r1=r1*10+dg;
no1=no1/10;
}
return(r1);
}
Program: W.A.P to find out X ^ Y.
#include<stdio.h>
#include<conio.h>
#include<math.h>
void main()
{
int power(int,int);
int x,y,n;
clrscr();
printf("Enter two no x & y");
scanf("%d%d",&x,&y);
n=power(x,y);
printf("%d",n);
}
int power(int a,int b)
{
int n1=1;
while(b>0)
{
n1=n1*a;
!!!!
""""
b--;
}
return(n1);
}
Program: W.A.P to find out factorial of a no.
#include<stdio.h>
#include<conio.h>
void main()
{
int fact(int);//function prototype
int no,f;
clrscr();
printf("Enter a no");
scanf("%d",&no);
f=fact(no);//actual argument
printf("%d",f);
}
//Function Defination
int fact(int no1)//formal argument
{
int f1=1;
while(no1>0)
{
f1=f1*no1;
no1--;
}
return(f1);
}
!!!!
""""
Program: W.A.P to find out biggest no among three no’s
#include<stdio.h>
#include<conio.h>
void main()
{
int biggest(int,int,int);//function prototype
int a,b,c,d;
clrscr();
printf("Enter three nos");
scanf("%d%d%d",&a,&b,&c);
d=biggest(a,b,c);//actual argument
printf("biggest=%d",d);
}
//Function Defination
int biggest(int x,int y,int z)//formal argument
{
if(x>y&&x>z)
return(x);
else if(y>x&&y>z)
return(y);
else
return(z);
}
Program: C code to print Fibonacci series without recursion:
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
!!!!
""""
printf("Fibonacci Series: ");
printf("%d ",0);
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
long int first=0,second=1,sum;
while(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
n--;
}
}
Recursion
1. A recursive function is a function which calls itself.
2. The speed of a recursive program is slower because of stack
overheads. (This attribute is evident if you run above C
program.)
3. A recursive function must have recursive conditions,
terminating conditions, and recursive expressions.
Program: Calculating factorial value using recursion
To understand how recursion works lets have another popular
example of recursion. In this example we will calculate the factorial
of n numbers. The factorial of n numbers is expressed as a series of
repetitive multiplication as shown below:
Factorial of n = n (n-1)(n-2)……1.
Example: Factorial of 5 = 5x4x3x2x1=120
!!!!
""""
#include<stdio.h>
#include<conio.h>
int factorial(int);
void main()
{
int x;
clrscr();
printf("Enter any number to calculate factorial :");
scanf("%d",&x);
printf("nFactorial : %d", factorial (x));
getch();
}
int factorial (int i)
{
int f;
if(i==1)
return 1;
else
f = i* factorial (i-1);
return f;
}
Explanation:
Suppose value of i=5, since i is not equal to 1, the statement:
f = i* factorial (i-1);
Will be executed with i=5 i.e.
f = 5* factorial (5-1);
Will be evaluated. As you can see this statement again calls
factorial function with value i-1 which will return value:
4*factorial (4-1);
This recursive calling continues until value of i is equal to 1 and
when i is equal to 1 it returns 1 and execution of this function
stops. We can review the series of recursive call as follow:
f = 5* factorial (5-1);
!!!!
""""
f = 5*4* factorial (4-1);
f = 5*4*3* factorial (3-1);
f = 5*4*3*2* factorial (2-1);
f = 5*4*3*2*1;
f = 120;
Program: C code to find the addition of n numbers by recursion
#include<stdio.h>
int main()
{
int n,sum;
printf("Enter the value of n: ");
scanf("%d",&n);
sum = getSum(n);
printf("Sum of n numbers: %d",sum);
return 0;
}
int getSum(n)
{
int sum=0;
if(n>0){
sum = sum + n;
getSum(n-1);
}
return sum;
}
Sample output:
Enter the value of n: 10
Sum of n numbers: 55
!!!!
""""
Program: Fibonacci series in c by using recursion
#include<stdio.h>
void printFibonacci(int);
int main(){
int k,n;
long int i=0,j=1,f;
printf("Enter the range of the Fibonacci series: ");
scanf("%d",&n);
printf("Fibonacci Series: ");
printf("%d ",0);
printFibonacci(n);
return 0;
}
void printFibonacci(int n){
static long int first=0,second=1,sum;
if(n>0){
sum = first + second;
first = second;
second = sum;
printf("%ld ",sum);
printFibonacci(n-1);
}
}
Sample output:
Enter the range of the Fibonacci series: 10
Fibonacci Series: 0 1 2 3 5 8 13 21 34 55 89
!!!!
""""
Program: Reverse A Number Using Recursion In C Program
#include<stdio.h>
int main(){
int num,rev;
printf("nEnter a number :");
scanf("%d",&num);
rev=reverse(num);
printf("nAfter reverse the no is :%d",rev);
return 0;
}
int sum=0,r;
reverse(int num){
if(num){
r=num%10;
sum=sum*10+r;
reverse(num/10);
}
else
return sum;
return sum;
}

More Related Content

What's hot

C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functions
vinay arora
 

What's hot (20)

C Programming Language Part 6
C Programming Language Part 6C Programming Language Part 6
C Programming Language Part 6
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
7 functions
7  functions7  functions
7 functions
 
Programming Fundamentals Arrays and Strings
Programming Fundamentals   Arrays and Strings Programming Fundamentals   Arrays and Strings
Programming Fundamentals Arrays and Strings
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
Functions in C
Functions in CFunctions in C
Functions in C
 
C function
C functionC function
C function
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
C Prog - Functions
C Prog - FunctionsC Prog - Functions
C Prog - Functions
 
8 arrays and pointers
8  arrays and pointers8  arrays and pointers
8 arrays and pointers
 
function in c
function in cfunction in c
function in c
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
Function in c
Function in cFunction in c
Function in c
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 

Viewers also liked (18)

Preeprocessor
PreeprocessorPreeprocessor
Preeprocessor
 
Co question 2009
Co question 2009Co question 2009
Co question 2009
 
Array
ArrayArray
Array
 
Os notes
Os notesOs notes
Os notes
 
Dma
DmaDma
Dma
 
2011dbms
2011dbms2011dbms
2011dbms
 
OS ASSIGNMENT-1
OS ASSIGNMENT-1OS ASSIGNMENT-1
OS ASSIGNMENT-1
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Lesson plan proforma database management system
Lesson plan proforma database management systemLesson plan proforma database management system
Lesson plan proforma database management system
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
OS ASSIGNMENT 2
OS ASSIGNMENT 2OS ASSIGNMENT 2
OS ASSIGNMENT 2
 
System programming note
System programming noteSystem programming note
System programming note
 
Loader
LoaderLoader
Loader
 
Loaders
LoadersLoaders
Loaders
 
Loaders
LoadersLoaders
Loaders
 
Introduction to systems programming
Introduction to systems programmingIntroduction to systems programming
Introduction to systems programming
 
Operating system notes
Operating system notesOperating system notes
Operating system notes
 
Introduction to loaders
Introduction to loadersIntroduction to loaders
Introduction to loaders
 

Similar to Functions

โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
Little Tukta Lita
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
v_jk
 

Similar to Functions (20)

CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Function
FunctionFunction
Function
 
6. function
6. function6. function
6. function
 
Function in c
Function in cFunction in c
Function in c
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Function
FunctionFunction
Function
 
Array Cont
Array ContArray Cont
Array Cont
 
2. operator
2. operator2. operator
2. operator
 
Embedded C - Day 2
Embedded C - Day 2Embedded C - Day 2
Embedded C - Day 2
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions in C
Functions in CFunctions in C
Functions in C
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
 
eee2-day4-structures engineering college
eee2-day4-structures engineering collegeeee2-day4-structures engineering college
eee2-day4-structures engineering college
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Recursion in C
Recursion in CRecursion in C
Recursion in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Functions
FunctionsFunctions
Functions
 
Function in C program
Function in C programFunction in C program
Function in C program
 

More from SANTOSH RATH

Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
SANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
SANTOSH RATH
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
SANTOSH RATH
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management system
SANTOSH RATH
 
Expected Questions TC
Expected Questions TCExpected Questions TC
Expected Questions TC
SANTOSH RATH
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
SANTOSH RATH
 
Oops model question
Oops model questionOops model question
Oops model question
SANTOSH RATH
 
Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002
SANTOSH RATH
 
Btech 2nd ds_2008.ppt
Btech 2nd ds_2008.pptBtech 2nd ds_2008.ppt
Btech 2nd ds_2008.ppt
SANTOSH RATH
 
Btech 2nd ds_2007.ppt
Btech 2nd ds_2007.pptBtech 2nd ds_2007.ppt
Btech 2nd ds_2007.ppt
SANTOSH RATH
 
Btech 2nd ds_2005.ppt
Btech 2nd ds_2005.pptBtech 2nd ds_2005.ppt
Btech 2nd ds_2005.ppt
SANTOSH RATH
 

More from SANTOSH RATH (20)

Lesson plan proforma progrmming in c
Lesson plan proforma progrmming in cLesson plan proforma progrmming in c
Lesson plan proforma progrmming in c
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tc
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questions
 
2006dbms
2006dbms2006dbms
2006dbms
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management system
 
Rdbms2010
Rdbms2010Rdbms2010
Rdbms2010
 
Expected Questions TC
Expected Questions TCExpected Questions TC
Expected Questions TC
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbms
 
Oops model question
Oops model questionOops model question
Oops model question
 
OS ASSIGNMENT 3
OS ASSIGNMENT 3OS ASSIGNMENT 3
OS ASSIGNMENT 3
 
Ds using c 2009
Ds using c 2009Ds using c 2009
Ds using c 2009
 
Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002Data structure using c bcse 3102 pcs 1002
Data structure using c bcse 3102 pcs 1002
 
Btech 2nd ds_2008.ppt
Btech 2nd ds_2008.pptBtech 2nd ds_2008.ppt
Btech 2nd ds_2008.ppt
 
Btech 2nd ds_2007.ppt
Btech 2nd ds_2007.pptBtech 2nd ds_2007.ppt
Btech 2nd ds_2007.ppt
 
Btech 2nd ds_2005.ppt
Btech 2nd ds_2005.pptBtech 2nd ds_2005.ppt
Btech 2nd ds_2005.ppt
 
Ds using c 2011
Ds using c 2011Ds using c 2011
Ds using c 2011
 
Co question 2008
Co question 2008Co question 2008
Co question 2008
 
Co question 2006
Co question 2006Co question 2006
Co question 2006
 
Co question 2010
Co question 2010Co question 2010
Co question 2010
 

Recently uploaded

Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
ciinovamais
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
kauryashika82
 

Recently uploaded (20)

Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Micro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdfMicro-Scholarship, What it is, How can it help me.pdf
Micro-Scholarship, What it is, How can it help me.pdf
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Spatium Project Simulation student brief
Spatium Project Simulation student briefSpatium Project Simulation student brief
Spatium Project Simulation student brief
 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Dyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptxDyslexia AI Workshop for Slideshare.pptx
Dyslexia AI Workshop for Slideshare.pptx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 

Functions

  • 1. !!!! """" Function Function is self-contained block of program that perform specific task and return certain values. Syntax: <return type><function name> (argument list); - Return type is either void or int types Every “C” program start with main () function which is user defined functions. - C language supports two types of functions. - I) Library function - II) User defined functions Library Function: these are pre-defined functions in different header file. e.g: printf (),scanf(),gets(),sqrt(),pow() etc. e.g: #include<stdio.h> #include<math.h> Void main () { int no, r; printf (“Enter a number”); scanf (“%d”,&no); r=sqrt(no); printf(“%d”,r); } User defined function: They are defined by the user according user requirement. A user defined function has three things. i) Function prototype ii) Function call iii) Function definition
  • 2. !!!! """" Why use function: if we want to perform a task repeatedly then it is not necessary to re-write the particular block of the program again and again. -Shift the particular block of statement in a user defined function. the function defined can be used for any number of times to perform the task. - Use the function can be reduced to smaller one. - It is easy to debug and find out the error. - It also increases readability. How it works: whenever a function is called control passes to the called function and working of the calling function is stopped. - When the execution of function is completed, control returns to the calling function and execute the next statement. - The value of actual argument passed by the calling function and received by the formal argument of the called function. The no of actual argument and formal argument are same. - The function operates on formal argument and sends back the result to the calling functions, the return () statement performs this task. Syntax to Declare the Calling Function: <return type> <function name> (argument list with its data type); Argumnent –declaration; { Statment1; Statment2; --------------- ---------------- Return(values); } Actual argument: the argument calling function are actual argument.
  • 3. !!!! """" Formal argument: the argument of called function is formal argument. Argument List: the argument list means various names enclosed with in parenthesis. They must separated by comma. Function Prototype: - By default the function can pass integer type of data and return integer type. - Other than type of data, the function cannot pass and return. In such cases we declare the function. Such definition of the function is known as function prototype. - The prototype of these function are given in header file. Those we including using # include directives. - In c while defining is defined function it must declare the prototype. - A prototype declaration consist of function return type ,name, and arguments list. - When programmers define the function of the function is same as prototype declaration. - If the programmer makes do any mistake, the compiler shows the error message. The function prototype statement always ends with semicolon. Syntax: Void main () { Variable declaration/function declaration; Value initialization; Calling function Printing result; } Function definition/return type function name (argument list)
  • 4. !!!! """" { Formal argument declaration; Operation; Return result; } Function declaration is four types: 1) Function with return type with arguments. 2) Function without return type with arguments 3) Function with return type without arguments 4) Function without return type without arguments. Example: W.A.P add two numbers. 1) Function with return type with arguments. Void main() { Int add(int,int); Int a,b,c; Calling function Printf(“Enter two numbers a & b”); Scanf(“%d%d”,&a,&b); C=add(a,b); } Int add(int x,int y) { Int z; Called function Z=x+y; Return(z); } Note: In this example a & b are the actual arguments x, y are the formal arguments.
  • 5. !!!! """" 2) Function without return type with arguments. Void main() { void add(int,int); Int a,b; Calling function Printf(“Enter two numbers a & b”); Scanf(“%d%d”,&a,&b); add(a,b); } Int add(int x,int y) { Int z; Called function Z=x+y; Printf (“%d”,z); } Note: In this example a & b are the actual arguments x, y are the formal arguments. 3) Function with return type without arguments. Void main() { int add( void ); calling function add(a,b); } Int add(void) { Int x,y,z; Printf(“Enter two numbers x & y”); Scanf(“%d%d”,&x,&y); Called function Z=x+y; Return (z); } Note: Void means null/no arguments
  • 6. !!!! """" 4) Function without return type without arguments. Void main() { void add( void ); calling function add(a,b); } void add(void) { Int x,y,z; Printf(“Enter two numbers x & y”); Scanf(“%d%d”,&x,&y); Called function Z=x+y; Printf (“%d”,z); } Note: Void means null/no arguments Return Statements: It is jump control statement, when it executes then the control return to calling function. Syntax: Return; it means that it returns statement without values. Return (value); it means that return a statement with a value. - Return statement assign value. - There is more than one return statement present in a function body with conditions.
  • 7. !!!! """" Program: W.A.P input Two no’s and then swap them. #include<stdio.h> #include<conio.h> void main() { void swap(int,int); int a,b; clrscr(); printf("Enter two no"); scanf("%d%d",&a,&b); swap(a,b); //printf("%d%d",a,b); getch(); } void swap(int x,int y) { int z; z=x; x=y; y=z; printf("x=%d y=%d",x,y); } Program: W.A.P input a no and then reverses it. #include<stdio.h> #include<conio.h> void main() { int rev(int); int no,r; clrscr(); printf("Enter a no");
  • 8. !!!! """" scanf("%d",&no); r=rev(no); printf("%d",r); getch(); } int rev(int no1) { int r1=0,dg; while(no1>0) { dg=no1%10; r1=r1*10+dg; no1=no1/10; } return(r1); } Program: W.A.P input a no and then check palindrome or not. #include<stdio.h> #include<conio.h> void main() { int pali(int); int no,r; clrscr(); printf("Enter a no"); scanf("%d",&no); r=pali(no); if(r==no) printf("palindrome"); else printf("Not palindrome"); getch();
  • 9. !!!! """" } int pali(int no1) { int r1=0,dg; while(no1>0) { dg=no1%10; r1=r1*10+dg; no1=no1/10; } return(r1); } Program: W.A.P to find out X ^ Y. #include<stdio.h> #include<conio.h> #include<math.h> void main() { int power(int,int); int x,y,n; clrscr(); printf("Enter two no x & y"); scanf("%d%d",&x,&y); n=power(x,y); printf("%d",n); } int power(int a,int b) { int n1=1; while(b>0) { n1=n1*a;
  • 10. !!!! """" b--; } return(n1); } Program: W.A.P to find out factorial of a no. #include<stdio.h> #include<conio.h> void main() { int fact(int);//function prototype int no,f; clrscr(); printf("Enter a no"); scanf("%d",&no); f=fact(no);//actual argument printf("%d",f); } //Function Defination int fact(int no1)//formal argument { int f1=1; while(no1>0) { f1=f1*no1; no1--; } return(f1); }
  • 11. !!!! """" Program: W.A.P to find out biggest no among three no’s #include<stdio.h> #include<conio.h> void main() { int biggest(int,int,int);//function prototype int a,b,c,d; clrscr(); printf("Enter three nos"); scanf("%d%d%d",&a,&b,&c); d=biggest(a,b,c);//actual argument printf("biggest=%d",d); } //Function Defination int biggest(int x,int y,int z)//formal argument { if(x>y&&x>z) return(x); else if(y>x&&y>z) return(y); else return(z); } Program: C code to print Fibonacci series without recursion: #include<stdio.h> void printFibonacci(int); int main(){ int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n);
  • 12. !!!! """" printf("Fibonacci Series: "); printf("%d ",0); printFibonacci(n); return 0; } void printFibonacci(int n){ long int first=0,second=1,sum; while(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); n--; } } Recursion 1. A recursive function is a function which calls itself. 2. The speed of a recursive program is slower because of stack overheads. (This attribute is evident if you run above C program.) 3. A recursive function must have recursive conditions, terminating conditions, and recursive expressions. Program: Calculating factorial value using recursion To understand how recursion works lets have another popular example of recursion. In this example we will calculate the factorial of n numbers. The factorial of n numbers is expressed as a series of repetitive multiplication as shown below: Factorial of n = n (n-1)(n-2)……1. Example: Factorial of 5 = 5x4x3x2x1=120
  • 13. !!!! """" #include<stdio.h> #include<conio.h> int factorial(int); void main() { int x; clrscr(); printf("Enter any number to calculate factorial :"); scanf("%d",&x); printf("nFactorial : %d", factorial (x)); getch(); } int factorial (int i) { int f; if(i==1) return 1; else f = i* factorial (i-1); return f; } Explanation: Suppose value of i=5, since i is not equal to 1, the statement: f = i* factorial (i-1); Will be executed with i=5 i.e. f = 5* factorial (5-1); Will be evaluated. As you can see this statement again calls factorial function with value i-1 which will return value: 4*factorial (4-1); This recursive calling continues until value of i is equal to 1 and when i is equal to 1 it returns 1 and execution of this function stops. We can review the series of recursive call as follow: f = 5* factorial (5-1);
  • 14. !!!! """" f = 5*4* factorial (4-1); f = 5*4*3* factorial (3-1); f = 5*4*3*2* factorial (2-1); f = 5*4*3*2*1; f = 120; Program: C code to find the addition of n numbers by recursion #include<stdio.h> int main() { int n,sum; printf("Enter the value of n: "); scanf("%d",&n); sum = getSum(n); printf("Sum of n numbers: %d",sum); return 0; } int getSum(n) { int sum=0; if(n>0){ sum = sum + n; getSum(n-1); } return sum; } Sample output: Enter the value of n: 10 Sum of n numbers: 55
  • 15. !!!! """" Program: Fibonacci series in c by using recursion #include<stdio.h> void printFibonacci(int); int main(){ int k,n; long int i=0,j=1,f; printf("Enter the range of the Fibonacci series: "); scanf("%d",&n); printf("Fibonacci Series: "); printf("%d ",0); printFibonacci(n); return 0; } void printFibonacci(int n){ static long int first=0,second=1,sum; if(n>0){ sum = first + second; first = second; second = sum; printf("%ld ",sum); printFibonacci(n-1); } } Sample output: Enter the range of the Fibonacci series: 10 Fibonacci Series: 0 1 2 3 5 8 13 21 34 55 89
  • 16. !!!! """" Program: Reverse A Number Using Recursion In C Program #include<stdio.h> int main(){ int num,rev; printf("nEnter a number :"); scanf("%d",&num); rev=reverse(num); printf("nAfter reverse the no is :%d",rev); return 0; } int sum=0,r; reverse(int num){ if(num){ r=num%10; sum=sum*10+r; reverse(num/10); } else return sum; return sum; }