Functions in C

Shobhit Upadhyay
Shobhit UpadhyaySenior Software Engineer
By: Shobhit Upadhyay
Functions
 A function is a self contained block of code that performs a
    particular task.
   Any C program can be seen as a collection/group of these
    functions.
   A functions takes some data as input, perform some
    operation on that data and then return a value.
   Any C program must contain at least one function, which is
    main().
   There is no limit on the number of functions that might be
    present in a C program.
 For ex.
       main()
       {
                message();
                printf(“I am in main”);
       }
       message()
       {
              printf(“I am in message”);
       }
Function Prototype
 All Identifiers in C must be declared before they are used. This is true
    for functions as well as variables.
   For functions, the declarations needs to be done before the first call of
    the function.
   A function declaration specifies the name, return type, and arguments
    of a function. This is also called the function prototype.
   To be a prototype, a function declaration must establish types for the
    function’s arguments.
   Having the prototype available before the first use of the function
    allows the compiler to check that the correct number and types of
    arguments are used in the function call.
 The prototype has the same syntax as the function
  definition, except that it is terminated by a semicolon
  following the closing parenthesis and therefore has no
  body.

 Although, functions that return int values do not require
  prototypes, prototypes are recommended.
Function Definition
 General form of any function definition is:
       return-type function-name(argument declarations)
       {
              declarations and statements
       }
 Return-type refers to the data type of the value being
  returned from the function. If the return type is omitted,
  int is assumed.
 The values provided to a function for processing are the
  arguments.
 The set of statements between the braces is called as the
  function body.
Arguments – Call by Value
 In C, all functions are passed “by value” by default.
 This means that the called function is given the values of its
  arguments in temporary variables rather than the originals.
 For ex.    main()
             {
                    int a=4,b=5;
                    sum(a,b);
                    printf(“Sum = %d”,a+b);
             }
             sum(int a,int b)
             {
                   a++;
                   b++;
                   printf(“Sum = %d”,a+b);
             }
Arguments – Call by Reference
 When necessary, it is possible to arrange a function which can modify a
  variable in a calling routine.
 The caller must provide the address of the variable to be set (generally, a
  pointer to a variable), and the called function must declare the parameter to
  be a pointer and access the variable indirectly through it.
 For ex:         main()
                 {
                          int a=4,b=5;
                          sum(&a,&b);
                          printf(“Sum = %d”,a+b);
                 }
                 sum(int *a,int *b)
                 {
                        (*a)++;
                        (*b)++;
                        printf(“Sum = %d”,(*a)+(*b));
                 }
Recursion
 Recursion defines a function in terms of itself.
 It is a programming technique in which a function calls
  itself.
 A recursive function calls itself repeatedly, with different
  argument values each time.
 Some argument values cause the recursive method to
  return without calling itself. This is the base case.
 Either omitting the base case or writing the recursion step
  incorrectly will cause infinite recursion (stack overflow
  error).
 Recursion to find the factorial of any number:
      int factorial(int x)
      {
            if(x<=1)
            return 1;
            else
            return(x*factorial(x-1));
      }
External Variables
 See the below example:
      main()
      {
            extern int a;
            printf(“Value of a = %d”,a);
      }
      int a=5;


Output: Value   of a = 5
Scope Rules
 Example:
       int num1 = 100;               //Global Scope
       static int num2 = 200;        //File Scope
       int main()
       {
             int num3 = 300;         //Local Scope
             if(num2>num1)
             {
                   int i=0;          //Block Scope
                   printf(“Value of i = %dn”,i++);
             }
             printf(“Value of num1 = %dn”,num1);
             printf(“Value of num2 = %dn”,num2);
             printf(“Value of num3 = %dn”,num3);
       }
Static Variables
 Example:
      static int min = 10;
      int setmax()
      {
             static int max =   100;
             return ++max;
      }
      main()
      {
             min++;
             printf(“Value of   max = %d”, setmax());
             printf(“Value of   max = %d”, setmax());
             printf(“Value of   min = %d”, min);
      }
Functions in C
1 de 14

Recomendados

Functions in cFunctions in c
Functions in csunila tharagaturi
1.5K vistas42 diapositivas
Function in cFunction in c
Function in csavitamhaske
1.3K vistas18 diapositivas
Function in cFunction in c
Function in cRaj Tandukar
6.8K vistas22 diapositivas
C functionsC functions
C functionsUniversity of Potsdam
9.9K vistas32 diapositivas
Functions in CFunctions in C
Functions in CKamal Acharya
25.1K vistas22 diapositivas
Function in c programFunction in c program
Function in c programumesh patil
1.3K vistas23 diapositivas

Más contenido relacionado

La actualidad más candente

Function in CFunction in C
Function in CDr. Abhineet Anand
10.2K vistas31 diapositivas
Constants in C ProgrammingConstants in C Programming
Constants in C Programmingprogramming9
6.2K vistas11 diapositivas
RECURSION IN C RECURSION IN C
RECURSION IN C v_jk
13.9K vistas35 diapositivas
Functions in c language Functions in c language
Functions in c language tanmaymodi4
2.5K vistas30 diapositivas

La actualidad más candente(20)

Function in CFunction in C
Function in C
Dr. Abhineet Anand10.2K vistas
Constants in C ProgrammingConstants in C Programming
Constants in C Programming
programming96.2K vistas
RECURSION IN C RECURSION IN C
RECURSION IN C
v_jk13.9K vistas
Functions in c language Functions in c language
Functions in c language
tanmaymodi42.5K vistas
Presentation on Function in C ProgrammingPresentation on Function in C Programming
Presentation on Function in C Programming
Shuvongkor Barman14.2K vistas
User defined functions in C programmigUser defined functions in C programmig
User defined functions in C programmig
Appili Vamsi Krishna13K vistas
Function in C ProgrammingFunction in C Programming
Function in C Programming
Anil Pokhrel161 vistas
data types in C programmingdata types in C programming
data types in C programming
Harshita Yadav15.5K vistas
Function overloading(c++)Function overloading(c++)
Function overloading(c++)
Ritika Sharma14.5K vistas
Structure of a C programStructure of a C program
Structure of a C program
David Livingston J22K vistas
Basics of C programmingBasics of C programming
Basics of C programming
avikdhupar164.8K vistas
Inline functionInline function
Inline function
Tech_MX14.2K vistas
FUNCTIONS IN c++ PPTFUNCTIONS IN c++ PPT
FUNCTIONS IN c++ PPT
0306267992925.3K vistas
FunctionFunction
Function
jayesh30sikchi1.3K vistas
Call by valueCall by value
Call by value
Dharani G8.6K vistas
C if elseC if else
C if else
Ritwik Das13.7K vistas
User defined functionsUser defined functions
User defined functions
Rokonuzzaman Rony1.2K vistas
C stringC string
C string
University of Potsdam4.6K vistas
Pointers  C programmingPointers  C programming
Pointers C programming
Appili Vamsi Krishna13K vistas
Variables in C ProgrammingVariables in C Programming
Variables in C Programming
programming99K vistas

Similar a Functions in C

C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7Rumman Ansari
465 vistas23 diapositivas
UNIT3.pptxUNIT3.pptx
UNIT3.pptxNagasaiT
6 vistas39 diapositivas
Function in cFunction in c
Function in cCGC Technical campus,Mohali
655 vistas22 diapositivas
FunctionsFunctions
FunctionsSANTOSH RATH
365 vistas16 diapositivas

Similar a Functions in C(20)

C Programming Language Part 7C Programming Language Part 7
C Programming Language Part 7
Rumman Ansari465 vistas
UNIT3.pptxUNIT3.pptx
UNIT3.pptx
NagasaiT6 vistas
Function in cFunction in c
Function in c
CGC Technical campus,Mohali655 vistas
FunctionsFunctions
Functions
SANTOSH RATH365 vistas
Dti2143 chapter 5Dti2143 chapter 5
Dti2143 chapter 5
alish sha736 vistas
Functions struct&unionFunctions struct&union
Functions struct&union
UMA PARAMESWARI115 vistas
functionsfunctions
functions
Makwana Bhavesh243 vistas
C functionC function
C function
thirumalaikumar3493 vistas
Recursion in CRecursion in C
Recursion in C
v_jk741 vistas
Presentation on functionPresentation on function
Presentation on function
Abu Zaman11.2K vistas
CH.4FUNCTIONS IN C_FYBSC(CS).pptxCH.4FUNCTIONS IN C_FYBSC(CS).pptx
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
SangeetaBorde329 vistas
function_v1.pptfunction_v1.ppt
function_v1.ppt
ssuser82367812 vistas
Classes function overloadingClasses function overloading
Classes function overloading
ankush_kumar1.9K vistas
An imperative study of cAn imperative study of c
An imperative study of c
Tushar B Kute668 vistas
46630497 fun-pointer-146630497 fun-pointer-1
46630497 fun-pointer-1
AmIt Prasad253 vistas
Array ContArray Cont
Array Cont
Ashutosh Srivasatava456 vistas
Unit 4.pdfUnit 4.pdf
Unit 4.pdf
thenmozhip84 vistas
Functions and pointers_unit_4Functions and pointers_unit_4
Functions and pointers_unit_4
Saranya saran82 vistas

Functions in C

  • 2. Functions  A function is a self contained block of code that performs a particular task.  Any C program can be seen as a collection/group of these functions.  A functions takes some data as input, perform some operation on that data and then return a value.  Any C program must contain at least one function, which is main().  There is no limit on the number of functions that might be present in a C program.
  • 3.  For ex. main() { message(); printf(“I am in main”); } message() { printf(“I am in message”); }
  • 4. Function Prototype  All Identifiers in C must be declared before they are used. This is true for functions as well as variables.  For functions, the declarations needs to be done before the first call of the function.  A function declaration specifies the name, return type, and arguments of a function. This is also called the function prototype.  To be a prototype, a function declaration must establish types for the function’s arguments.  Having the prototype available before the first use of the function allows the compiler to check that the correct number and types of arguments are used in the function call.
  • 5.  The prototype has the same syntax as the function definition, except that it is terminated by a semicolon following the closing parenthesis and therefore has no body.  Although, functions that return int values do not require prototypes, prototypes are recommended.
  • 6. Function Definition  General form of any function definition is: return-type function-name(argument declarations) { declarations and statements }  Return-type refers to the data type of the value being returned from the function. If the return type is omitted, int is assumed.  The values provided to a function for processing are the arguments.  The set of statements between the braces is called as the function body.
  • 7. Arguments – Call by Value  In C, all functions are passed “by value” by default.  This means that the called function is given the values of its arguments in temporary variables rather than the originals.  For ex. main() { int a=4,b=5; sum(a,b); printf(“Sum = %d”,a+b); } sum(int a,int b) { a++; b++; printf(“Sum = %d”,a+b); }
  • 8. Arguments – Call by Reference  When necessary, it is possible to arrange a function which can modify a variable in a calling routine.  The caller must provide the address of the variable to be set (generally, a pointer to a variable), and the called function must declare the parameter to be a pointer and access the variable indirectly through it.  For ex: main() { int a=4,b=5; sum(&a,&b); printf(“Sum = %d”,a+b); } sum(int *a,int *b) { (*a)++; (*b)++; printf(“Sum = %d”,(*a)+(*b)); }
  • 9. Recursion  Recursion defines a function in terms of itself.  It is a programming technique in which a function calls itself.  A recursive function calls itself repeatedly, with different argument values each time.  Some argument values cause the recursive method to return without calling itself. This is the base case.  Either omitting the base case or writing the recursion step incorrectly will cause infinite recursion (stack overflow error).
  • 10.  Recursion to find the factorial of any number: int factorial(int x) { if(x<=1) return 1; else return(x*factorial(x-1)); }
  • 11. External Variables  See the below example: main() { extern int a; printf(“Value of a = %d”,a); } int a=5; Output: Value of a = 5
  • 12. Scope Rules  Example: int num1 = 100; //Global Scope static int num2 = 200; //File Scope int main() { int num3 = 300; //Local Scope if(num2>num1) { int i=0; //Block Scope printf(“Value of i = %dn”,i++); } printf(“Value of num1 = %dn”,num1); printf(“Value of num2 = %dn”,num2); printf(“Value of num3 = %dn”,num3); }
  • 13. Static Variables  Example: static int min = 10; int setmax() { static int max = 100; return ++max; } main() { min++; printf(“Value of max = %d”, setmax()); printf(“Value of max = %d”, setmax()); printf(“Value of min = %d”, min); }