SlideShare a Scribd company logo
1 of 26
A large program in c can be divided to many subprogram
The subprogram posses a self contain components and have well define purpose.
The subprogram is called as a function
Basically a job of function is to do something
C program contain at least one function which is main().
Classification of
Function
Library
function
User define
function
- main() -printf()
-scanf()
-pow()
-ceil()
It is much easier to write a structured program where a large program can be divided
into a smaller, simpler task.
Allowing the code to be called many times
Easier to read and update
It is easier to debug a structured program where there error is easy to find and fix
1: #include <stdio.h>
2:
3: long cube(long x);
4:
5: long input, answer;
6:
7: int main( void )
8: {
9: printf(“Enter an integer value: ”);
10: scanf(“%d”, &input);
11: answer = cube(input);
12: printf(“nThe cube of %ld is %ld.n”, input,
answer);
13:
14: return 0;
15: }
16:
17: long cube(long x)
18: {
19: long x_cubed;
20:
21: x_cubed = x * x * x;
22: return x_cubed;
23: }
 Function names is cube
 Variable that are requires is
long
 The variable to be passed
on is X(has single
arguments)—value can be
passed to function so it can
perform the specific task. It
is called
Output
Enter an integer
value:4
The cube of 4 is 64.
Return data type
Arguments/formal parameter
Actual parameters
C program doesn't execute the statement in function until the function is called.
When function is called the program can send the function information in the form
of one or more argument.
When the function is used it is referred to as the called function
Functions often use data that is passed to them from the calling function
Data is passed from the calling function to a called function by specifying the
variables in a argument list.
Argument list cannot be used to send data. Its only copy data/value/variable that
pass from the calling function.
The called function then performs its operation using the copies.
Provides the compiler with the description of functions that will be used later in the
program
Its define the function before it been used/called
Function prototypes need to be written at the beginning of the program.
The function prototype must have :
A return type indicating the variable that the function will be return
Syntax for Function Prototype
return-type function_name( arg-type name-1,...,arg-type name-n);
Function Prototype Examples
 double squared( double number );
 void print_report( int report_number );
 int get_menu_choice( void);
It is the actual function that contains the code that will be execute.
Should be identical to the function prototype.
Syntax of Function Definition
return-type function_name( arg-type name-1,...,arg-type name-n) ---- Function header
{
declarations;
statements;
return(expression);
}
Function Body
Function Definition Examples
float conversion (float celsius)
{
float fahrenheit;
fahrenheit = celcius*33.8
return fahrenheit;
}
The function name’s is conversion
This function accepts arguments celcius of the type float. The function return a float
value.
So, when this function is called in the program, it will perform its task which is to convert
fahrenheit by multiply celcius with 33.8 and return the result of the summation.
Note that if the function is returning a value, it needs to use the keyword return.
Can be any of C’s data type:
char
int
float
long………
Examples:
int func1(...) /* Returns a type int. */
float func2(...) /* Returns a type float. */
void func3(...) /* Returns nothing. */
Function can be divided into 4 categories:
A function with no arguments and no return value
A function with no arguments and a return value
A function with an argument or arguments and returning no value
A function with arguments and returning a values
A function with no arguments and no return
value
Called function does not have any arguments
Not able to get any value from the calling function
Not returning any value
There is no data transfer between the calling function and called
function.
#include<stdio.h>
#include<conio.h>
void printline();
void main()
{
printf("Welcome to function in
C");
printline();
printf("Function easy to learn.");
printline();
getch();
}
void printline()
{
int i;
printf("n");
for(i=0;i<30;i++)
{ printf("-"); }
printf("n");
}
A function with no arguments and a return value
Does not get any value from the calling function
Can give a return value to calling program
#include <stdio.h>
#include <conio.h>
int send();
void main()
{
int z;
z=send();
printf("nYou entered : %d.",z);
getch();
}
int send()
{
int no1;
printf("Enter a no: ");
scanf("%d",&no1);
return(no1);
}
Enter a no: 46
You entered : 46.
A function with an argument or arguments and returning
no value
A function has argument/s
A calling function can pass values to function called , but calling function not
receive any value
Data is transferred from calling function to the called function but no data is
transferred from the called function to the calling function
Generally Output is printed in the Called function
A function that does not return any value cannot be used in an expression it can
be used only as independent statement.
#include<stdio.h>
#include<conio.h>
void add(int x, int y);
void main()
{
add(30,15);
add(63,49);
add(952,321);
getch();
}
void add(int x, int y)
{
int result;
result = x+y;
printf("Sum of %d and %d is
%d.nn",x,y,result);
}
A function with arguments and returning a values
Argument are passed by calling function to the called function
Called function return value to the calling function
Mostly used in programming because it can two way communication
Data returned by the function can be used later in our program for further
calculation.
Result 85.
Result 1273.
#include <stdio.h>
#include <conio.h>
int add(int x,int y);
void main()
{
int z;
z=add(952,321);
printf("Result %d. nn",add(30,55));
printf("Result %d.nn",z);
getch();
}
int add(int x,int y)
{
int result;
result = x + y;
return(result);
}
Send 2 integer value x and y to add()
Function add the two values and send
back the result to the calling function
int is the return type of function
Return statement is a keyword and in
bracket we can give values which we
want to return.
Variable that declared occupies a memory according to it size
It has address for the location so it can be referred later by CPU for manipulation
The ‘*’ and ‘&’ Operator
Int x= 10
x
10
76858
Memory location name
Value at memory location
Memory location address
We can use the address which also point the same value.
#include <stdio.h>
#include <conio.h>
void main()
{
int i=9;
printf("Value of i : %dn",i);
printf("Adress of i %dn", &i);
getch();
}
& show the address of the variable
#include <stdio.h>
#include <conio.h>
void main()
{
int i=9;
printf("Value of i : %dn",i);
printf("Address of i %dn", &i);
printf("Value at address of i: %d", *(&i));
getch();
}
* Symbols called the value at the addres
CALL BY VALUE & REFERENCE
Call By value:
 Value of actual arguments are passed to
formal arguments.
 The operation is done on formal operations.
Call By Reference:
 It is passing values, address are passed.
 The function operates on address rather then
values.
EXAMPLE PGM’S
CALL BY VALUE
main()
{
int x,y,change(int,int);
clrscr();
printf(“Enter the values of X & Y:”);
scanf(“%d%d”,&x,&y);
change(x,y);
printf(“In main() X=%d Y=%d”,x,y);
return 0;
}
change(int a,int b)
{
int k;
k=a;
a=b;
b=k;
printf(“In Change() X=%d
y=%d”,a,b);
}
CALL BY REFERENCE
main()
{
int x,y,change(int*,int*);
clrscr();
printf(“Enter the values of X & Y:”);
scanf(“%d%d”,&x,&y);
change(x,y);
printf(“In main() X=%d Y=%d”,x,y);
return 0;
}
change(int *a,int *b)
{
int *k;
*k=*a;
*a=*b;
*b=*k;
printf(“InChange()X=%dy=%d”,*a,*b)
;
}7
OUTPUT
CALL BY VALUE:
Enter the value of X & Y: 5 4
In Change X=4 Y=5
In main() X=5 Y=4
CALL BY REFERENCE:
Enter the value of X & Y: 5 4
In Change X=4 Y=5
In main() X=4 Y=5
RECURSION
 It function is called repetitively by itself.
 Recursion can be used directly and indirectly.
 Directly recursion function calls itself for
condition true.
 Indirectly recursion function is called another
function calls
EXAMPLE
int x=1,s=0;
void main(int);
void main(x)
{
s=s+x;
printf(“n x=%d s=%d”,x,s);
if(x==5)
exit(0);
main(++x);
}
OUTPUT
x=1 s=1
x=2 s=3
x=3 s=6
x=4 s=10
x=5 s=15
C function

More Related Content

What's hot (20)

Function Pointer
Function PointerFunction Pointer
Function Pointer
 
C functions
C functionsC functions
C functions
 
Constants in C Programming
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
 
C programming tutorial
C programming tutorialC programming tutorial
C programming tutorial
 
Strings in c
Strings in cStrings in c
Strings in c
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
 
Function in c
Function in cFunction in c
Function in c
 
C++
C++C++
C++
 
Programming in c
Programming in cProgramming in c
Programming in c
 
Looping statements in C
Looping statements in CLooping statements in C
Looping statements in C
 
Character Array and String
Character Array and StringCharacter Array and String
Character Array and String
 
Variables in C Programming
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
 
Introduction to c programming
Introduction to c programmingIntroduction to c programming
Introduction to c programming
 
Command line arguments
Command line argumentsCommand line arguments
Command line arguments
 
C presentation
C presentationC presentation
C presentation
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
C function presentation
C function presentationC function presentation
C function presentation
 
Functions in c
Functions in cFunctions in c
Functions in c
 
Arrays in C language
Arrays in C languageArrays in C language
Arrays in C language
 
structure and union
structure and unionstructure and union
structure and union
 

Viewers also liked

Bespoke glasses | Cool Eyewear
Bespoke glasses | Cool EyewearBespoke glasses | Cool Eyewear
Bespoke glasses | Cool EyewearMono Qool
 
The different rhinoplasty procedures
The different rhinoplasty proceduresThe different rhinoplasty procedures
The different rhinoplasty proceduresHealth First
 
The many hidden causes of hip pain
The many hidden causes of hip painThe many hidden causes of hip pain
The many hidden causes of hip painHealth First
 
exhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractorsexhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractorsexhibithire
 
The 8 different sub disciplines of urology
The 8 different sub disciplines of urologyThe 8 different sub disciplines of urology
The 8 different sub disciplines of urologyHealth First
 
Tension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guideTension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guideexhibithire
 
VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]Carlos Castilho
 
event planning,exhibition,designers,events management,companies,contractors
event planning,exhibition,designers,events management,companies,contractorsevent planning,exhibition,designers,events management,companies,contractors
event planning,exhibition,designers,events management,companies,contractorsexhibithire
 

Viewers also liked (11)

Coper in C
Coper in CCoper in C
Coper in C
 
Bespoke glasses | Cool Eyewear
Bespoke glasses | Cool EyewearBespoke glasses | Cool Eyewear
Bespoke glasses | Cool Eyewear
 
SGI. PROFILE
SGI. PROFILESGI. PROFILE
SGI. PROFILE
 
The different rhinoplasty procedures
The different rhinoplasty proceduresThe different rhinoplasty procedures
The different rhinoplasty procedures
 
The many hidden causes of hip pain
The many hidden causes of hip painThe many hidden causes of hip pain
The many hidden causes of hip pain
 
exhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractorsexhibitions,event planning,designers,events management,companies,contractors
exhibitions,event planning,designers,events management,companies,contractors
 
The 8 different sub disciplines of urology
The 8 different sub disciplines of urologyThe 8 different sub disciplines of urology
The 8 different sub disciplines of urology
 
Tension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guideTension fabric-printing-inspiration-guide
Tension fabric-printing-inspiration-guide
 
VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]VGIPM_Conference_CTA_2013_Abstracts[1]
VGIPM_Conference_CTA_2013_Abstracts[1]
 
event planning,exhibition,designers,events management,companies,contractors
event planning,exhibition,designers,events management,companies,contractorsevent planning,exhibition,designers,events management,companies,contractors
event planning,exhibition,designers,events management,companies,contractors
 
Data type in c
Data type in cData type in c
Data type in c
 

Similar to C function

Similar to C function (20)

Function in c program
Function in c programFunction in c program
Function in c program
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
Function in c
Function in cFunction in c
Function in c
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptxUnit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
Array Cont
Array ContArray Cont
Array Cont
 
Classes function overloading
Classes function overloadingClasses function overloading
Classes function overloading
 

More from thirumalaikumar3

More from thirumalaikumar3 (7)

Control flow in c
Control flow in cControl flow in c
Control flow in c
 
C basics
C   basicsC   basics
C basics
 
Structure c
Structure cStructure c
Structure c
 
String c
String cString c
String c
 
File handling in c
File  handling in cFile  handling in c
File handling in c
 
File handling-c programming language
File handling-c programming languageFile handling-c programming language
File handling-c programming language
 
Data type2 c
Data type2 cData type2 c
Data type2 c
 

Recently uploaded

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
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 ClassesCeline George
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introductionMaksud Ahmed
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIShubhangi Sonawane
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfAyushMahapatra5
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
 
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-701bronxfugly43
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Shubhangi Sonawane
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxnegromaestrong
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxAreebaZafar22
 
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).pptxVishalSingh1417
 

Recently uploaded (20)

Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
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
 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-IIFood Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
Food Chain and Food Web (Ecosystem) EVS, B. Pharmacy 1st Year, Sem-II
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Class 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdfClass 11th Physics NEET formula sheet pdf
Class 11th Physics NEET formula sheet pdf
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
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
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
Ecological Succession. ( ECOSYSTEM, B. Pharmacy, 1st Year, Sem-II, Environmen...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Seal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptxSeal of Good Local Governance (SGLG) 2024Final.pptx
Seal of Good Local Governance (SGLG) 2024Final.pptx
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
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
 

C function

  • 1.
  • 2. A large program in c can be divided to many subprogram The subprogram posses a self contain components and have well define purpose. The subprogram is called as a function Basically a job of function is to do something C program contain at least one function which is main(). Classification of Function Library function User define function - main() -printf() -scanf() -pow() -ceil()
  • 3. It is much easier to write a structured program where a large program can be divided into a smaller, simpler task. Allowing the code to be called many times Easier to read and update It is easier to debug a structured program where there error is easy to find and fix
  • 4. 1: #include <stdio.h> 2: 3: long cube(long x); 4: 5: long input, answer; 6: 7: int main( void ) 8: { 9: printf(“Enter an integer value: ”); 10: scanf(“%d”, &input); 11: answer = cube(input); 12: printf(“nThe cube of %ld is %ld.n”, input, answer); 13: 14: return 0; 15: } 16: 17: long cube(long x) 18: { 19: long x_cubed; 20: 21: x_cubed = x * x * x; 22: return x_cubed; 23: }  Function names is cube  Variable that are requires is long  The variable to be passed on is X(has single arguments)—value can be passed to function so it can perform the specific task. It is called Output Enter an integer value:4 The cube of 4 is 64. Return data type Arguments/formal parameter Actual parameters
  • 5. C program doesn't execute the statement in function until the function is called. When function is called the program can send the function information in the form of one or more argument. When the function is used it is referred to as the called function Functions often use data that is passed to them from the calling function Data is passed from the calling function to a called function by specifying the variables in a argument list. Argument list cannot be used to send data. Its only copy data/value/variable that pass from the calling function. The called function then performs its operation using the copies.
  • 6. Provides the compiler with the description of functions that will be used later in the program Its define the function before it been used/called Function prototypes need to be written at the beginning of the program. The function prototype must have : A return type indicating the variable that the function will be return Syntax for Function Prototype return-type function_name( arg-type name-1,...,arg-type name-n); Function Prototype Examples  double squared( double number );  void print_report( int report_number );  int get_menu_choice( void);
  • 7. It is the actual function that contains the code that will be execute. Should be identical to the function prototype. Syntax of Function Definition return-type function_name( arg-type name-1,...,arg-type name-n) ---- Function header { declarations; statements; return(expression); } Function Body
  • 8. Function Definition Examples float conversion (float celsius) { float fahrenheit; fahrenheit = celcius*33.8 return fahrenheit; } The function name’s is conversion This function accepts arguments celcius of the type float. The function return a float value. So, when this function is called in the program, it will perform its task which is to convert fahrenheit by multiply celcius with 33.8 and return the result of the summation. Note that if the function is returning a value, it needs to use the keyword return.
  • 9. Can be any of C’s data type: char int float long……… Examples: int func1(...) /* Returns a type int. */ float func2(...) /* Returns a type float. */ void func3(...) /* Returns nothing. */
  • 10. Function can be divided into 4 categories: A function with no arguments and no return value A function with no arguments and a return value A function with an argument or arguments and returning no value A function with arguments and returning a values
  • 11. A function with no arguments and no return value Called function does not have any arguments Not able to get any value from the calling function Not returning any value There is no data transfer between the calling function and called function. #include<stdio.h> #include<conio.h> void printline(); void main() { printf("Welcome to function in C"); printline(); printf("Function easy to learn."); printline(); getch(); } void printline() { int i; printf("n"); for(i=0;i<30;i++) { printf("-"); } printf("n"); }
  • 12. A function with no arguments and a return value Does not get any value from the calling function Can give a return value to calling program #include <stdio.h> #include <conio.h> int send(); void main() { int z; z=send(); printf("nYou entered : %d.",z); getch(); } int send() { int no1; printf("Enter a no: "); scanf("%d",&no1); return(no1); } Enter a no: 46 You entered : 46.
  • 13. A function with an argument or arguments and returning no value A function has argument/s A calling function can pass values to function called , but calling function not receive any value Data is transferred from calling function to the called function but no data is transferred from the called function to the calling function Generally Output is printed in the Called function A function that does not return any value cannot be used in an expression it can be used only as independent statement.
  • 14. #include<stdio.h> #include<conio.h> void add(int x, int y); void main() { add(30,15); add(63,49); add(952,321); getch(); } void add(int x, int y) { int result; result = x+y; printf("Sum of %d and %d is %d.nn",x,y,result); }
  • 15. A function with arguments and returning a values Argument are passed by calling function to the called function Called function return value to the calling function Mostly used in programming because it can two way communication Data returned by the function can be used later in our program for further calculation.
  • 16. Result 85. Result 1273. #include <stdio.h> #include <conio.h> int add(int x,int y); void main() { int z; z=add(952,321); printf("Result %d. nn",add(30,55)); printf("Result %d.nn",z); getch(); } int add(int x,int y) { int result; result = x + y; return(result); } Send 2 integer value x and y to add() Function add the two values and send back the result to the calling function int is the return type of function Return statement is a keyword and in bracket we can give values which we want to return.
  • 17. Variable that declared occupies a memory according to it size It has address for the location so it can be referred later by CPU for manipulation The ‘*’ and ‘&’ Operator Int x= 10 x 10 76858 Memory location name Value at memory location Memory location address We can use the address which also point the same value.
  • 18. #include <stdio.h> #include <conio.h> void main() { int i=9; printf("Value of i : %dn",i); printf("Adress of i %dn", &i); getch(); } & show the address of the variable
  • 19. #include <stdio.h> #include <conio.h> void main() { int i=9; printf("Value of i : %dn",i); printf("Address of i %dn", &i); printf("Value at address of i: %d", *(&i)); getch(); } * Symbols called the value at the addres
  • 20. CALL BY VALUE & REFERENCE Call By value:  Value of actual arguments are passed to formal arguments.  The operation is done on formal operations. Call By Reference:  It is passing values, address are passed.  The function operates on address rather then values.
  • 21. EXAMPLE PGM’S CALL BY VALUE main() { int x,y,change(int,int); clrscr(); printf(“Enter the values of X & Y:”); scanf(“%d%d”,&x,&y); change(x,y); printf(“In main() X=%d Y=%d”,x,y); return 0; } change(int a,int b) { int k; k=a; a=b; b=k; printf(“In Change() X=%d y=%d”,a,b); } CALL BY REFERENCE main() { int x,y,change(int*,int*); clrscr(); printf(“Enter the values of X & Y:”); scanf(“%d%d”,&x,&y); change(x,y); printf(“In main() X=%d Y=%d”,x,y); return 0; } change(int *a,int *b) { int *k; *k=*a; *a=*b; *b=*k; printf(“InChange()X=%dy=%d”,*a,*b) ; }7
  • 22. OUTPUT CALL BY VALUE: Enter the value of X & Y: 5 4 In Change X=4 Y=5 In main() X=5 Y=4 CALL BY REFERENCE: Enter the value of X & Y: 5 4 In Change X=4 Y=5 In main() X=4 Y=5
  • 23. RECURSION  It function is called repetitively by itself.  Recursion can be used directly and indirectly.  Directly recursion function calls itself for condition true.  Indirectly recursion function is called another function calls
  • 24. EXAMPLE int x=1,s=0; void main(int); void main(x) { s=s+x; printf(“n x=%d s=%d”,x,s); if(x==5) exit(0); main(++x); }
  • 25. OUTPUT x=1 s=1 x=2 s=3 x=3 s=6 x=4 s=10 x=5 s=15