Functions.pptx, programming language in c

floraaluoch3 29 views 33 slides May 08, 2024
Slide 1
Slide 1 of 33
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29
Slide 30
30
Slide 31
31
Slide 32
32
Slide 33
33

About This Presentation

Programming in c


Slide Content

Chapter 4: Functions, Pointers, Errors, Testing By Mr. Samwel Tarus

Overview Functions Definition Role functions Types of functions Communication in functions Types of variables Recursion Program demos Pointers Uses of pointers Declaration of pointers Errors Definition Types of errors Testing Qualities of a good program

Functions Self contained block of statements which performs a particular task and returns a value. The function contains the set of programming statements enclosed by {}. Collection of functions creates a C program i.e C = main() + add() + sum() + ….. +last() Function also called module, block, partition, procedure, subroutine Every C program must have the main() Check the role of main() [ 2 main / key roles ]

Importance of functions in a program Facilitate code Re-use Facilitate program expansion Facilitate the location and isolation of faulty functions in a program Facilitate top down programming

Types of functions Library Functions : Functions which are declared in the C header files such as scanf (), printf (), gets(), puts(), ceil(), floor(), sqrt (), pow() etc. Functions which have been coded, compiled and utilized in other programs. User-defined functions : Functions which are created by the C programmer, so that he/she can use it many times. It reduces the complexity of a big program and optimizes the code.

Communication in functions Functions communicate by passing messages. Three concepts in functions that are very important: Function declaration Function call Function definition User defined functions must be declared before they are used in the program.

Program demo for functions /* functions demo program */ #include< stdio.h > #include< conio.h > void japan(); void china(); int main() { printf (“\n initially in main function\n”); japan(); printf (“\ nfrom japan \n”); china(); printf (“\ nfrom china \n”); return 0; } void china() { printf (“\n iam in china \n”); } void japan() { printf (“\n iam in japan \n”); }

Concepts of functions Function Declaration: Done outside all other functions [ between header files and main() ] Syntax: return_type function_name (return_type1 arg1, ……); E.g , int addition( int a, int b, int c); Declaration informs the compiler about the following: Return type of the function Function name Return types of parameters in the argument list. Number of parameters in the argument list Nb : Declaration creates a template / structure of the function. This structure will be used to link the function call and function definition.

#include< stdio.h > #include< conio.h >   int  sum( int ,  int );   void  main()   {        int   a,b,result ;          printf ("\ nEnter  two numbers:");        scanf ("%d % d",&a,&b );       result = sum( a,b );        printf ("\ nThe  sum is : %d", result);   }   int  sum( int  a,  int  b)   {        return   a+b ;   }  

#include< stdio.h >  #include< conio.h >  int  sum();   void  main()   {        int  result;        result = sum();        printf ("%d", result);   }   int  sum()   {        int   a,b ;         printf ("\ nEnter  two numbers");        scanf ("%d % d",&a,&b );        return   a+b ;    }  

Communication in functions: Cont’d….. Pass by value Pass by reference

Pass by value Call/Pass by value is the process where the copies of the actual parameters are passed in one to one correspondence to the formal parameters in the function definition. Therefore, any changes made in the formal parameters do not affect the actual parameters. e.g , int addition( int a, int b, int c); Copies int addition( int x, int y, int z) Nb : Variables a, b, c are called actual parameters Variables x, y, z are called formal parameters

/* Pass by Value*/ #include< stdio.h > #include< conio.h > int badilisha ( int , int ); int main() { int a=5, b=7; printf ("\ nOriginal values\n"); printf ("\ na = %d\t", a); printf ("b = %d\ n",b ); badilisha ( a,b ); printf ("\ nValue after interchange\n"); printf ("\ na = %d\t", a); printf ("b = %d\ n",b ); return 0; } int badilisha ( int x, int y) { int z; z = x; x = y; y = z; printf ("\ nValues in function definiti \n"); printf ("\ na = %d\t", x); printf ("b = %d\ n",y ); }

Pass by reference Call by reference is the process where the original values are passed to the corresponding function definition. Any changes made in the function definition affects the actual parameters. eg int addition( int &a, int &b, int &c); int addition( int *x, int *y, int *z) Pass by reference uses the concept of pointers:

5 Operators used in Pointers & Ampersand Address of operator Returns the address where the variable is residing in the memory * Asterisk Value at address operator returns the value contained in the address Let a = 5; a 5 6500

Pointers Variable which stores the address of another variable. Pointer variables can be of type i.e , int , float, char, array, function, etc. Pointers are user defined datatypes Syntax for declaration: return type * variable_name ; int * ptr ; char *c;

/* Pass by Reference*/ #include< stdio.h > #include< conio.h > int swap( int *x, int *y); int main() { int a=5, b=7; printf ("\ nOriginal values\n"); printf ("\ na = %d\t", a); printf ("b = %d\ n",b ); badilisha (& a,&b ); printf ("\ nValue after interchange\n"); printf ("\ na = %d\t", a); printf ("b = %d\ n",b ); return 0; } int badilisha ( int *x, int *y) { int z; z = *x; *x = *y; *y = z; printf ("\ nValues in function definition\n"); printf ("\ na = %d\t", *x); printf ("b = %d\n", *y); }

Advantages / uses of pointers Reduces the code  and  improves the performance , it is used to retrieving strings, trees, etc. and used with arrays, structures, and functions. We can  return multiple values from a function  using the pointer. It makes you able to  access any memory location  in the computer's memory. Dynamic memory allocation c language, we can dynamically allocate memory using malloc () and calloc () functions Arrays, Functions, and Structures C language pointers are widely used in arrays, functions, and structures.

Types of variables Local variables: Variables declared within a function. Lifetime and the scope is within the function for which it is declared. Global variables: Variables declared outside all other functions in the program. Lifetime and the scope is the entire program

Program example /* Types of Variable */ #include< stdio.h > #include< conio.h > int a = 20; int show(); int main() { int a = 10; printf ("\n a = %d\ n",a ); show(); return 0; } int show() { printf ("\n a = %d\n", a ); return 0; }

Storage Classes in C Storage classes in C are used to determine the lifetime, visibility, memory location, and initial value of a variable. There are four types of storage classes in C: Automatic External Static Register

Storage Classes Storage Place Default Value Scope Lifetime auto RAM Garbage Value Local Within function extern RAM Zero Global Whole program static RAM Zero Local Till the end of the main program, Retains value between multiple functions call register Register Garbage Value Local Within the function Storage Classes in C

Recursion in C A process where a function calls itself several times; e.g , /*Recursion */ #include< stdio.h > #include< conio.h > int main() { printf ("\ nExample of recursion\n"); main(); return 0; }

/*factorial of any value */ #include < stdio.h > #include< conio.h > int fact( int ); int main() { int n,f ; printf ("Enter value\n"); scanf ("% d",&n ); f = fact(n); printf ("factorial = %d\n", f); } int fact( int n) { int res = 1; if (n==0) { return res; } else if ( n == 1) { return res; } else { res = res * n*fact(n-1); return res; } }

Errors Problems or faults that occur in the program, which makes the behavior of the program abnormal. Also known as the bugs or faults Detected either during the time of compilation or execution. The process of removing these bugs is known as  debugging . Types of errors Syntax error Run-time error Logical error Latent / Hidden errors Semantic error

Syntax error Errors that occur as a result of the violation of the rules of the language; Detected by the compiler at compilation time. Commonly occurred syntax errors are: If we miss the parenthesis (}) while writing the code. Displaying the value of a variable without its declaration. If we miss the semicolon (;) at the end of the statement. #include < stdio.h >   int  main()   {       a = 10;        printf ("The value of a is : %d", a);       return  0;   }  

Runtime error Errors that occur at runtime. Detected by the compiler when running the program. Examples Mismatch of data types Trying to open a file which is not created Lack of free memory space.

Logical error Errors that occur as a result of poor understanding of the logic. Compiler cannot detect such errors. Program runs and outputs results but results are wrong.

Latent error Errors that are only visible when some set of values are used in the program. Example: result = (a + b) /(a – b); Let1 a = 10, b = 5; Output: result = 3 Let2 a = 5, b = 5; Output: division by zero

Semantic error Errors that occurred when the statements are not understandable by the compiler. E.g , Use of a un-initialized variable. int i ; i =i+2; Type compatibility int b = " javatpoint "; Errors in expressions int a, b, c; a+b = c;

Testing The process of evaluating a system or its component(s) with the intent to find whether it satisfies the specified requirements or not. Executing a system in order to identify any gaps, errors, or missing requirements in contrary to the actual requirements. The process of analyzing a software item to detect the differences between existing and required conditions (that is defects/errors/bugs) and to evaluate the features of the software item.

Criteria for testing a program Accuracy Functionality Reliability Usability Efficiency Maintainability Portability Robustness User friendliness Completeness Consistency
Tags