SlideShare una empresa de Scribd logo
1 de 24
A function is a block of statements that performs a specific task.
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
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 );
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(expressio
n);
}
Function
Body
Function Definition Examples
float conversion (float celsius)
{
float fahrenheit;
fahrenheit =
celcius*33.8 return
fahrenheit;
}
The function name‟s isconversion
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‟sdata type:
char
int
float
long………
Examples:
/* Returns a type int. */int func1(...)
float
func2(...)
void
func3(...)
/* Returns a type float.
*/
/* Returns nothing. */
`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) //called
function 18:
{
x_cubed = x * x *
x; return
x_cubed;
19: long x_cubed;
20:
21:
22:
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 datatype
Arguments/formalparameter
Actualparameters
• Write a program in C to find the square of any number using the function.
Test Data :
Input any number for square : 20
Expected Output :
The square of 20 is : 400.00
• Write a program in C to check a given number is even or odd using the function.
Test Data :
Input any number : 5
Expected Output :
The entered number is odd.
Function can be divided into 4 categories:
1. A function with no arguments and no return value
2. A function with no arguments and a return value
3. A function with an argument or arguments and returning no value
4. A function with arguments and returning a values
A function with no arguments and no return
valueCalled 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("-"); }
A function with no arguments and a return
valueDoes 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);
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
valuesArgument 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 +
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
7685
8
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
address
#include
<stdio.h>
#include
<conio.h>
int Value (int x);
int Reference (int *x);
int main()
{
int Batu_Pahat =
2; int Langkawi =
2;
Value(Batu_Pahat);
Reference(&Langkawi
);
printf("Batu_Pahat is number
%dn",Batu_Pahat); printf("Langkawi is
number %d",Langkawi);
}
int Value(int x)
{
x = 1;
}
int Reference(int *x)
{
*x = 1;
}
Pass by reference
You pass the variable address
Give the function direct access to the
variable
#include
<stdio.h>
#include
<conio.h>
void callByValue(int, int);
void callByReference(int *, int *);
int main()
{
int x=10, y =20;
printf("Value of x = %d and y = %d.
n",x,y);
printf("nCAll By Value function
call...n"); callByValue(x,y);
printf("nValue of x = %d and y = %d.n",
x,y);
printf("nCAll By Reference function
call...n"); callByReference(&x,&y);
printf("Value of x = %d and y = %d.n",
void callByValue(int x, int y)
{
int temp;
temp=x;
x=y;
y=temp;
printf("nValue of x = %d and y = %d inside callByValue
function",x,y);
}
void callByReference(int *x, int *y)
{
int temp;
temp=*x
;
*x=*y;
*y=temp;
}
Functionincprogram

Más contenido relacionado

La actualidad más candente

La actualidad más candente (20)

Call by value
Call by valueCall by value
Call by value
 
Function in c
Function in cFunction in c
Function in c
 
Functions in c
Functions in cFunctions in c
Functions in c
 
C function presentation
C function presentationC function presentation
C function presentation
 
C functions
C functionsC functions
C functions
 
Programming Fundamentals Functions in C and types
Programming Fundamentals  Functions in C  and typesProgramming Fundamentals  Functions in C  and types
Programming Fundamentals Functions in C and types
 
Function in C
Function in CFunction in C
Function in C
 
Types of function call
Types of function callTypes of function call
Types of function call
 
Function lecture
Function lectureFunction lecture
Function lecture
 
Functions in C
Functions in CFunctions in C
Functions in C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
C and C++ functions
C and C++ functionsC and C++ functions
C and C++ functions
 
Functions
FunctionsFunctions
Functions
 
Function in C program
Function in C programFunction in C program
Function in C program
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Functions in c
Functions in cFunctions in c
Functions in c
 
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
C++ FunctionC++ Function
C++ Function
 
Functions in C++
Functions in C++Functions in C++
Functions in C++
 

Similar a Functionincprogram

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).pptxvekariyakashyap
 
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).pptxSangeetaBorde3
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfJAVVAJI VENKATA RAO
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1Jeevan Raj
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4Saranya saran
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1YOGESH SINGH
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfTeshaleSiyum
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdfTeshaleSiyum
 

Similar a Functionincprogram (20)

unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
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
 
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
 
unit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdfunit3 part2 pcds function notes.pdf
unit3 part2 pcds function notes.pdf
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
C programming language working with functions 1
C programming language working with functions 1C programming language working with functions 1
C programming language working with functions 1
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Functions
Functions Functions
Functions
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1VIT351 Software Development VI Unit1
VIT351 Software Development VI Unit1
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++[ITP - Lecture 12] Functions in C/C++
[ITP - Lecture 12] Functions in C/C++
 
Functions in C++.pdf
Functions in C++.pdfFunctions in C++.pdf
Functions in C++.pdf
 
Chapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdfChapter_1.__Functions_in_C++[1].pdf
Chapter_1.__Functions_in_C++[1].pdf
 
Chapter 1. Functions in C++.pdf
Chapter 1.  Functions in C++.pdfChapter 1.  Functions in C++.pdf
Chapter 1. Functions in C++.pdf
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Function
Function Function
Function
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
4. function
4. function4. function
4. function
 

Último

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptxmary850239
 
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
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfJemuel Francisco
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4MiaBumagat1
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYKayeClaireEstoconing
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management systemChristalin Nelson
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONHumphrey A Beña
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxAshokKarra1
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxHumphrey A Beña
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Celine George
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxAnupkumar Sharma
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfErwinPantujan2
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...Nguyen Thanh Tu Collection
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parentsnavabharathschool99
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Jisc
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17Celine George
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxPoojaSen20
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinojohnmickonozaleda
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptxiammrhaywood
 

Último (20)

4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx4.16.24 21st Century Movements for Black Lives.pptx
4.16.24 21st Century Movements for Black Lives.pptx
 
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 🔝✔️✔️
 
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdfGrade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
Grade 9 Quarter 4 Dll Grade 9 Quarter 4 DLL.pdf
 
ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4ANG SEKTOR NG agrikultura.pptx QUARTER 4
ANG SEKTOR NG agrikultura.pptx QUARTER 4
 
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITYISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
ISYU TUNGKOL SA SEKSWLADIDA (ISSUE ABOUT SEXUALITY
 
Concurrency Control in Database Management system
Concurrency Control in Database Management systemConcurrency Control in Database Management system
Concurrency Control in Database Management system
 
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATIONTHEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
THEORIES OF ORGANIZATION-PUBLIC ADMINISTRATION
 
Karra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptxKarra SKD Conference Presentation Revised.pptx
Karra SKD Conference Presentation Revised.pptx
 
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptxINTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
INTRODUCTION TO CATHOLIC CHRISTOLOGY.pptx
 
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
Incoming and Outgoing Shipments in 3 STEPS Using Odoo 17
 
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptxMULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
MULTIDISCIPLINRY NATURE OF THE ENVIRONMENTAL STUDIES.pptx
 
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdfVirtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
Virtual-Orientation-on-the-Administration-of-NATG12-NATG6-and-ELLNA.pdf
 
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
HỌC TỐT TIẾNG ANH 11 THEO CHƯƠNG TRÌNH GLOBAL SUCCESS ĐÁP ÁN CHI TIẾT - CẢ NĂ...
 
Choosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for ParentsChoosing the Right CBSE School A Comprehensive Guide for Parents
Choosing the Right CBSE School A Comprehensive Guide for Parents
 
Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...Procuring digital preservation CAN be quick and painless with our new dynamic...
Procuring digital preservation CAN be quick and painless with our new dynamic...
 
How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17How to Add Barcode on PDF Report in Odoo 17
How to Add Barcode on PDF Report in Odoo 17
 
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptxCulture Uniformity or Diversity IN SOCIOLOGY.pptx
Culture Uniformity or Diversity IN SOCIOLOGY.pptx
 
FILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipinoFILIPINO PSYCHology sikolohiyang pilipino
FILIPINO PSYCHology sikolohiyang pilipino
 
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptxAUDIENCE THEORY -CULTIVATION THEORY -  GERBNER.pptx
AUDIENCE THEORY -CULTIVATION THEORY - GERBNER.pptx
 
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptxYOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
YOUVE_GOT_EMAIL_PRELIMS_EL_DORADO_2024.pptx
 

Functionincprogram

  • 1.
  • 2. A function is a block of statements that performs a specific task. 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. 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.
  • 5. 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 );
  • 6. 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(expressio n); } Function Body
  • 7. Function Definition Examples float conversion (float celsius) { float fahrenheit; fahrenheit = celcius*33.8 return fahrenheit; } The function name‟s isconversion 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.
  • 8. Can be any of C‟sdata type: char int float long……… Examples: /* Returns a type int. */int func1(...) float func2(...) void func3(...) /* Returns a type float. */ /* Returns nothing. */
  • 9. `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) //called function 18: { x_cubed = x * x * x; return x_cubed; 19: long x_cubed; 20: 21: 22: 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 datatype Arguments/formalparameter Actualparameters
  • 10. • Write a program in C to find the square of any number using the function. Test Data : Input any number for square : 20 Expected Output : The square of 20 is : 400.00 • Write a program in C to check a given number is even or odd using the function. Test Data : Input any number : 5 Expected Output : The entered number is odd.
  • 11. Function can be divided into 4 categories: 1. A function with no arguments and no return value 2. A function with no arguments and a return value 3. A function with an argument or arguments and returning no value 4. A function with arguments and returning a values
  • 12. A function with no arguments and no return valueCalled 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("-"); }
  • 13. A function with no arguments and a return valueDoes 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); Enter a no: 46 You entered : 46.
  • 14. 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.
  • 15. #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);
  • 16. A function with arguments and returning a valuesArgument 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.
  • 17. 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 + 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.
  • 18. 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 7685 8 Memory location name Value at memory location Memory location address We can use the address which also point the same value.
  • 19. #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
  • 20. #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 address
  • 21. #include <stdio.h> #include <conio.h> int Value (int x); int Reference (int *x); int main() { int Batu_Pahat = 2; int Langkawi = 2; Value(Batu_Pahat); Reference(&Langkawi ); printf("Batu_Pahat is number %dn",Batu_Pahat); printf("Langkawi is number %d",Langkawi); } int Value(int x) { x = 1; } int Reference(int *x) { *x = 1; } Pass by reference You pass the variable address Give the function direct access to the variable
  • 22. #include <stdio.h> #include <conio.h> void callByValue(int, int); void callByReference(int *, int *); int main() { int x=10, y =20; printf("Value of x = %d and y = %d. n",x,y); printf("nCAll By Value function call...n"); callByValue(x,y); printf("nValue of x = %d and y = %d.n", x,y); printf("nCAll By Reference function call...n"); callByReference(&x,&y); printf("Value of x = %d and y = %d.n",
  • 23. void callByValue(int x, int y) { int temp; temp=x; x=y; y=temp; printf("nValue of x = %d and y = %d inside callByValue function",x,y); } void callByReference(int *x, int *y) { int temp; temp=*x ; *x=*y; *y=temp; }