Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
SlideShare a Scribd company logo
Function in c program
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
#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
The variable can be modified inside the
#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", x,y);
getch();
return 0;
}
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;
}
Function in c program

More Related Content

What's hot

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
03062679929
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
Rokonuzzaman Rony
 
C functions
C functionsC functions
Function in c
Function in cFunction in c
Function in c
Raj Tandukar
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
Abu Zaman
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
Neeru Mittal
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
Vineeta Garg
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
Alisha Korpal
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
Sathish Narayanan
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
Sokngim Sa
 
Function in C
Function in CFunction in C
Function in C
Dr. Abhineet Anand
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk
 
Branching statements
Branching statementsBranching statements
Branching statements
ArunMK17
 
Function C programming
Function C programmingFunction C programming
Function C programming
Appili Vamsi Krishna
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
Harsh Pathak
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
Shaina Arora
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
Ideal Eyes Business College
 
virtual function
virtual functionvirtual function
virtual function
VENNILAV6
 
C function
C functionC function
C function
thirumalaikumar3
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna
 

What's hot (20)

FUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
 
Functions in c++
Functions in c++Functions in c++
Functions in c++
 
C functions
C functionsC functions
C functions
 
Function in c
Function in cFunction in c
Function in c
 
Presentation on function
Presentation on functionPresentation on function
Presentation on function
 
Operators and expressions in C++
Operators and expressions in C++Operators and expressions in C++
Operators and expressions in C++
 
Pointers in c++
Pointers in c++Pointers in c++
Pointers in c++
 
Prsentation on functions
Prsentation on functionsPrsentation on functions
Prsentation on functions
 
Control statements in c
Control statements in cControl statements in c
Control statements in c
 
C Programming: Control Structure
C Programming: Control StructureC Programming: Control Structure
C Programming: Control Structure
 
Function in C
Function in CFunction in C
Function in C
 
RECURSION IN C
RECURSION IN C RECURSION IN C
RECURSION IN C
 
Branching statements
Branching statementsBranching statements
Branching statements
 
Function C programming
Function C programmingFunction C programming
Function C programming
 
Recursive Function
Recursive FunctionRecursive Function
Recursive Function
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
virtual function
virtual functionvirtual function
virtual function
 
C function
C functionC function
C function
 
Pointers C programming
Pointers  C programmingPointers  C programming
Pointers C programming
 

Similar to Function in c program

Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
Chandrakant Divate
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
Sampath Kumar
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
mohd_mizan
 
Array Cont
Array ContArray Cont
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
MKalpanaDevi
 
Function in c
Function in cFunction in c
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
KarthikSivagnanam2
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
Venkatesh Goud
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
Sowri Rajan
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
JVenkateshGoud
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman
 
Function
FunctionFunction
Function
mshoaib15
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
Amarjith C K
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha
 
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
vekariyakashyap
 
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
imtiazalijoono
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
psaravanan1985
 

Similar to Function in c program (20)

Fundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptxFundamentals of functions in C program.pptx
Fundamentals of functions in C program.pptx
 
Functionincprogram
FunctionincprogramFunctionincprogram
Functionincprogram
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Array Cont
Array ContArray Cont
Array Cont
 
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
 
Function in c
Function in cFunction in c
Function in c
 
Functions and pointers_unit_4
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
 
Functions in C.pptx
Functions in C.pptxFunctions in C.pptx
Functions in C.pptx
 
unit_2.pptx
unit_2.pptxunit_2.pptx
unit_2.pptx
 
Unit 3 (1)
Unit 3 (1)Unit 3 (1)
Unit 3 (1)
 
unit_2 (1).pptx
unit_2 (1).pptxunit_2 (1).pptx
unit_2 (1).pptx
 
Presentation on Function in C Programming
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
 
Function
FunctionFunction
Function
 
cp Module4(1)
cp Module4(1)cp Module4(1)
cp Module4(1)
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
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
 
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
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 

More from umesh patil

Ccna security
Ccna security Ccna security
Ccna security
umesh patil
 
Array in c language
Array in c languageArray in c language
Array in c language
umesh patil
 
Array in c language
Array in c language Array in c language
Array in c language
umesh patil
 
Jquery Preparation
Jquery PreparationJquery Preparation
Jquery Preparation
umesh patil
 
Cloud computing
Cloud computingCloud computing
Cloud computing
umesh patil
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
umesh patil
 
Introduction to asp .net
Introduction to asp .netIntroduction to asp .net
Introduction to asp .net
umesh patil
 
C language
C language C language
C language
umesh patil
 
Html and css presentation
Html and css presentationHtml and css presentation
Html and css presentation
umesh patil
 
Html Presentation
Html PresentationHtml Presentation
Html Presentation
umesh patil
 
Cloud computing
Cloud computingCloud computing
Cloud computing
umesh patil
 
Oops and c fundamentals
Oops and c fundamentals Oops and c fundamentals
Oops and c fundamentals
umesh patil
 
Java script
Java scriptJava script
Java script
umesh patil
 
css and wordpress
css and wordpresscss and wordpress
css and wordpress
umesh patil
 
css and wordpress
css and wordpresscss and wordpress
css and wordpress
umesh patil
 
Php vs asp
Php vs aspPhp vs asp
Php vs asp
umesh patil
 
Ccna security
Ccna security Ccna security
Ccna security
umesh patil
 
Cloud computing
Cloud computingCloud computing
Cloud computing
umesh patil
 
Cloud computing
Cloud computingCloud computing
Cloud computing
umesh patil
 
C language
C languageC language
C language
umesh patil
 

More from umesh patil (20)

Ccna security
Ccna security Ccna security
Ccna security
 
Array in c language
Array in c languageArray in c language
Array in c language
 
Array in c language
Array in c language Array in c language
Array in c language
 
Jquery Preparation
Jquery PreparationJquery Preparation
Jquery Preparation
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Static and dynamic polymorphism
Static and dynamic polymorphismStatic and dynamic polymorphism
Static and dynamic polymorphism
 
Introduction to asp .net
Introduction to asp .netIntroduction to asp .net
Introduction to asp .net
 
C language
C language C language
C language
 
Html and css presentation
Html and css presentationHtml and css presentation
Html and css presentation
 
Html Presentation
Html PresentationHtml Presentation
Html Presentation
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Oops and c fundamentals
Oops and c fundamentals Oops and c fundamentals
Oops and c fundamentals
 
Java script
Java scriptJava script
Java script
 
css and wordpress
css and wordpresscss and wordpress
css and wordpress
 
css and wordpress
css and wordpresscss and wordpress
css and wordpress
 
Php vs asp
Php vs aspPhp vs asp
Php vs asp
 
Ccna security
Ccna security Ccna security
Ccna security
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
Cloud computing
Cloud computingCloud computing
Cloud computing
 
C language
C languageC language
C language
 

Recently uploaded

Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and RemediesArdra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Astro Pathshala
 
How to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 NotebookHow to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 Notebook
Celine George
 
ENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUM
ENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUMENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUM
ENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUM
HappieMontevirgenCas
 
Final_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptx
Final_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptxFinal_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptx
Final_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptx
shimeathdelrosario1
 
Credit limit improvement system in odoo 17
Credit limit improvement system in odoo 17Credit limit improvement system in odoo 17
Credit limit improvement system in odoo 17
Celine George
 
The basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptxThe basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptx
heathfieldcps1
 
NLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacherNLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacher
AngelicaLubrica
 
Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024
Elizabeth Walsh
 
Book Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docxBook Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docx
drtech3715
 
Front Desk Management in the Odoo 17 ERP
Front Desk  Management in the Odoo 17 ERPFront Desk  Management in the Odoo 17 ERP
Front Desk Management in the Odoo 17 ERP
Celine George
 
How to Configure Time Off Types in Odoo 17
How to Configure Time Off Types in Odoo 17How to Configure Time Off Types in Odoo 17
How to Configure Time Off Types in Odoo 17
Celine George
 
NLC Grade 3.................................... ppt.pptx
NLC Grade 3.................................... ppt.pptxNLC Grade 3.................................... ppt.pptx
NLC Grade 3.................................... ppt.pptx
MichelleDeLaCruz93
 
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdfARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
DharmarajPawar
 
AI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdfAI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdf
SrimanigandanMadurai
 
How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17
Celine George
 
BRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptx
BRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptxBRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptx
BRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptx
kambal1234567890
 
Split Shifts From Gantt View in the Odoo 17
Split Shifts From Gantt View in the  Odoo 17Split Shifts From Gantt View in the  Odoo 17
Split Shifts From Gantt View in the Odoo 17
Celine George
 
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Murugan Solaiyappan
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
heathfieldcps1
 

Recently uploaded (20)

Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and RemediesArdra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
Ardra Nakshatra (आर्द्रा): Understanding its Effects and Remedies
 
How to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 NotebookHow to Add Colour Kanban Records in Odoo 17 Notebook
How to Add Colour Kanban Records in Odoo 17 Notebook
 
ENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUM
ENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUMENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUM
ENGLISH-7-CURRICULUM MAP- MATATAG CURRICULUM
 
Final_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptx
Final_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptxFinal_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptx
Final_SD_Session3_Ferriols, Ador Dionisio, Fajardo.pptx
 
“A NOSSA CA(U)SA”. .
“A NOSSA CA(U)SA”.                      .“A NOSSA CA(U)SA”.                      .
“A NOSSA CA(U)SA”. .
 
Credit limit improvement system in odoo 17
Credit limit improvement system in odoo 17Credit limit improvement system in odoo 17
Credit limit improvement system in odoo 17
 
The basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptxThe basics of sentences session 9pptx.pptx
The basics of sentences session 9pptx.pptx
 
NLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacherNLC English 7 Consolidation Lesson plan for teacher
NLC English 7 Consolidation Lesson plan for teacher
 
Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024Howe Writing Center - Orientation Summer 2024
Howe Writing Center - Orientation Summer 2024
 
Book Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docxBook Allied Health Sciences kmu MCQs.docx
Book Allied Health Sciences kmu MCQs.docx
 
Front Desk Management in the Odoo 17 ERP
Front Desk  Management in the Odoo 17 ERPFront Desk  Management in the Odoo 17 ERP
Front Desk Management in the Odoo 17 ERP
 
How to Configure Time Off Types in Odoo 17
How to Configure Time Off Types in Odoo 17How to Configure Time Off Types in Odoo 17
How to Configure Time Off Types in Odoo 17
 
NLC Grade 3.................................... ppt.pptx
NLC Grade 3.................................... ppt.pptxNLC Grade 3.................................... ppt.pptx
NLC Grade 3.................................... ppt.pptx
 
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdfARCHITECTURAL PATTERNS IN HISTOPATHOLOGY  pdf-  [Autosaved].pdf
ARCHITECTURAL PATTERNS IN HISTOPATHOLOGY pdf- [Autosaved].pdf
 
AI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdfAI_in_HR_Presentation Part 1 2024 0703.pdf
AI_in_HR_Presentation Part 1 2024 0703.pdf
 
How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17How to Show Sample Data in Tree and Kanban View in Odoo 17
How to Show Sample Data in Tree and Kanban View in Odoo 17
 
BRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptx
BRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptxBRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptx
BRIGADA ESKWELA OPENING PROGRAM KICK OFF.pptx
 
Split Shifts From Gantt View in the Odoo 17
Split Shifts From Gantt View in the  Odoo 17Split Shifts From Gantt View in the  Odoo 17
Split Shifts From Gantt View in the Odoo 17
 
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
Lecture_Notes_Unit4_Chapter_8_9_10_RDBMS for the students affiliated by alaga...
 
The basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptxThe basics of sentences session 10pptx.pptx
The basics of sentences session 10pptx.pptx
 

Function in c program

  • 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. #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 The variable can be modified inside the
  • 21. #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", x,y); getch(); return 0; }
  • 22. 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; }