Function Definitions and Parameters in ANSI C Programming

com101b lecture 9 functions n.w
1 / 25
Embed
Share

Learn about function definitions, parameters, return types, and function bodies in ANSI C programming. Explore examples and details to enhance your understanding of C programming concepts.

  • C Programming
  • Functions
  • Parameters
  • ANSI C

Uploaded on | 1 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. COM101B LECTURE 9: FUNCTIONS ASST. PROF. DR. HACER YALIM KELE REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  2. FUNCTION DEFINITION A function definition introduces a new function by declaring the type of value it returns and its parameters ; and specifying the statements that are executed when the function is called, i.e. via function body. Has the following form: <function-type> <function-name>(parameter declarations) { variable-declarations function-statements } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  3. EXAMPLE flaot interest(float pm, float rate, int year) { ... } defines interest to be a function that returns a floating point value and has three parameters of types float, float and int, respectively. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  4. DETAILS Function-type: specifies the type of the function, it corresponds to the type returned by the function A function which does not return any value, returns void. Function-name: the name of the function Parameter-declarations: specifies the names and the types of the parameters are also called formal parameters they are separated by commas if a function does not take any formal parameters, void keyword is written there. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  5. void myfunct(void){...} : this function neither returns a value nor takes any formal parameters. quotient(int i, int j){...} : when a function return type is not specified explicitly, C language by default assumes an integer return value. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  6. FUNCTION BODY The function body consists of variable-declarations followed by function- statements enclosed within the opening and closing curly braces: { } Variable-declarations specify the types and names of the variables that are local to the function A local variable is the one whose value can only be accessed by the function, in which it is declared. Variables declared local to a function supersede any identically named variables outside the function. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  7. FUNCTION PARAMETERS Parameters are treated as if they are declared at the top of the function body. Local variables help functions to be developped independently; Each function opens a local scope, name conflicts are avoided with the outside scopes, automatically. No need to concern for variable names in other functions. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  8. For example: x,y and a variables in f and g functions have no relations. void f(int x, int y) void g(int x, int y) { { int a; int a; ... ... } } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  9. Function-statements: can be any valid C statements they are executed when the function is called. The execution of the function terminates when; the execution reaches the closing brace at the end of the function body a return statement is encountered. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  10. RETURN STATEMENT Can be of two forms: return expression; the value of the expression is returned to the calling function. Example: double smaller(double x, double y) { return x>y ? y : x; } return; returns no value to the calling function. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  11. RETURN TYPE if the return type of the expression does not match the type of the function, it is converted to the type of the function: Example: int truncate(void) { return 1.5; // will return (int)1.5; hence, 1 is returned } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  12. RETURN TYPE When return; statement (without expr) is used, function does not return anything to the calling function This form can only be used when the function is of type void When closing curly brace ( } ) , i.e. end of the function body, is reached without a return statement, it is equivalent to execution of the return statement without expression. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  13. RETURN STATEMENT More than one return statement can be used in the same function Example: int fact(int N) { int i, result; if(n<0) return -1; if(n==0) return 1; for(i=1, result=1;i<=n;i++) result *= i; return result; } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  14. FUNCTION CALL Funtion call expression has the following form: <function-name>(argument-list) function-name: name of the called function argument-list: coma separated list of expressions that constitute the arguments, i.e. called as actual arguments, to the function. Example: fact(5); REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  15. FUNCTION CALL calling function: the function in which the function call is contained called function: The function named in the call Ex: void g() //calling function {... f() // called function ... } REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  16. FUNCTION CALL A function call alters the sequential execution of the program. Upon call, the program control passes from the calling function to the called function Execution begins from the first executable statement of the called function The function is executed until a return or the closing brace of the function is encountered At that point, program control passes back to the calling function, to the point after the function call. The calling function may choose to ignore the returned value. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  17. FUNCTION CALL When a function is called, parameters in the called function are bound to the corresponding arguments that are supplied by the calling function. The names of the parameters need not be identical to those of the arguments. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  18. CALL-BY-VALUE VS CALL-BY-REFERENCE C only provides call by value parameter passing; the function is only provided with the copies of the current values, not the addresses. the changes that take place inside a function is not reflected to the corresponding argument that are passeb by the calling function (in that scope) Call by reference parameter passing is supported using addresses; any change to the value of a parameter is automatically reflected to the corresponding argument. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  19. FUNCTION PROTOTYPES Before calling a function, it must be declared with a prototype of its parameters: function-type function-name(parameter-type list); Ex: Prototype of a function must agree with the function definition. However, parameter names can be given different names. Parameter names in the prototype defs. are actually ignored by the compiler. int fuct(int); is a function prototype REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  20. BLOCK STRUCTURE A block is a sequence of variable declarations and statements within braces. C does not allow a function to be defined in another function. but, it allows block definition inside a function The scope of the variable declarations inside a block extends from the point of its declaration until the end of the block. such a declaration hides any identically named variables in the outer blocks REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  21. EXTERNAL VARIABLES if a variable is defined outside any function, at the same level of function definitions, it is available to all function defined below in the same source file. They are called external variables. Technically, the part of the program within which a name can be used is called its scope. The scope of an external variable is the rest of the source file starting from its definition. Note that the scope of a variable that is defined before any function will be the whole program; these variables are sometimes called as global variables. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  22. STORAGE CLASSES A variable belongs to one of the two storage classes: automatic static The storage class determines the lifetime of the storage associated with the variable. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  23. AUTOMATIC VARIABLES auto type variable-name; A variable is said to be automatic if it is allocated storage upon entry to a segment of a code, and the storage is deallocated upon exit from this segment. Non-static local variables are of auto type by default. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  24. STATIC VARIABLES static type variable-name; A variable is said to be static if it is allocated storage at the beginning of the program execution and the storage remains allocated until the program execution terminates. Variables that are declared outside all blocks at the same level as the function defs are always static. Variables that are declared as static can be initialized only by constant expressions. initialization takes place only once; if not explicitly initialized, by default static variables are initialized to 0. The values assigned to static variables are retained, even if they are local. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

  25. READING ASSIGNMENT Please read Section 5.8 from the reference book. REFERENCE: PROGRAMMING IN ANSI C , KUMAR & AGRAWAL, WEST PUBLISHING CO., 1992

More Related Content