Pps Unit4 Qa
Pps Unit4 Qa
A function prototype is simply the declaration of a function that specifies function's name, parameters
and return type. It doesn't contain function body.
A function prototype gives information to the compiler that the function may later be used in the
program.
Example
int addNumbers(int a, int b); is the function prototype which provides the following
information to the compiler:
By this information, the compiler cross-checks the function signatures before calling it.
If the function prototypes are not mentioned, then the program may be compiled with some
warnings, and sometimes generate some strange output.
If some function is called somewhere, but its body is not defined yet, that is defined after the current
line, then it may generate problems.
The compiler does not find what is the function? and what is its signature?. In that case, we need to
function prototypes.
If the function is defined before then we do not need prototypes.
Example Program
#include<stdio.h>
void function(int); //prototype
main() {
function(50); //Function call
}
void function(int x) //Function definition
{
printf("The value of x is: %d", x);
}
Output
The value of x is: 50
---------------------------------------------------------------------------------------------------------------------------
2. List and explain different types of user defined functions with respect to parameter and return
type of function.
Depending upon the presence of arguments and the return values, user defined functions can be
classified into four categories.
Function with no arguments and no return value
Function with no arguments and return value
Function with arguments and no return values
Function with arguments and return value
function1( ) function2( )
{ {
…… ……
…… ……
…… ……
function2( ); }
……
……
……
}
#include<stdio.h>
void sum();
void main()
{
printf("\Call the function sum:");
sum();
}
void sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
printf("The sum is %d",a+b);
}
Function with no arguments and return value
Function with no argument: The called function does not receive any data from calling function.
Function with return value: Called function will sent back a value to the calling function.
Calling Function Called Function
function1( ) function2( )
{ {
…… ……
…… ……
…… return (x);
function2( ); }
……
……
……
}
#include<stdio.h>
int sum();
void main()
{
int result;
printf("\nCall the function sum:");
result = sum();
printf("%d",result);
}
int sum()
{
int a,b;
printf("\nEnter two numbers");
scanf("%d %d",&a,&b);
return a+b;
}
Function with arguments and no return values
Function with argument: The called function will accept data from the calling function as there are
arguments.
Function with no return value: There is no value will be returned to the calling program from the called
function.
#include<stdio.h>
void sum(int, int);
void main()
{
int a,b,result;
printf("\nCall the function sum:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
sum(a,b);
}
#include<stdio.h>
int sum(int, int);
void main()
{
int a,b,result;
printf("\nCall the function sum:");
printf("\nEnter two numbers:");
scanf("%d %d",&a,&b);
result = sum(a,b);
printf("\nThe sum is : %d",result);
}
Call by Value
In call by value parameter passing method, the copy of actual parameter values are copied to formal
parameters and these formal parameters are used in called function.
The changes made on the formal parameters does not affect the values of actual parameters.
That means, after the execution control comes back to the calling function, the actual parameter values
remains same.
In call by value, different memory is allocated for actual and formal parameters since the value of the
actual parameter is copied into the formal parameter.
Program
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
temp = a;
a=b;
b=temp;
printf("After swapping values in function a = %d, b = %d\n",a,b);
}
Output
Before swapping the values in main a = 10, b = 20
Before swapping values in function a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 10, b = 20
Call by reference
In call by reference, the address of the variable is passed into the function call as the actual parameter.
The value of the actual parameters can be modified by changing the formal parameters since the
address of the actual parameters is passed.
In call by reference, the memory allocation is similar for both formal parameters and actual parameters.
All the operations in the function are performed on the value stored at the address of the actual
parameters, and the modified value gets stored at the same address.
Program
#include <stdio.h>
void swap(int *, int *); //prototype of the function
int main()
{
int a = 10;
int b = 20;
printf("Before swapping the values in main a = %d, b = %d\n",a,b)
swap(&a,&b);
temp = *n1;
*n1 = *n2;
*n2 = temp;
Output
Before swapping the values in main a = 10, b = 20
Before swapping values in function a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
---------------------------------------------------------------------------------------------------------------------------
4. Explain with examples how arrays are passed as arguments in functions.
To process arrays in a large program, we have to be able to pass them to functions.
The array_name contains the address of the first element. Here, we must notice that we need to
pass only the name of the array in the function which is intended to accept an array.
The array defined as the formal parameter will automatically refer to the array specified by the
array name defined as an actual parameter.
We can pass arrays in two ways:
Passing individual elements
Passing a whole array
Passing individual elements
Just like normal variable, we can pass individual elements of an array to a function.
We can pass either the values (or) the address of an array to a function.
Call by value: int sum (int arr[]);
Call by reference: int sum (int* ptr);
To pass a value to a function, give the array name along with its index in the function call.
We can pass the address of individual array elements to a function.
When we pass the address of an array while calling a function then this is called function call by
reference. When we pass an address as an argument, the function declaration should have a
pointer as a parameter to receive the passed address.
Program: Pass Individual Array Elements
#include <stdio.h>
void display(int n1, int n2)
{
printf("%d\n", n1);
printf("%d\n", n2);
}
int main( ) {
int a[ ] = {2, 8, 4, 12};
int main()
{
float result, a[] = {23.4, 55, 22.6, 3, 40.5, 18};
// array is passed to Sum()
result = Sum(a,6);
printf("Sum of array values = %.2f", result);
return 0;
}
return sum;
}
Output
Passing each item of the structure as a function argument. It is similar to passing normal values as
arguments.
Pass the whole structure as a value.
Pass the address of the structure (pass by reference).
#include <stdio.h>
#include <string.h>
struct student
{
int id;
char name[20];
float percentage;
};
void func(struct student record);
int main()
{
struct student record;
record.id=1;
strcpy(record.name, "Raju");
record.percentage = 86.5;
func(record);
return 0;
}
Output
Id is: 1
Name is: Raju
Percentage is: 86.500000
---------------------------------------------------------------------------------------------------------------------------
6. Define Recursion.
Recursion is the process of repeating items in a self-similar way.
In programming languages, if a program allows user to call a function inside the same function,
then it is called a recursive call of the function.
A function that calls itself is known as a recursive function. And, this technique is known as
recursion.
a. Write a recursive function for factorial of a given number n.
#include<stdio.h>
int fact(int n);
int main() {
int n;
printf("Enter a positive integer: ");
scanf("%d",&n);
printf("Factorial of %d = %d", n, fact(n));
return 0;
}
int fact(int n) {
if (n>=1)
return n*fact(n-1);
else
return 1;
}
Output:
int main()
{
int n,i;
printf("Enter a number: ");
scanf("%d",&n);
for(i = 0;i<n;i++)
{
printf("%d ",fibbonacci(i));
}
return 0;
}
int fibbonacci(int n)
{
if(n == 0)
{
return 0;
}
else if(n == 1)
{
return 1;
}
else
{
return (fibbonacci(n-1) + fibbonacci(n-2));
}
}
Output:
Enter a number: 5
0 1 1 2 3
---------------------------------------------------------------------------------------------------------------------------
7. List and explain the some C standard functions and libraries.
---------------------------------------------------------------------------------------------------------------------------
8. List and explain the functions used to allocate and free memory dynamically.
The concept of dynamic memory allocation in c language enables the C programmer to allocate
memory at runtime.
Dynamic memory allocation in c language is possible by 4 functions of stdlib.h header file.
malloc() - allocates single block of requested memory.
calloc() - allocates multiple block of requested memory.
realloc() - reallocates the memory occupied by malloc() or calloc() functions.
free() - frees the dynamically allocated memory.
malloc( )
The malloc() function allocates single block of requested memory.
It doesn't initialize memory at execution time, so it has garbage value initially.
It returns NULL if memory is not sufficient.
The syntax of malloc() function is given below:
ptr=(cast-type*)malloc(byte-size)
calloc()
The calloc() function allocates multiple block of requested memory.
It initially initialize all bytes to zero.
It returns NULL if memory is not sufficient.
The syntax of calloc() function is given below:
ptr=(cast-type*)calloc(number, byte-size)
free()
The memory occupied by malloc() or calloc() functions must be released by calling free()
function. Otherwise, it will consume memory until program exit.
The syntax of free() function.
free(ptr)
Program
#include <stdio.h>
#include <stdlib.h>
int main()
{
int *ptr, *ptr1;
int n, i;
n = 5;
printf("Enter number of elements: %d\n", n);
free(ptr);
printf("Malloc Memory successfully freed.\n");
free(ptr1);
printf("Calloc Memory successfully freed.\n");
}
return 0;
}
Output
Enter number of elements: 5
Memory successfully allocated using malloc.
Malloc Memory successfully freed.