PPL Unit 4
PPL Unit 4
Function Declaration
The function declaration tells the compiler about function name, the data type of the return value
and parameters.
The function declaration is also called a function prototype.
The function declaration is performed before the main function or inside the main function or
any other function.
Function declaration syntax -
returnType functionName(parametersList/argument list);
E.g void add();
In the above syntax, returnType specifies the data type of the value which is sent as a return value
from the function definition.
The functionName is a user-defined name used to identify the function uniquely in the program.
The parametersList is the data values that are sent to the function definition.
Function Aspects
Function Definition
The function definition provides the actual code of that function.
The function definition is also known as the body of the function. The actual task of the function is
implemented in the function definition.
That means the actual instructions to be performed by a function are written in function definition.
The actual instructions of a function are written inside the braces "{ }".
The function definition is performed before the main function or after the main function.
returnType functionName(parametersList)
{
Actual code...
}
Function Aspects
Function Call
The function call tells the compiler when to execute the function definition.
When a function call is executed, the execution control jumps to the function definition
where the actual code gets executed and returns to the same functions call once the
execution completes.
The function call is performed inside the main function or any other function or
inside the function itself.
functionName(parameters);
Return Statement
When program execution reaches the return statement, it
will leave the current function and the program execution
will resume to the next statement, where it was actually
called.
Syntax
Example
return;
return (value or variable);
return 10;
return a;
return a+b;
Return Statement
The value will be
passed back to the
function where it was
called. We can receive
the value and do the
further programming
from that point.
Types of Functions in C
In C Programming Language, based on providing the
function definition, functions are divided into two types.
Those are as follows...
System Defined Functions
User Defined Functions
System Defined Functions
The C Programming Language provides pre-defined functions to make
programming easy.
These pre-defined functions are known as system defined functions. The system
defined function is defined as follows...
The function whose definition is defined by the system is called as system defined
function.
The system defined functions are also called as Library Functions or Standard
Functions or Pre-Defined Functions. The implementation of system defined
functions is already defined by the system.
In C, all the system defined functions are defined inside the header files like
stdio.h, conio.h, math.h, string.h etc., For example, the funtions printf() and
scanf() are defined in the header file called stdio.h.
System Defined Functions
Whenever we use system defined functions in the program, we must
include the respective header file using #include statement.
For example, if we use a system defined function sqrt() in the
program, we must include the header file called math.h because the
function sqrt() is defined in math.h.
Points to be Remembered
System defined functions are declared in header files
System defined functions are implemented in .dll files. (DLL stands
for Dynamic Link Library).
To use system defined functions the respective header file must be
included.
User Defined Functions
In C programming language, users can also create their own functions.
The functions that are created by users are called as user defined functions.
The user defined function is defined as follows...
The function whose definition is defined by the user is called as user defined
function.
That means the function that is implemented by user is called as user defined
function. For example, the function main is implemented by user so it is
called as user defined function.
In C every user defined function must be declared and implemented.
Whenever we make function call the function definition gets executed.
For example, consider the following program in which we create a function
called addition with two parameters and a return value.
User Defined Functions
That means the function that is implemented by user is called as user defined
function. For example, the function main is implemented by user so it is called
as user defined function.
Output:
ENTER A NUMBER: 7
7 IS A PRIME NUMBER
ENTER A NUMBER: 6
6 IS NOT A PRIME NUMBER
Scope rules of Identifiers
File Scope: Scope of a Identifier starts at the beginning of the file and ends
at the end of the file. It refers to only those Identifiers that are declared
outside of all functions. The Identifiers of File scope are visible all over the
file Identifiers having file scope are global
Block Scope: Scope of a Identifier begins at opening of the block ‘{‘ and
ends at the end of the block ‘}’. Identifiers with block scope are local to
their block
Function Prototype Scope: Identifiers declared in function prototype are
visible within the prototype
Function scope: Function scope begins at the opening of the function and
ends with the closing of it. Function scope is applicable to labels only. A
label declared is used as a target to goto statement and both goto and label
statement must be in same function
Call by value and Call by reference in C
There are two methods to pass the data into the function in C language,
i.e., call by value and call by reference.
Before we discuss function call by value, lets understand the
terminologies that we will use while explaining this:
Actual parameters: The parameters that appear in function calls.
Formal parameters: The parameters that appear in function
declarations.
Call by value
In call by value method, the value of the actual parameters is copied into
the formal parameters.
In other words, we can say that the value of the variable is used in the
function call in the call by value method.
In call by value method, we can not modify the value of the actual
parameter by the formal parameter.
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.
The actual parameter is the argument which is used in the function call
whereas formal parameter is the argument which is used in the function
definition.
Example – call by value
#include<stdio.h>
void change(int num)
{
printf("Before adding value inside function
num=%d \n",num);
num=num+100;
printf("After adding value inside function
num=%d \n", num);
}
int main()
{
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function Output:
printf("After function call x=%d \n", x); Before function call x=100
return 0; Before adding value inside function num=100
After adding value inside function num=200
} After function call x=100
Swapping numbers using Call
by value
#include <stdio.h>
void swap(int , int); //prototype of the function
int main()
{
int a = 10; int b = 20;
// printing the value of a and b in main
printf("Before swapping the values in main a = %d, b = %d\n",a,b);
swap(a,b);
printf("After swapping values in main a = %d, b = %d\n",a,b);
// The value of actual parameters do not change by changing the formal parameters in call
// by value, a = 10, b = 20
return 0; }
Output:
void swap (int a, int b) Before swapping the values in main a = 10, b = 20
{ After swapping values in function a = 20, b = 10
• 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 the same 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.
Pointers
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.
void main()
{
int n = 10;
int* p = &n;
printf("\nThe value of P=%d",p); Output:
printf("\nThe value of N=%d",n); The value of P=-1757119676
} The value of N=10
Example of Call by Reference
#include<stdio.h>
void change(int *)
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(&x);//passing reference in function
printf("After function call x=%d \n", x);
return 0;
}
void change(int *num) {
printf("Before adding value inside function num=%d \n",*num);
*num=*num+100;
printf("After adding value inside function num=%d \n", *num);
} Output:
Before function call x=100
Before adding value inside function num=100
After adding value inside function num=200
After function call x=200
Swapping numbers using Call by
Reference
#include <stdio.h> void swap (int *a, int *b)
void swap(int *, int *); //prototype of the function
{
int main()
{ int temp;
int a = 10; temp = *a;
int b = 20; *a=*b;
printf("Before swapping the values in main a = %d, b = *b=temp;
%d\n",a,b); printf("After swapping
// printing the value of a and b in main (10,20) values in function a = %d, b =
swap(&a,&b); %d\n",*a,*b);
printf("After swapping values in main a = %d, b = // Formal parameters, a = 20, b =
%d\n",a,b); 10
// The values of actual parameters do change in call by reference, a = 20, b = 10 }
}
Output:
Before swapping the values in main a = 10, b = 20
After swapping values in function a = 20, b = 10
After swapping values in main a = 20, b = 10
Difference between Call by Value
and Call by Reference in C
No. Call by value Call by reference
A copy of the value is passed An address of value is passed into
1
into the function the function
Changes made inside the
Changes made inside the function
function are limited to the
validate outside of the function
function only. The values of
2 also. The values of the actual
the actual parameters do not
parameters do change by changing
change by changing the
the formal parameters.
formal parameters.
Actual and formal arguments Actual and formal arguments are
3 are created at the different created at the same memory
memory location location
INTRODUCTION TO POINTERS
Location of i in memory can be represented by following memory map
i location name
3 value at location
65524 location number
It can be see that computer has selected memory location 65524 as the place to
store the value 3
The location number 65524 is not a number to relied upon, because some other time
the computer may choose a different location for storing value 3.
The important point is, i’s address in memory is a number
Address number can be printed through following program
Hence it is printed using %u which is format specifier for unsigned integer
Pointer Notation
#include<stdio.h>
void main()
{
int n = 10;
Output:
int *p = &n;
Address of n = 2050626372
// ‘&’ is ‘Address of’ operator Address of n = 2050626372
// ‘*’ is ‘Value at’ operator The value at p = 10
int **q =&p; The value of P=2050626372
printf("\nAddress of n = %u",&n); The value at p = 10
The value of Q=2050626360
printf("\nAddress of n = %u",p );
The value at q = 10
printf("\nThe value at p = %d", The value at q = 2050626372
*(&n));
printf("\nThe value of P=%u", p);
printf("\nThe value at p = %d", *p);
printf("\nThe value of Q=%u", q);
printf("\nThe value at q = %d", **q);
printf("\nThe value at q = %d", p++);
❖ The other pointer operator available in C is ‘*’, called ‘value at address’
operator.
i j k
3 65524 65522
f = 5*rec(4);
= 5*4*rec(3);
= 5*4*3*rec(2);
= 5*4*3*2*rec(1);
= 5*4*3*2*1
= 120
Practical Exercise 9
1.Write a program to find sum of two numbers using functions.
2. Write a program to find product of two numbers using functions without
arguments, without return type.
3. Write a program to find difference of two numbers using functions without
arguments, with return type.
4. Write a program to find sum of two numbers using functions with arguments
& without return type.
5. Write a program to find product of two numbers using functions with
arguments, with return type.
6. Write a program to swap two numbers using a) Call By Value B) Call By
Reference.