1 / 31

Functions in C

Functions in C. Consider. #include < stdio.h > main() { int i ; for( i =1; i <= 5; i ++) { printf ("%d ", i * i ); } for( i =1; i <= 5; i ++) { printf ("%d ", i * i ); } return 0; }. Life would be easier. If we could call a function to do the same

olga-curry
Download Presentation

Functions in C

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions in C

  2. Consider #include <stdio.h> main() { inti; for(i=1; i <= 5; i++) { printf("%d ", i*i); } for(i=1; i <= 5; i++) { printf("%d ", i*i); } return 0; }

  3. Life would be easier • If we could call a function to do the same • Consider the following

  4. void Print_Squares(void) { int i; for(i=1; i <=5; i++) { printf("%d ", i*i); } }

  5. Our program now is #include <stdio.h> void Print_Squares(); main() {Print_Squares(); Print_Squares(); return(0); } void Print_Squares(void) { inti; for(i=1; i <=5; i++) { printf("%d ", i*i); } }

  6. The syntax of a function is

  7. typename(type1arg1, type2arg2, ...) { /* code */ }

  8. Example functions int square(int x) { int square_of_x; square_of_x = x * x; return square_of_x; } Returns the square of an integer

  9. Another float doubleit(float x) { return x*2.0; }

  10. Program syntax • Header files e.g. #include<string.h> • Function declaration i.e. a function header • Syntax type function name( Argument list); • Sometimes argument list is omitted, sometimes not • main() • Function call i.e. function name followed by legitimate arguments in parentheses e.g. • sin(30); • After program body full function definition

  11. Example • //Header files and function declaration • #include <stdio.h> • float doubleit(float x) // or float doubleit() • //Next program body

  12. Program Body main() { float y; y= doubleit(2.0); printf(“value of y is now %f”,y); } //This is followed by function definition

  13. float doubleit(float x) { return x*2.0; }

  14. Note • Result of function is attached by return to function type indicated by typename(type1arg1, type2arg2, ...) { /* code */ }

  15. Functions and Truth Values

  16. Boolean Types • In computer science, the Boolean or logical data type is a data type, having two values (usually denoted true and false), intended to represent the truth values of logic and Boolean algebra. • It is named after George Boole, who first defined an algebraic system of logic in the mid 19th century.

  17. Boolean types in programming languages • In programming languages that have a built-in Boolean data type, such as Pascal and Java, the comparison operators such as '>' and '≠' are usually defined to return a Boolean value. Also, conditional and iterative commands may be defined to test Boolean-valued expressions.

  18. C and Boolean Data Types • Languages without an explicit Boolean data type, like C90 and Lisp, may still represent truth values by some other data type. • C uses an integer type, where relational expressions like i > j and logical expressions connected by && and || are defined to have value 1 if true and 0 if false, whereas the test parts of if, while, for, etc., treat any non-zero value as true. • Indeed, a Boolean variable may be regarded (and be implemented) as a numerical variable with a single binary digit (bit), which can store only two values.

  19. Boolean Types in C after 1999 i.e. Version C99 • The initial standards for the C language (1972) provided no Boolean type; and, to this day, Boolean values are commonly represented by integers (ints) in C programs. • Some of its dialects, like C99 and Objective-C, provide standard definitions of a Boolean type as an integer type and macros for "false" and "true" as 0 and 1, respectively.

  20. NOTE: There is NO Boolean type in C -- you should use char, int or (better) unsigned char. Unsigned can be used with all char and int types.

  21. Boolean Functions Boolean functions are functions that return either TRUE or FALSE.   Until recently, since the C-language did not contain any data types to represent a Boolean value., programmers resorted to defining TRUE and FALSE as preprocessing declarations of one and zero.  

  22. Integers and Bools • The integer one was used to represent a Boolean value of TRUE, while an integer value of zero was used to represent a FALSE. 

  23. typedef enum { false = 0, true = 1 } bool ; The Box 1 program demonstrates a simple example of using a boolean return value from a function to test the passed argument is odd.  The BOOL declaration is used to represent a boolean datatype - really an integer datatype.

  24. Box1 Box 1 // Header Files usually come first #include <stdio.h> // Preprocessing statements are // usually written after Header Files #define TRUE 1 #define FALSE 0 #define BOOL int // Function returns TRUE, if arg is odd BOOL IsOdd(int arg);

  25. Main • ///////////////////////////////////////////// • // main function int • main() • { int num; • printf("Enter a Number: "); • scanf("%d",&num); • // use Boolean Function • if ( IdOdd(num) == TRUE) • { printf("%d is Odd\n",num); } • else { printf("%d is Even\n",num); } • return 0; } • /////////////////////////////////////////////

  26. // IsOdd Function • BOOL IsOdd(int arg) • { if(arg%2==0) • { return FALSE; } • else • { return TRUE; } • }

  27. Note If you noticed the program, the preprocessing define statements have symbols all of uppercase (ie TRUE, FALSE, BOOL).  Preprocessing define statements make programs more readable.  But, they can create subtle errors in the compile phase.  For instance, suppose that the first define statement in the above program was changed to   #define   TRUE   0;.  Now, the symbol TRUE would be always translated into 0; everywhere where the symbol is used.   This leads to many compiler errors where ever the symol TRUE is in your code.  .

  28. Most C/C++ programmers always used uppercase letters for all symbols that are in #define statements.  If a code gets compiler errors on statements containing uppercase symbols, then it may be due to preprocessing #define statement errors. Notice, that the if-else statement in the main function of the Box 1 program has a conditional test of IsOdd(num)==TRUE.  This seems to a be a waste, as the condition can be written simply as IsOdd(num). 

  29. Since the function IsOdd returns a non zero value, the condition is effectively true.  There seems to be no need to perform a second equality test.  But, by introducing the test, the code is more easily read, and errors are cought more easily.   Some programmers often do not put this equality test into Boolean functions.  

  30. Note BOOL IsOdd(int arg) { if(arg%2==0) { return FALSE; } else { return TRUE; } }

  31. Is the same as int IsOdd(int arg) { if(arg%2==0) { return 0; } else { return 1; } }

More Related