PPS
Unit 3Function & Pointers Q1) What is a function? Why do we use function? Functions are useful to divide c programs into smaller modules. Programmer can invoked these modules anywhere inside c program for any number of times. Functions are used to increase readability of the code. Size of program can be reduce by using functions. By using function, programmer can divide complex tasks into smaller manageable tasks and test them independently before using them together.Functions of C language are defined with the type of function. The type of functions indicates the data type of value which will return by function. In order to use function in the program, initially programmer have to inform compiler about the function. This is also called as defining a function. In C programme all the function definition present outside the main function. All function need to be declared and defined before use. Function declaration requires function name, argument list, and return type.
Q10) Explain the advantages of function?We can divide c program in smaller modules. We can call module whenever require. e.g suppose we have written calculator program then we can write 4 modules (i.e add,sub,multiply,divide) Modular programming makes C program more readable. Modules once created , can be reused in other programs.
Return Type Function name (Argument list)
{
Statement 1;
Statement 2;
…………...
Statement n;
}
Q2) How do we pass value between functions?This is the default way of passing the parameters to the function. This is achieved by passing the copy of data to the function. This mechanism is also called as call by value. In case of parameter passing by value, the changes made to the formal arguments in the called function have no effect on the values of actual arguments in the calling function. This mechanism is used when programmer don't want to change the value of passed parameters. When parameters are passed by value then functions in C create copies of the passed in variables and do required processing on these copied variables. Pass-by-value is implemented by actual data transfer so additional storage is required to maintain the copies of passed parameters.Example:#include <stdio.h>
#include<conio.h>
/* function declaration goes here.*/
void swap( int p1, int p2 );
int main()
{
int a = 10;
int b = 20;
printf("Before: Value of a = %d and value of b = %d\n", a, b );
swap( a, b );
printf("After: Value of a = %d and value of b = %d\n", a, b );
getch();
}
void swap( int p1, int p2 )
{
int t;
t = p2;
p2 = p1;
p1 = t;
printf("Value of a (p1) = %d and value of b(p2) = %d\n", p1, p2 );
}
Output :Before: Value of a = 10 and value of b = 20Value of a (p1) = 20 and value of b (p2) = 10After: Value of a = 10 and value of b = 20Note: In the above example the values of “a” and “b” remain unchanged before calling swap function and after calling swap function. Q3) Explain the scope rule of functions? A scope in any programming is a region of the program where a defined variable can have its existence and beyond that variable it cannot be accessed. There are three places where variables can be declared in C programming language −Inside a function or a block which is called local variables.Outside of all functions which is called global variables.In the definition of function parameters which are called formal parameters.Local VariablesVariables that are declared inside a function or block are called local variables. They can be used only by statements that are inside that function or block of code. Local variables are not known to functions outside their own. The following example shows how local variables are used. Here all the variables a, b, and c are local to main() function. #include <stdio.h>int main () { /* local variable declaration */ int a, b; int c; /* actual initialization */ a = 10; b = 20; c = a + b; printf ("value of a = %d, b = %d and c = %d\n", a, b, c); return 0;}Global VariablesGlobal variables are defined outside a function, usually on top of the program. Global variables hold their values throughout the lifetime of your program and they can be accessed inside any of the functions defined for the program.A global variable can be accessed by any function. That is, a global variable is available for use throughout your entire program after its declaration. The following program show how global variables are used in a program.#include <stdio.h>/* global variable declaration */int g;int main () { /* local variable declaration */ int a, b; /* actual initialization */ a = 10; b = 20; g = a + b; printf ("value of a = %d, b = %d and g = %d\n", a, b, g); return 0;}Q4) Explain the order pf passing arguments? The function called at program startup is named main . The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared. A main function has the following syntax:int main(void) { . . . }int main(int argc, char *argv[ ]) { . . . })argcThe number of arguments in the command line that invoked the program. The value of argc is nonnegative.argvPointer to an array of character strings that contain the arguments, one per string. The value argv[argc] is a null pointer.If the value of argc is greater than zero, the array members argv[0] through argv[argc – 1] inclusive contain pointers to strings, which are given implementation-defined values by the host environment before program startup. The intent is to supply the program with information determined before program startup from elsewhere in the host environment. If the host environment cannot supply strings with letters in both uppercase and lowercase, the host environment ensures that the strings are received in lowercase.If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] is the null character if the program name is not available from the host environment.If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc - 1] represent the program parameters.The parameters argc and argv, and the strings pointed to by the argv array, can be modified by the program and keep their last-stored values between program startup and program termination.In the main function definition, parameters are optional. However, only the parameters that are defined can be accessed.Q5) Explain call by value and call by reference? The function called at program startup is named main . The main function can be defined with no parameters or with two parameters (for passing command-line arguments to a program when it begins executing). The two parameters are referred to here as argc and argv, though any names can be used because they are local to the function in which they are declared. A main function has the following syntax:int main(void) { . . . }int main(int argc, char *argv[ ]) { . . . })argcThe number of arguments in the command line that invoked the program. The value of argc is nonnegative.argvPointer to an array of character strings that contain the arguments, one per string. The value argv[argc] is a null pointer.If the value of argc is greater than zero, the array members argv[0] through argv[argc - 1] inclusive contain pointers to strings, which are given implementation-defined values by the host environment before program startup. The intent is to supply the program with information determined before program startup from elsewhere in the host environment. If the host environment cannot supply strings with letters in both uppercase and lowercase, the host environment ensures that the strings are received in lowercase.If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] is the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc – 1] represent the program parameters.The parameters argc and argv, and the strings pointed to by the argv array, can be modified by the program and keep their last-stored values between program startup and program termination.Q6) Explain pointers in C? The pointer in C language is a variable which stores the address of another variable. This variable can be of type int, char, array, function, or any other pointer. ... int n = 10; int* p = &n; // Variable p of type pointer is pointing to the address of the variable n of type integer.One of the vital and heavily used feature ‘C’ is pointer. Most of the other programming languages also support pointers but only few of them use it freely. Support pointers but only few of them use it freely. When we declare any variable in C language, there are three things associated with that variable. 1. Data type of variable: Data type defines the type of data that variable can hold. Data type tells compiler about the amount of memory allocate to the variable.2. Address of Variable: Address of variable represent the exact address of memory location which is allocated to variable.3. Value of variable : It is the value of variable which is store at the address of memory location allocated to variable. Example : int n = 5;In the above example ‘int’ is the data type which tells compiler to allocate 2 bytes of memory to variable ‘n’.Once the variable is declare compiler allocated two bytes of memory to variable ‘n’. Suppose the address of that memory location is 1020. At the address of memory location 1020 the value 5 is store.Memory map for above declaration is as follows. use of &,/and ‘*’ operator Q7) Explain Pointer notation? Pointers in C language is a variable that stores/points the address of another variable. A Pointer in C is used to allocate memory dynamically i.e. at run time. The pointer variable might be belonging to any of the data type such as int, float, char, double, short etc.Syntax : data_type *var_name; Example : int *p; char *p;Where, * is used to denote that “p” is pointer variable and not a normal variable.Normal variable stores the value whereas pointer variable stores the address of the variable.The content of the C pointer always be a whole number i.e. address.Always C pointer is initialized to null, i.e. int *p = null.The value of null pointer is 0.& symbol is used to get the address of the variable.* symbol is used to get the value of the variable that the pointer is pointing to.If a pointer in C is assigned to NULL, it means it is pointing to nothing.Two pointers can be subtracted to know how many elements are available between these two pointers.But, Pointer addition, multiplication, division are not allowed.The size of any pointer is 2 byte (for 16 bit compiler). Q8) Explain back function calls? 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. // 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 0; } I am function A Q9) Differentiate call by value and call by reference?0 matching results found