Subprograms, Scope, and Lifetime of Variables in Programming - Prof. Khan, Exams of Programming Languages

Important notes for programming language

Typology: Exams

2020/2021

Uploaded on 07/11/2021

saniya-begum
saniya-begum 🇮🇳

5

(1)

1 document

1 / 38

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
UNIT -3
1
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26

Partial preview of the text

Download Subprograms, Scope, and Lifetime of Variables in Programming - Prof. Khan and more Exams Programming Languages in PDF only on Docsity!

  • UNIT -

Unit -

Subprograms Blocks and Fundamentals of sub-

programs: Scope and lifetime of variable,

static and dynamic scope, Design issues of

subprograms and operations, local referencing

environments, parameter passing methods,

overloaded sub-programs, generic sub-

programs, parameters that are subprogram

names, design issues for functions user

defined overloaded operators, co routines

Subprograms

Functions Procedure

  • (^) A function is a group of statements that together perform a task. Every C program has at least one function, which is main() , and all the most trivial programs can define additional functions.
  • (^) A function declaration tells the compiler about a function's name, return type, and parameters. A function definition provides the actual body of the function.
  • (^) The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.
  • (^) A function definition in C programming consists of a function header and a function body. Here are all the parts of a function
  • (^) Return Type − A function may return a value. The return type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return type is the keyword void. (^5)

/* function declaration / int max(int num1, int num2); int main () { / local variable definition / int a = 100; int b = 200; int ret; / calling a function to get max value / ret = max(a, b); printf( "Max value is : %d\n", ret ); return 0; } / function returning the max between two numbers / int max(int num1, int num2) { / local variable declaration */ int result; if (num1 > num2) result = num1; else result = num2; return result; 8

Function Arguments

The call by value method

of passing arguments to a

function copies the actual

value.

The call by

reference method of

passing arguments to a

function copies the address

of an argument into the

#include <stdio.h>^ formal parameter.

/* function declaration / void swap(int x, int y); int main () { / local variable definition / int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); / calling a function to swap the values */ swap(a, b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; }

  • function definition to swap the values / void swap(int x, int y) { int temp; temp = x; / save the value of x / x = y; / put y into x / y = temp; / put temp into y / return; } #include <stdio.h> / function declaration */ void swap(int *x, int y); int main () { / local variable definition / int a = 100; int b = 200; printf("Before swap, value of a : %d\n", a ); printf("Before swap, value of b : %d\n", b ); / calling a function to swap the values.
  • &a indicates pointer to a ie. address of variable a and
  • &b indicates pointer to b ie. address of variable b. */ swap(&a, &b); printf("After swap, value of a : %d\n", a ); printf("After swap, value of b : %d\n", b ); return 0; } 10

#include int sum(int num1, int num2); int main () { // local variable declaration: int a = 10; int b = 20; int res; res = sum(a, b); std::cout << a << "+" << b << "=" << res << std::endl; return 0; } int sum(int num1, int num2) { int result; result = num1 + num2; return result; }

By using a procedure, a programmer can

make a program do that one thing in

many different ways, using different

parameters and sets of data, simply by

invoking the procedure with different

variables attached.

In many computer programming

languages, procedures are specifically

defined in various ways.. The procedure

is a basic building block for what’s called

object oriented programming, which has

brought a more powerful set of tools to

today’s developer community.

#include <stdio.h> Static int i; int main ( ) { return 0; } #include <stdio.h> Static int i=24; int main ( ) { return 0; } #include <stdio.h> Static int i=0; int main ( ) { return 0; }

Scope and Lifetime of a variable

  • (^) Properties of variables : a name : symbolic name used to identify the variable an address : the location in memory where variable is stored a type Common built-in data types: character, byte integer short integer.
  • (^) Scope – The scope of any variable is actually a subset of life time. A variable may be in the memory but may not be accessible though. So, the area of our program where we can actually access our entity (variable in this case) is the scope of that variable.

Global scope Local scope

Function scope 14

  • (^) Function scope : When variable is passed as formal arguments, it is

said to have function scope.

Actual Parameter

Formal Parameter

The Parameters

which is passed to

function is know as

Actual Parameter

The Parameters

which is Received

by the function

function is know as

Formal Parameter

#include<stdio.h> void sum( int i, int j, int k); /* calling function / int main() { int a = 5; // actual arguments sum(3, 2 * a, a);//Actual Parameter return 0; } / called function / / formal arguments*/ void sum( int i, int j, int k) //Formal Parameter { int s; s = i + j + k; printf ("sum is %d", s); }

Scoping : int a=10,b =20; int fun ( ) { int a = { int c; c= b/c; printf( “%d”,c) } }

Static Scoping or Lexical scoping :

The definition of a variable is

resolved by searching its

containing block or function. It

that fails , then searching the

outer containing block and so on.

Dynamic Scoping: The definition of a

variable is resolved by searching its

containing block and if it is not found ,

then searching its calling function and if

still not found then function which called

that calling function will be searched and

so on

int fun1 (int); int fuc 2(int b) int fun2(int); { int a=5 ; int c; int main ( ) c= a+b; { return c; int a =10; } a = fuc1 (a); printf (“%d”, a); } int fuc1 (int b) { b= b+10; b= fun2 (b); return b; }

Parameter Passing Methods

Parameter Passing Methods

Call by Value Call of Reference

  • (^) Local Variable , Global Variable.

Actual Parameter—Function call and Formal Parameters –Function Definition.

#include<stdio.h> void sum( int i, int j, int k); /* calling function / int main() { int a = 5; // actual arguments sum(3, 2 * a, a);//Actual Parameter return 0; } / called function / / formal arguments*/ void sum( int i, int j, int k) //Formal Parameter { int s; s = i + j + k; printf ("sum is %d", s); }

Actual Parameter – Copied to Formal Parameter

Will Not change

Will change