SlideShare a Scribd company logo
1 of 10
Modularizing and Reusing of code through Functions

Calculation of area of Circle is separated into a separate module from Calculation of area of
Ring and the same module can be reused for multiple times.


 /* program to find area of a ring                   /* program to find area of a ring */
 */                                                  #include<stdio.h>
 #include<stdio.h>                                   float area();         Function Declaration
 int main()              Repeated & Reusable         int main()
 {                           blocks of code          {
    float a1,a2,a,r1,r2;                                float a1,a2,a;
                                                        a1 = area();        Function Calls
    printf("Enter the radius : ");
                                                        a2 = area();
    scanf("%f",&r1);                                    a = a1- a2;
    a1 = 3.14*r1*r1;                                    printf("Area of Ring : %.3fn", a);
    printf("Enter the radius : ");                   }
    scanf("%f",&r2);                                 float area()         Function Definition
    a2 = 3.14*r2*r2;                                 {
    a = a1- a2;                                         float r;
    printf("Area of Ring : %.3fn",                     printf("Enter the radius : ");
                                                        scanf("%f", &r);
 a);
                                                        return (3.14*r*r);
 }                                                   }
A Function is an independent, reusable module of statements, that specified by a name.
 This module (sub program) can be called by it’s name to do a specific task. We can call the
 function, for any number of times and from anywhere in the program. The purpose of a function
 is to receive zero or more pieces of data, operate on them, and return at most one piece of
 data.
        A Called Function receives control from a Calling Function. When the called function
 completes its task, it returns control to the calling function. It may or may not return a value to
 the caller. The function main() is called by the operating system; main() calls other functions.
 When main() is complete, control returns to the operating system.

                                         value of ‘p’ is copied to loan’
                                                value of ‘n’ is copied to terms’
int main() {
                                                                      value of ‘r’ is copied to ‘iRate’
  int n;
  float p, r, si;
  printf(“Enter Details of Loan1:“);      float calcInterest(float loan , int terms , float iRate )
                                          {
  scanf( “%f %d %f”, &p, &n, &r);
                                              float interest;                            The block is
  si =calcInterest( p, n , r );               interest = ( loan * terms * iRate )/100;   executed
  printf(“Interest : Rs. %f”, si);            return ( interest );
  printf(“Enter Details of Loan2:“);      }
}
                                                                            Called Function
                    value of ‘interest’ is assigned to ‘si ’

              Calling Function                     Process of Execution for a Function Call
int main()
            {
                int n1, n2;
                printf("Enter a number : ");
                scanf("%d",&n1);
                printOctal(n1);
                readPrintHexa();
                printf("Enter a number : ");
                scanf("%d",&n2);                1
        2       printOctal(n2);
                printf(“n”);
            }                                             3   7
                                                                       Flow of
    8       void printOctal(int n)                                     Control
            {
                 printf("Number in octal form : %o n", n);                in
            }
                                                                    Multi-Function
6           void readPrintHexa()                                      Program
            {
                 int num;
                 printf("Enter a number : ");
                 scanf("%d",&num);
                 printHexa(num);
                 printf(“n”);
            }                       4
            void printHexa(int n)
        5   {
                 printf("Number in Hexa-Decimal form : %x n",n);
            }
/* Program demonstrates function calls */    Function-It’s Terminology
#include<stdio.h>
int add ( int n1, int n2 ) ;                         Function Name
int main(void)
{                                             Declaration (proto type) of Function
    int a, b, sum;
    printf(“Enter two integers : ”);               Formal Parameters
    scanf(“%d %d”, &a, &b);
                                                      Function Call
      sum = add ( a , b ) ;
      printf(“%d + %d = %dn”, a, b, sum);
      return 0;                                     Actual Arguments
}
                                                       Return Type
/* adds two numbers and return the sum */
                                                  Definition of Function
int    add ( int x , int y   )
{                                             Parameter List used in the Function
      int s;
      s = x + y;                             Return statement of the Function
      return ( s );
}                                                       Return Value
Categories of Functions
/* using different functions */            void printMyLine()      Function with No parameters
int main()                                 {                             and No return value
                                              int i;
{
                                              for(i=1; i<=35;i++) printf(“%c”, ‘-’);
   float radius, area;                        printf(“n”);
   printMyLine();                          }
   printf(“ntUsage of functionsn”);
   printYourLine(‘-’,35);                  void printYourLine(char ch, int n)
   radius = readRadius();                  {
                                                                   Function with parameters
   area = calcArea ( radius );                                         and No return value
   printf(“Area of Circle = %f”,               int i;
                                              for(i=1; i<=n ;i++) printf(“%c”, ch);
area);
                                              printf(“n”);
}                                          }

                                                  float calcArea(float r)
 float readRadius()       Function with return    {                       Function with return
 {                       value & No parameters
     float r;                                         float a;           value and parameters
     printf(“Enter the radius : “);                   a = 3.14 * r * r ;
     scanf(“%f”, &r);                                 return ( a ) ;
     return ( r );                                }
 }
                                                 Note: ‘void’ means “Containing nothing”
Static Local Variables
 #include<stdio.h>                           void area()
                                                                      Visible with in the function,
 float length, breadth;                      {                        created only once when
 int main()                                    static int num = 0;    function is called at first
 {                                                                    time and exists between
    printf("Enter length, breadth : ");          float a;             function calls.
    scanf("%f %f",&length,&breadth);             num++;
    area();                                      a = (length * breadth);
    perimeter();                                 printf(“nArea of Rectangle %d : %.2f", num, a);
    printf(“nEnter length, breadth: ");     }
    scanf("%f %f",&length,&breadth);
    area();                                  void perimeter()
    perimeter();                             {
 }                                             int no = 0;
                                               float p;
                                               no++;
       External Global Variables
                                               p = 2 *(length + breadth);
Scope:     Visible   across     multiple
                                               printf(“Perimeter of Rectangle %d: %.2f",no,p);
functions Lifetime: exists till the end
                                             }
of the program.

                                                       Automatic Local Variables
Enter length, breadth : 6 4
                                           Scope : visible with in the function.
Area of Rectangle 1 : 24.00
                                           Lifetime: re-created for every function call and
Perimeter of Rectangle 1 : 20.00
                                           destroyed automatically when function is exited.
Enter length, breadth : 8 5
Area of Rectangle 2 : 40.00
Perimeter of Rectangle 1 : 26.00                 Storage Classes – Scope & Lifetime
File1.c                                             File2.c
#include<stdio.h>
                                                    extern float length, breadth ;
float length, breadth;
                                                    /* extern base , height ; --- error */
                                                    float rectanglePerimeter()
static float base, height;
                                                    {
int main()
                                                       float p;
{
                                                       p = 2 *(length + breadth);
   float peri;
                                                       return ( p );
   printf("Enter length, breadth : ");
                                                    }
   scanf("%f %f",&length,&breadth);
   rectangleArea();
   peri = rectanglePerimeter();
                                                             External Global Variables
   printf(“Perimeter of Rectangle : %f“, peri);
                                                    Scope: Visible to all functions across all
   printf(“nEnter base , height: ");
                                                    files in the project.
   scanf("%f %f",&base,&height);
                                                    Lifetime: exists      till the end of the
   triangleArea();
                                                    program.
}
void rectangleArea() {
  float a;
                                                              Static Global Variables
  a = length * breadth;
                                                    Scope: Visible to all functions with in
  printf(“nArea of Rectangle : %.2f", a);
                                                    the file only.
}
                                                    Lifetime: exists     till the end of the
void triangleArea() {
                                                    program.
  float a;
  a = 0.5 * base * height ;
  printf(“nArea of Triangle : %.2f", a);         Storage Classes – Scope & Lifetime
}
#include<stdio.h>                                       Preprocessor Directives
void showSquares(int n)      A function
{                            calling itself   #define  - Define a macro substitution
    if(n == 0)                                #undef   - Undefines a macro
                                   is         #ifdef   - Test for a macro definition
       return;                Recursion       #ifndef  - Tests whether a macro is not
    else
                                                         defined
       showSquares(n-1);                      #include - Specifies the files to be included
    printf(“%d “, (n*n));                     #if      - Test a compile-time condition
}                                             #else    - Specifies alternatives when #if
int main()                                               test fails
{                                             #elif    - Provides alternative test facility
   showSquares(5);                            #endif   - Specifies the end of #if
}                                             #pragma - Specifies certain instructions
                                              #error   - Stops compilation when an error
                                                         occurs
Output : 1 4 9 16 25
                                              #        - Stringizing operator
addition    showSquares(1)       execution    ##       - Token-pasting operator
  of        showSquares(2)           of
function                          function
  calls     showSquares(3)          calls          Preprocessor is a program that
   to       showSquares(4)           in          processes the source code before it
  call-                           reverse           passes through the compiler.
 stack      showSquares(5)
                main()         call-stack
For More Materials, Text Books,
Previous Papers & Mobile updates
of B.TECH, B.PHARMACY, MBA,
MCA of JNTU-HYD,JNTU-
KAKINADA & JNTU-ANANTAPUR
visit www.jntuworld.com
For More Materials, Text Books,
Previous Papers & Mobile updates
of B.TECH, B.PHARMACY, MBA,
MCA of JNTU-HYD,JNTU-
KAKINADA & JNTU-ANANTAPUR
visit www.jntuworld.com

More Related Content

What's hot

C programming function
C  programming functionC  programming function
C programming functionargusacademy
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumpingMomenMostafa
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of cTushar B Kute
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5alish sha
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 FocJAYA
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branchingSaranya saran
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string libraryMomenMostafa
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]Abhishek Sinha
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c languageMomenMostafa
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in cSaranya saran
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’eShikshak
 
Functions in c
Functions in cFunctions in c
Functions in cInnovative
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statementsMomenMostafa
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1AmIt Prasad
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
 

What's hot (20)

C programming function
C  programming functionC  programming function
C programming function
 
Unit2 C
Unit2 CUnit2 C
Unit2 C
 
7 functions
7  functions7  functions
7 functions
 
6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping6 c control statements branching &amp; jumping
6 c control statements branching &amp; jumping
 
lets play with "c"..!!! :):)
lets play with "c"..!!! :):)lets play with "c"..!!! :):)
lets play with "c"..!!! :):)
 
An imperative study of c
An imperative study of cAn imperative study of c
An imperative study of c
 
Dti2143 chapter 5
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
 
C programming
C programmingC programming
C programming
 
Unit 5 Foc
Unit 5 FocUnit 5 Foc
Unit 5 Foc
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
9 character string &amp; string library
9  character string &amp; string library9  character string &amp; string library
9 character string &amp; string library
 
Concepts of C [Module 2]
Concepts of C [Module 2]Concepts of C [Module 2]
Concepts of C [Module 2]
 
1 introducing c language
1  introducing c language1  introducing c language
1 introducing c language
 
Expressions using operator in c
Expressions using operator in cExpressions using operator in c
Expressions using operator in c
 
Mesics lecture 5 input – output in ‘c’
Mesics lecture 5   input – output in ‘c’Mesics lecture 5   input – output in ‘c’
Mesics lecture 5 input – output in ‘c’
 
Functions in c
Functions in cFunctions in c
Functions in c
 
4 operators, expressions &amp; statements
4  operators, expressions &amp; statements4  operators, expressions &amp; statements
4 operators, expressions &amp; statements
 
46630497 fun-pointer-1
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
 
C Programming Language Part 7
C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
 
Input And Output
 Input And Output Input And Output
Input And Output
 

Viewers also liked

ประวัติภาษาซี
ประวัติภาษาซีประวัติภาษาซี
ประวัติภาษาซีTharathep Chumchuen
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfilesmrecedu
 
Gerencia de proyectos
Gerencia de proyectos Gerencia de proyectos
Gerencia de proyectos Moralespaola
 
Finance consultant perfomance appraisal 2
Finance consultant perfomance appraisal 2Finance consultant perfomance appraisal 2
Finance consultant perfomance appraisal 2tonychoper4604
 
Activities for all 5 C's During Small Group Centers
Activities for all 5 C's During Small Group CentersActivities for all 5 C's During Small Group Centers
Activities for all 5 C's During Small Group Centersjhaxhi
 

Viewers also liked (10)

ประวัติภาษาซี
ประวัติภาษาซีประวัติภาษาซี
ประวัติภาษาซี
 
Unit3 jwfiles
Unit3 jwfilesUnit3 jwfiles
Unit3 jwfiles
 
ความหมาย
ความหมายความหมาย
ความหมาย
 
Yazdani Dental
 Yazdani Dental Yazdani Dental
Yazdani Dental
 
Gerencia de proyectos
Gerencia de proyectos Gerencia de proyectos
Gerencia de proyectos
 
Finance consultant perfomance appraisal 2
Finance consultant perfomance appraisal 2Finance consultant perfomance appraisal 2
Finance consultant perfomance appraisal 2
 
Activities for all 5 C's During Small Group Centers
Activities for all 5 C's During Small Group CentersActivities for all 5 C's During Small Group Centers
Activities for all 5 C's During Small Group Centers
 
Can Nature Solve It?
Can Nature Solve It?Can Nature Solve It?
Can Nature Solve It?
 
La florida (1)
La florida (1)La florida (1)
La florida (1)
 
Adopción y tdah
Adopción y tdahAdopción y tdah
Adopción y tdah
 

Similar to Unit2 jwfiles

Similar to Unit2 jwfiles (20)

Unit2 C
Unit2 C Unit2 C
Unit2 C
 
Function in C program
Function in C programFunction in C program
Function in C program
 
Functions struct&union
Functions struct&unionFunctions struct&union
Functions struct&union
 
Unit 4 (1)
Unit 4 (1)Unit 4 (1)
Unit 4 (1)
 
Function in c program
Function in c programFunction in c program
Function in c program
 
Array Cont
Array ContArray Cont
Array Cont
 
Function in c
Function in cFunction in c
Function in c
 
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
โปรแกรมย่อยและฟังชั่นมาตรฐาน ม.6 1
 
Functions
FunctionsFunctions
Functions
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
function_v1.ppt
function_v1.pptfunction_v1.ppt
function_v1.ppt
 
Functions in C
Functions in CFunctions in C
Functions in C
 
L25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptxL25-L26-Parameter passing techniques.pptx
L25-L26-Parameter passing techniques.pptx
 
CHAPTER 6
CHAPTER 6CHAPTER 6
CHAPTER 6
 
Function in c
Function in cFunction in c
Function in c
 
Fucntions & Pointers in C
Fucntions & Pointers in CFucntions & Pointers in C
Fucntions & Pointers in C
 
function in in thi pdf you will learn what is fu...
function in  in thi pdf you will learn   what                           is fu...function in  in thi pdf you will learn   what                           is fu...
function in in thi pdf you will learn what is fu...
 
presentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.pptpresentation_functions_1443207686_140676.ppt
presentation_functions_1443207686_140676.ppt
 
C function
C functionC function
C function
 
C- Programming Assignment 4 solution
C- Programming Assignment 4 solutionC- Programming Assignment 4 solution
C- Programming Assignment 4 solution
 

More from mrecedu

Brochure final
Brochure finalBrochure final
Brochure finalmrecedu
 
Filters unit iii
Filters unit iiiFilters unit iii
Filters unit iiimrecedu
 
Attenuator unit iv
Attenuator unit ivAttenuator unit iv
Attenuator unit ivmrecedu
 
Two port networks unit ii
Two port networks unit iiTwo port networks unit ii
Two port networks unit iimrecedu
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)mrecedu
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)mrecedu
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfilesmrecedu
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfilesmrecedu
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfilesmrecedu
 
M1 unit vi-jntuworld
M1 unit vi-jntuworldM1 unit vi-jntuworld
M1 unit vi-jntuworldmrecedu
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworldmrecedu
 
M1 unit iv-jntuworld
M1 unit iv-jntuworldM1 unit iv-jntuworld
M1 unit iv-jntuworldmrecedu
 
M1 unit iii-jntuworld
M1 unit iii-jntuworldM1 unit iii-jntuworld
M1 unit iii-jntuworldmrecedu
 
M1 unit ii-jntuworld
M1 unit ii-jntuworldM1 unit ii-jntuworld
M1 unit ii-jntuworldmrecedu
 
M1 unit i-jntuworld
M1 unit i-jntuworldM1 unit i-jntuworld
M1 unit i-jntuworldmrecedu
 
M1 unit viii-jntuworld
M1 unit viii-jntuworldM1 unit viii-jntuworld
M1 unit viii-jntuworldmrecedu
 

More from mrecedu (20)

Brochure final
Brochure finalBrochure final
Brochure final
 
Unit i
Unit iUnit i
Unit i
 
Filters unit iii
Filters unit iiiFilters unit iii
Filters unit iii
 
Attenuator unit iv
Attenuator unit ivAttenuator unit iv
Attenuator unit iv
 
Two port networks unit ii
Two port networks unit iiTwo port networks unit ii
Two port networks unit ii
 
Unit 8
Unit 8Unit 8
Unit 8
 
Unit4 (2)
Unit4 (2)Unit4 (2)
Unit4 (2)
 
Unit5
Unit5Unit5
Unit5
 
Unit4
Unit4Unit4
Unit4
 
Unit5 (2)
Unit5 (2)Unit5 (2)
Unit5 (2)
 
Unit6 jwfiles
Unit6 jwfilesUnit6 jwfiles
Unit6 jwfiles
 
Unit1 jwfiles
Unit1 jwfilesUnit1 jwfiles
Unit1 jwfiles
 
Unit7 jwfiles
Unit7 jwfilesUnit7 jwfiles
Unit7 jwfiles
 
M1 unit vi-jntuworld
M1 unit vi-jntuworldM1 unit vi-jntuworld
M1 unit vi-jntuworld
 
M1 unit v-jntuworld
M1 unit v-jntuworldM1 unit v-jntuworld
M1 unit v-jntuworld
 
M1 unit iv-jntuworld
M1 unit iv-jntuworldM1 unit iv-jntuworld
M1 unit iv-jntuworld
 
M1 unit iii-jntuworld
M1 unit iii-jntuworldM1 unit iii-jntuworld
M1 unit iii-jntuworld
 
M1 unit ii-jntuworld
M1 unit ii-jntuworldM1 unit ii-jntuworld
M1 unit ii-jntuworld
 
M1 unit i-jntuworld
M1 unit i-jntuworldM1 unit i-jntuworld
M1 unit i-jntuworld
 
M1 unit viii-jntuworld
M1 unit viii-jntuworldM1 unit viii-jntuworld
M1 unit viii-jntuworld
 

Recently uploaded

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...apidays
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesSinan KOZAK
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Allon Mureinik
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Enterprise Knowledge
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024Results
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024The Digital Insurer
 

Recently uploaded (20)

Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Unblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen FramesUnblocking The Main Thread Solving ANRs and Frozen Frames
Unblocking The Main Thread Solving ANRs and Frozen Frames
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)Injustice - Developers Among Us (SciFiDevCon 2024)
Injustice - Developers Among Us (SciFiDevCon 2024)
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024A Call to Action for Generative AI in 2024
A Call to Action for Generative AI in 2024
 
Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024Partners Life - Insurer Innovation Award 2024
Partners Life - Insurer Innovation Award 2024
 

Unit2 jwfiles

  • 1. Modularizing and Reusing of code through Functions Calculation of area of Circle is separated into a separate module from Calculation of area of Ring and the same module can be reused for multiple times. /* program to find area of a ring /* program to find area of a ring */ */ #include<stdio.h> #include<stdio.h> float area(); Function Declaration int main() Repeated & Reusable int main() { blocks of code { float a1,a2,a,r1,r2; float a1,a2,a; a1 = area(); Function Calls printf("Enter the radius : "); a2 = area(); scanf("%f",&r1); a = a1- a2; a1 = 3.14*r1*r1; printf("Area of Ring : %.3fn", a); printf("Enter the radius : "); } scanf("%f",&r2); float area() Function Definition a2 = 3.14*r2*r2; { a = a1- a2; float r; printf("Area of Ring : %.3fn", printf("Enter the radius : "); scanf("%f", &r); a); return (3.14*r*r); } }
  • 2. A Function is an independent, reusable module of statements, that specified by a name. This module (sub program) can be called by it’s name to do a specific task. We can call the function, for any number of times and from anywhere in the program. The purpose of a function is to receive zero or more pieces of data, operate on them, and return at most one piece of data. A Called Function receives control from a Calling Function. When the called function completes its task, it returns control to the calling function. It may or may not return a value to the caller. The function main() is called by the operating system; main() calls other functions. When main() is complete, control returns to the operating system. value of ‘p’ is copied to loan’ value of ‘n’ is copied to terms’ int main() { value of ‘r’ is copied to ‘iRate’ int n; float p, r, si; printf(“Enter Details of Loan1:“); float calcInterest(float loan , int terms , float iRate ) { scanf( “%f %d %f”, &p, &n, &r); float interest; The block is si =calcInterest( p, n , r ); interest = ( loan * terms * iRate )/100; executed printf(“Interest : Rs. %f”, si); return ( interest ); printf(“Enter Details of Loan2:“); } } Called Function value of ‘interest’ is assigned to ‘si ’ Calling Function Process of Execution for a Function Call
  • 3. int main() { int n1, n2; printf("Enter a number : "); scanf("%d",&n1); printOctal(n1); readPrintHexa(); printf("Enter a number : "); scanf("%d",&n2); 1 2 printOctal(n2); printf(“n”); } 3 7 Flow of 8 void printOctal(int n) Control { printf("Number in octal form : %o n", n); in } Multi-Function 6 void readPrintHexa() Program { int num; printf("Enter a number : "); scanf("%d",&num); printHexa(num); printf(“n”); } 4 void printHexa(int n) 5 { printf("Number in Hexa-Decimal form : %x n",n); }
  • 4. /* Program demonstrates function calls */ Function-It’s Terminology #include<stdio.h> int add ( int n1, int n2 ) ; Function Name int main(void) { Declaration (proto type) of Function int a, b, sum; printf(“Enter two integers : ”); Formal Parameters scanf(“%d %d”, &a, &b); Function Call sum = add ( a , b ) ; printf(“%d + %d = %dn”, a, b, sum); return 0; Actual Arguments } Return Type /* adds two numbers and return the sum */ Definition of Function int add ( int x , int y ) { Parameter List used in the Function int s; s = x + y; Return statement of the Function return ( s ); } Return Value
  • 5. Categories of Functions /* using different functions */ void printMyLine() Function with No parameters int main() { and No return value int i; { for(i=1; i<=35;i++) printf(“%c”, ‘-’); float radius, area; printf(“n”); printMyLine(); } printf(“ntUsage of functionsn”); printYourLine(‘-’,35); void printYourLine(char ch, int n) radius = readRadius(); { Function with parameters area = calcArea ( radius ); and No return value printf(“Area of Circle = %f”, int i; for(i=1; i<=n ;i++) printf(“%c”, ch); area); printf(“n”); } } float calcArea(float r) float readRadius() Function with return { Function with return { value & No parameters float r; float a; value and parameters printf(“Enter the radius : “); a = 3.14 * r * r ; scanf(“%f”, &r); return ( a ) ; return ( r ); } } Note: ‘void’ means “Containing nothing”
  • 6. Static Local Variables #include<stdio.h> void area() Visible with in the function, float length, breadth; { created only once when int main() static int num = 0; function is called at first { time and exists between printf("Enter length, breadth : "); float a; function calls. scanf("%f %f",&length,&breadth); num++; area(); a = (length * breadth); perimeter(); printf(“nArea of Rectangle %d : %.2f", num, a); printf(“nEnter length, breadth: "); } scanf("%f %f",&length,&breadth); area(); void perimeter() perimeter(); { } int no = 0; float p; no++; External Global Variables p = 2 *(length + breadth); Scope: Visible across multiple printf(“Perimeter of Rectangle %d: %.2f",no,p); functions Lifetime: exists till the end } of the program. Automatic Local Variables Enter length, breadth : 6 4 Scope : visible with in the function. Area of Rectangle 1 : 24.00 Lifetime: re-created for every function call and Perimeter of Rectangle 1 : 20.00 destroyed automatically when function is exited. Enter length, breadth : 8 5 Area of Rectangle 2 : 40.00 Perimeter of Rectangle 1 : 26.00 Storage Classes – Scope & Lifetime
  • 7. File1.c File2.c #include<stdio.h> extern float length, breadth ; float length, breadth; /* extern base , height ; --- error */ float rectanglePerimeter() static float base, height; { int main() float p; { p = 2 *(length + breadth); float peri; return ( p ); printf("Enter length, breadth : "); } scanf("%f %f",&length,&breadth); rectangleArea(); peri = rectanglePerimeter(); External Global Variables printf(“Perimeter of Rectangle : %f“, peri); Scope: Visible to all functions across all printf(“nEnter base , height: "); files in the project. scanf("%f %f",&base,&height); Lifetime: exists till the end of the triangleArea(); program. } void rectangleArea() { float a; Static Global Variables a = length * breadth; Scope: Visible to all functions with in printf(“nArea of Rectangle : %.2f", a); the file only. } Lifetime: exists till the end of the void triangleArea() { program. float a; a = 0.5 * base * height ; printf(“nArea of Triangle : %.2f", a); Storage Classes – Scope & Lifetime }
  • 8. #include<stdio.h> Preprocessor Directives void showSquares(int n) A function { calling itself #define - Define a macro substitution if(n == 0) #undef - Undefines a macro is #ifdef - Test for a macro definition return; Recursion #ifndef - Tests whether a macro is not else defined showSquares(n-1); #include - Specifies the files to be included printf(“%d “, (n*n)); #if - Test a compile-time condition } #else - Specifies alternatives when #if int main() test fails { #elif - Provides alternative test facility showSquares(5); #endif - Specifies the end of #if } #pragma - Specifies certain instructions #error - Stops compilation when an error occurs Output : 1 4 9 16 25 # - Stringizing operator addition showSquares(1) execution ## - Token-pasting operator of showSquares(2) of function function calls showSquares(3) calls Preprocessor is a program that to showSquares(4) in processes the source code before it call- reverse passes through the compiler. stack showSquares(5) main() call-stack
  • 9. For More Materials, Text Books, Previous Papers & Mobile updates of B.TECH, B.PHARMACY, MBA, MCA of JNTU-HYD,JNTU- KAKINADA & JNTU-ANANTAPUR visit www.jntuworld.com
  • 10. For More Materials, Text Books, Previous Papers & Mobile updates of B.TECH, B.PHARMACY, MBA, MCA of JNTU-HYD,JNTU- KAKINADA & JNTU-ANANTAPUR visit www.jntuworld.com