COM 241 Programming Languages Concepts Assoc Prof Dr

  • Slides: 12
Download presentation
COM 241 Programming Languages Concepts Assoc. Prof. Dr. Mehmet Serdar Güzel Slides are mainly

COM 241 Programming Languages Concepts Assoc. Prof. Dr. Mehmet Serdar Güzel Slides are mainly adapted from the following course page: http: //www. cs. utexas. edu/~shmat/courses/cs 345/ • slid e 1

COM 241 Functions slide 2

COM 241 Functions slide 2

Functional Abstraction A function is a block of organized, reusable code that is used

Functional Abstraction A function is a block of organized, reusable code that is used to perform a single, related action. Functions provide better modularity for your application and a high degree of code reusing. You have already seen various functions like printf() and main(). These are called built-in functions provided by the language itself, but we can write our own functions as well and this tutorial will teach you how to write and use those functions in C programming language. Value-returning functions Example: m = sqrt(4*a*c))/2 Non-value returning functions Called “procedures” (Ada), “subroutines” (Fortran), “void functions/methods” (C, C++, Java, C#) Have a visible side effect: change the state of some data value not defined in the function definition Example: strcpy(a 1, a 2) slide 3

System Calls A system call is a way for programs to interact with the

System Calls A system call is a way for programs to interact with the operating system. A computer program makes a system call when it makes a request to the operating system's kernel. slide 4

Arguments && Parameters Argument: expression appearing in a function call Parameter: identifier appeared in

Arguments && Parameters Argument: expression appearing in a function call Parameter: identifier appeared in function declaration Parameter-argument matches by considering number and position Exception: Perl. slide 5

Parameter Passing Mechanisms Call By value Call By reference Call By value-result Call By

Parameter Passing Mechanisms Call By value Call By reference Call By value-result Call By name slide 6

Pass by Value vs Reference Pass by value means you are making a copy

Pass by Value vs Reference Pass by value means you are making a copy in memory of the actual parameter's value that is passed in, a copy of the contents of the actual parameter. Pass-by-reference means to pass the reference of an argument in the calling function to the corresponding formal parameter of the called function slide 7

Two Ways To Pass By Reference C or C++ only void swap (int *a,

Two Ways To Pass By Reference C or C++ only void swap (int *a, int *b) { int temp = *a; *a = *b; *b = temp; } void swap (int& a, int& b) { int temp = a; a = b; b = temp; } int a=13, b=14; swap(&a, &b); int a=13, b=14; swap(a, b); Which one is better? slide 8

Pass by Value-Result Pass by value at the time of the call and/or copy

Pass by Value-Result Pass by value at the time of the call and/or copy the result back to the argument at the end of the call (copy -in-copy-out) Example: “in out” parameters in Ada slide 9

Pass by Value-Result Refered by http: //courses. cs. vt. edu/cs 3304/Fall 16/meng/lecture_notes/cs 3304 -16.

Pass by Value-Result Refered by http: //courses. cs. vt. edu/cs 3304/Fall 16/meng/lecture_notes/cs 3304 -16. pdf slide 10

Pass by Name Textually substitute the argument for every instance of its corresponding parameter

Pass by Name Textually substitute the argument for every instance of its corresponding parameter in the function body Originated with Algol 60 but dropped by Algol’s successors -- Pascal, Ada, Modula slide 11

Pass by Name Refered by http: //courses. cs. vt. edu/cs 3304/Fall 16/meng/lecture_notes/cs 3304 -16.

Pass by Name Refered by http: //courses. cs. vt. edu/cs 3304/Fall 16/meng/lecture_notes/cs 3304 -16. pdf slide 12