Docsity

Function Calls and Variable Scope in C: Understanding Call by Value and Call by Reference, Slides of Computer Fundamentals

Partial preview of the text

Download Function Calls and Variable Scope in C: Understanding Call by Value and Call by Reference and more Slides Computer Fundamentals in PDF only on Docsity!

Topics

  • Argument passing
    • Call by value
    • Call by reference
  • Scope of variable
    • Local
    • Global

Calling functions: Passing by value

2

main() square ()

x y 2

argument (^) parameter

2

Area of the Ring

Outer Circle

Inner Circle

Area of Outer Circle ____^ Area of Inner Circle = Area of the Ring

Area of the Circle = pi * radius of the circle

void main ( )

{

double radius, ringArea;

ringArea = ( 3.1415926 * rad1 * rad1 ) โ€“ ( 3.1415926 * rad2 * rad2 ) ;

Calculating ringArea without using Function

double circleArea ( double ); // Function Declaration

void main() { int rad1,rad2; rad1=20; rad2=32; ringArea = circleArea(rad1) โ€“ circleArea (rad2); // Function Call }

double circleArea ( double radius ) // Function Definition { return ( 3.1415926 * radius * radius ) ; }

Calculating ringArea by using a Function

#include < stdio.h >

double raiseToPow ( double, int); // function prototype

void main ( ) { double x ; int i ; printf( โ€œ Please enter the number โ€œ ); scanf (โ€œ%dโ€, &x ) ; printf( " Please enter the integer power that you want this number raised to " ); scanf (โ€œ%dโ€, &i ) ; printf(โ€œ%d raise to power %d is equal to %dโ€, x, i , raiseToPow ( x , i ) ) ; //function call }

*double raiseToPow ( double x , int power ) // function definition { double result ; int i ; result = 1.0 ; for ( i = 1 ; i <= power ; i ++ ) { result * = x ; // result = result x } return ( result ) ; }

Program to implement x^n

Example: Call by Value

#include <stdio.h>

int func ( int ) ;

main ( )

{

int i = 10 ; printf( โ€œIn main i = %dโ€œ, i) ; func ( i ) ; printf( " Back in main, i = %d" , i) ;

}

Example: Call by Value

int func ( int i )

{

printf("In function f , i = %dโ€œ, i) ; i *= 2 ; printf("In function f , i is now = %dโ€œ , i) ; return i ;

}

Call by Reference

  • A function in which original value of the

variable is changed

  • To call by reference we cannot pass value, we

have to pass memory address of variable

  • โ€œ&โ€ is used to take the address of a variable
  • Will see this later

Calling functions: Passing by

reference

2

main() square ()

x

y

argument (^) parameter

Identifiers

  • Identifier is any name user creates in a program e.g. constants and variables
  • Functions are also identifiers
  • Labels are also identifiers

Scope of Identifiers

  • Scope means visibility
    • E.g. variable scope means which parts of the program can access that variable

1.Local Scope

2.File Scope

Example

void functionName ( )

{

{ int i ; }

โ€ฆ..

}

Example: Block Scope

for ( int i = 0 ; i < 10 ; i++ ) {

}

  • It is block level scope declared in for loop
  • When for is finished โ€œ i โ€ no longer exists
  • This statement is valid when we are talking about C99 standard. - If you want to compile using C99 standard, you have to use $gcc block_scope.c โ€“ o ouput โ€“ std=c