2
Most read
12
Most read
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

More Related Content

PPTX
Presentation on Function in C Programming
PPTX
Function in C program
PPTX
Programming in c Arrays
PPTX
PPTX
Functions in C
PPTX
C tokens
PDF
Python Manuel-R2021.pdf
PPTX
SOP POS, Minterm and Maxterm
Presentation on Function in C Programming
Function in C program
Programming in c Arrays
Functions in C
C tokens
Python Manuel-R2021.pdf
SOP POS, Minterm and Maxterm

What's hot (20)

PPTX
User defined functions in C
PPTX
Function in c program
PPT
Function in C Language
PPTX
Function in c
PPT
How to execute a C program
PPT
Operators in C++
ODP
Function
PPT
RECURSION IN C
PDF
Operators in c programming
PPTX
Operators and expressions in c language
PPT
Variables in C Programming
PPT
friend function(c++)
PPTX
CONDITIONAL STATEMENT IN C LANGUAGE
PPTX
Printf and scanf
PPT
Pointers C programming
PPTX
Control statements in c
PPTX
Function C++
PPTX
String in c programming
PPTX
Call by value or call by reference in C++
PPTX
functions of C++
User defined functions in C
Function in c program
Function in C Language
Function in c
How to execute a C program
Operators in C++
Function
RECURSION IN C
Operators in c programming
Operators and expressions in c language
Variables in C Programming
friend function(c++)
CONDITIONAL STATEMENT IN C LANGUAGE
Printf and scanf
Pointers C programming
Control statements in c
Function C++
String in c programming
Call by value or call by reference in C++
functions of C++
Ad

Similar to Functions in C (20)

PDF
functionsinc-130108032745-phpapp01.pdf
PPTX
C Programming Language Part 7
PPTX
UNIT3.pptx
PPTX
PPTX
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
PDF
Functions
PPTX
Dti2143 chapter 5
DOC
Functions struct&union
PPTX
functions
PPTX
C function
PPTX
Detailed concept of function in c programming
PPT
Recursion in C
PPTX
Presentation on function
PPT
presentation_functions_1443207686_140676.ppt
PPTX
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
PPT
function_v1.ppt
PPT
function_v1.ppt
PPTX
Functions.pptx, programming language in c
PPTX
Classes function overloading
PPT
C programming is a powerful, general-purpose language used for developing ope...
functionsinc-130108032745-phpapp01.pdf
C Programming Language Part 7
UNIT3.pptx
Unit_5Functionspptx__2022_12_27_10_47_17 (1).pptx
Functions
Dti2143 chapter 5
Functions struct&union
functions
C function
Detailed concept of function in c programming
Recursion in C
Presentation on function
presentation_functions_1443207686_140676.ppt
CH.4FUNCTIONS IN C_FYBSC(CS).pptx
function_v1.ppt
function_v1.ppt
Functions.pptx, programming language in c
Classes function overloading
C programming is a powerful, general-purpose language used for developing ope...
Ad

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); }