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 (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

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 (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

Lesson plan proforma progrmming in c
Lesson plan proforma progrmming in cLesson plan proforma progrmming in c
Lesson plan proforma progrmming in cSANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tcSANTOSH RATH
 
Expected questions tc
Expected questions tcExpected questions tc
Expected questions tcSANTOSH RATH
 
Module wise format oops questions
Module wise format oops questionsModule wise format oops questions
Module wise format oops questionsSANTOSH RATH
 
( Becs 2208 ) database management system
( Becs 2208 ) database management system( Becs 2208 ) database management system
( Becs 2208 ) database management systemSANTOSH RATH
 
Expected Questions TC
Expected Questions TCExpected Questions TC
Expected Questions TCSANTOSH RATH
 
Expected questions for dbms
Expected questions for dbmsExpected questions for dbms
Expected questions for dbmsSANTOSH RATH
 
Oops model question
Oops model questionOops model question
Oops model questionSANTOSH 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 1002SANTOSH RATH
 
Btech 2nd ds_2008.ppt
Btech 2nd ds_2008.pptBtech 2nd ds_2008.ppt
Btech 2nd ds_2008.pptSANTOSH RATH
 
Btech 2nd ds_2007.ppt
Btech 2nd ds_2007.pptBtech 2nd ds_2007.ppt
Btech 2nd ds_2007.pptSANTOSH RATH
 
Btech 2nd ds_2005.ppt
Btech 2nd ds_2005.pptBtech 2nd ds_2005.ppt
Btech 2nd ds_2005.pptSANTOSH 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

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppCeline George
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfUmakantAnnand
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️9953056974 Low Rate Call Girls In Saket, Delhi NCR
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 

Recently uploaded (20)

“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
URLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website AppURLs and Routing in the Odoo 17 Website App
URLs and Routing in the Odoo 17 Website App
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
Concept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.CompdfConcept of Vouching. B.Com(Hons) /B.Compdf
Concept of Vouching. B.Com(Hons) /B.Compdf
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
call girls in Kamla Market (DELHI) 🔝 >༒9953330565🔝 genuine Escort Service 🔝✔️✔️
 
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 

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; }