Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE) PDF Download

Callbacks in C

A callback is any executable code that is passed as an argument to other code, which is expected to call back (execute) the argument at a given time [Source : Wiki]. In simple language, If a reference of a function is passed to another function as an argument to call it, then it will be called as a Callback function.
In C, a callback function is a function that is called through a function pointer.

Below is a simple example in C to illustrate the above definition to make it more clear:
// A simple C program to demonstrate callback
#include<stdio.h>
void A()
{
    printf("I am function A\n");
}
// callback function
void B(void (*ptr)())
{
    (*ptr) (); // callback to A
}
int main()
{
    void (*ptr)() = &A;
    // calling function B and passing
    // address of the function A as argument
    B(ptr);
   return θ;
}
I am function A

In C++ STL, functors are also used for this purpose.

Nested functions in C

Some programmer thinks that defining a function inside an another function is known as “nested function”. But the reality is that it is not a nested function, it is treated as lexical scoping. Lexical scoping is not valid in C because the compiler cant reach/find the correct memory location of the inner function.
Nested function is not supported by C because we cannot define a function within another function in C. We can declare a function inside a function, but it’s not a nested function.
Because nested functions definitions can not access local variables of the surrounding blocks, they can access only global variables of the containing module. This is done so that lookup of global variables doesn’t have to go through the directory. As in C, there are two nested scopes: local and global (and beyond this, built-ins). Therefore, nested functions have only a limited use. If we try to approach nested function in C, then we will get compile time error.
// C program to illustrate the
// concept of Nested function.
#include <stdio.h>
int main(void)
{
    printf("Main");
    int fun()
    {
        printf("fun");
       // defining view() function inside fun() function.
        int view()
        {
            printf("view");
        }
       return 1;
    }
    view();
}
Output:
Compile time error: undefined reference to `view'
An extension of the GNU C Compiler allows the declarations of nested functions. The declarations of nested functions under GCC’s extension need to be prefix/start with the auto keyword.
// C program of nested function
// with the help of gcc extension
#include <stdio.h>
int main(void)
{
    auto int view(); // declare function with auto keyword
    view(); // calling function
    printf("Main\n");
    int view()
    {
        printf("View\n");
        return 1;
    }
    printf("GEEKS");
    return 0;
}
Output:
view
Main
GEEKS

Parameter Passing Techniques in C/C++

There are different ways in which parameter data can be passed into and out of methods and functions. Let us assume that a function B() is called from another function A(). In this case A is called the “caller function” and B is called the “called function or callee function”. Also, the arguments which A sends to B are called actual arguments and the parameters of B are called formal arguments.

Terminology

1. Formal Parameter: A variable and its type as they appear in the prototype of the function or method.
2. Actual Parameter: The variable or expression corresponding to a formal parameter that appears in the function or method call in the calling environment.
3. Modes:

  • IN: Passes info from caller to calle.
  • OUT: Callee writes values in caller.
  • IN/OUT: Caller tells callee value of variable, which may be updated by callee.

Important methods of Parameter Passing

1. Pass By Value: This method uses in-mode semantics. Changes made to formal parameter do not get transmitted back to the caller. Any modifications to the formal parameter variable inside the called function or method affect only the separate storage location and will not be reflected in the actual parameter in the calling environment. This method is also called as call by value.
Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE)

// C program to illustrate
// call by value
#include <stdio.h>
void func(int a, int b)
{
    a += b;
    printf("In func, a = %d b = %d\n", a, b);
}
int main(void)
{
    int x = 5, y = 7;  
    // Passing parameters
    func(x, y);
    printf("In main, x = %d y = %d\n", x, y);
    return 0;
}
Output:
In func, a = 12 b = 7
In main, x = 5 y = 7
Languages like C, C++, Java support this type of parameter passing. Java in fact is strictly call by value.
Shortcomings:

  • Inefficiency in storage allocation
  • For objects and arrays, the copy semantics are costly

2. Pass by reference(aliasing): This technique uses in/out-mode semantics. Changes made to formal parameter do get transmitted back to the caller through parameter passing. Any changes to the formal parameter are reflected in the actual parameter in the calling environment as formal parameter receives a reference (or pointer) to the actual data. This method is also called as call by reference. This method is efficient in both time and space.
Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE)// C program to illustrate
// call by reference
#include <stdio.h>
void swapnum(int* i, int* j)
{
    int temp = *i;
    *i = *j;
    *j = temp;
}
int main(void)

{
    int a = 10, b = 20;
    // passing parameters
    swapnum(&a, &b);
    printf("a is %d and b is %d\n", a, b);
    return 0;
}
Output:
a is 2θ and b is 1θ
C and C++ both support call by value as well as call by reference whereas Java does’nt support call by reference.
Shortcomings:

  • Many potential scenarios can occur
  • Programs are difficult to understand sometimes

Other methods of Parameter Passing

These techniques are older and were used in earlier programming languages like Pascal, Algol and Fortran. These techniques are not applicable in high level languages.
1. Pass by Result: This method uses out-mode semantics. Just before control is transfered back to the caller, the value of the formal parameter is transmitted back to the actual parameter.T his method is sometimes called call by result. In general, pass by result technique is implemented by copy.
2. Pass by Value-Result: This method uses in/out-mode semantics. It is a combination of Pass-by-Value and Pass-by-result. Just before the control is transferred back to the caller, the value of the formal parameter is transmitted back to the actual parameter. This method is sometimes called as call by value-result
3. Pass by name: This technique is used in programming language such as Algol. In this technique, symbolic “name” of a variable is passed, which allows it both to be accessed and update.
Example:
To double the value of C[j], you can pass its name (not its value) into the following procedure.
procedure double(x);
  real x;
begin
  x:=x*2
end;
In general, the effect of pass-by-name is to textually substitute the argument in a procedure call for the corresponding parameter in the body of the procedure.

Implications of Pass-by-Name mechanism

  • The argument expression is re-evaluated each time the formal parameter is passed.
  • The procedure can change the values of variables used in the argument expression and hence change the expression’s value.
The document Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE) is a part of the Computer Science Engineering (CSE) Course Programming and Data Structures.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)
119 docs|30 tests

Top Courses for Computer Science Engineering (CSE)

119 docs|30 tests
Download as PDF
Explore Courses for Computer Science Engineering (CSE) exam

Top Courses for Computer Science Engineering (CSE)

Signup for Free!
Signup to see your scores go up within 7 days! Learn & Practice with 1000+ FREE Notes, Videos & Tests.
10M+ students study on EduRev
Related Searches

pdf

,

Semester Notes

,

ppt

,

mock tests for examination

,

Viva Questions

,

Previous Year Questions with Solutions

,

video lectures

,

study material

,

past year papers

,

Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE)

,

practice quizzes

,

Important questions

,

shortcuts and tricks

,

Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE)

,

MCQs

,

Summary

,

Objective type Questions

,

Free

,

Sample Paper

,

Functions in C-3 | Programming and Data Structures - Computer Science Engineering (CSE)

,

Exam

,

Extra Questions

;