Top Banner
Functions in C
31

Functions in C. Consider #include main() { int i; for(i=1; i

Dec 30, 2015

Download

Documents

Ronald Mcgee
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Functions in C. Consider #include main() { int i; for(i=1; i

Functions in C

Page 2: Functions in C. Consider #include main() { int i; for(i=1; i

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; }

Page 3: Functions in C. Consider #include main() { int i; for(i=1; i

Life would be easier

• If we could call a function to do the same• Consider the following

Page 4: Functions in C. Consider #include main() { int i; for(i=1; i

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

Page 5: Functions in C. Consider #include main() { int i; for(i=1; i

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

Page 6: Functions in C. Consider #include main() { int i; for(i=1; i

The syntax of a function is

Page 7: Functions in C. Consider #include main() { int i; for(i=1; i

type name(type1 arg1, type2 arg2, ...) { /* code */ }

Page 8: Functions in C. Consider #include main() { int i; for(i=1; i

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

Page 9: Functions in C. Consider #include main() { int i; for(i=1; i

Another

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

Page 10: Functions in C. Consider #include main() { int i; for(i=1; i

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

Page 11: Functions in C. Consider #include main() { int i; for(i=1; i

Example

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

Page 12: Functions in C. Consider #include main() { int i; for(i=1; i

Program Body

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

Page 13: Functions in C. Consider #include main() { int i; for(i=1; i

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

Page 14: Functions in C. Consider #include main() { int i; for(i=1; i

Note

• Result of function is attached by return to function type indicated by

type name(type1 arg1, type2 arg2, ...) { /* code */ }

Page 15: Functions in C. Consider #include main() { int i; for(i=1; i

Functions and Truth Values

Page 16: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 17: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 18: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 19: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 20: Functions in C. Consider #include main() { int i; for(i=1; i

• 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.

Page 21: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 22: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 23: Functions in C. Consider #include main() { int i; for(i=1; i

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.

Page 24: Functions in C. Consider #include main() { int i; for(i=1; i

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);

Page 25: Functions in C. Consider #include main() { int i; for(i=1; i

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; } • /////////////////////////////////////////////

Page 26: Functions in C. Consider #include main() { int i; for(i=1; i

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

Page 27: Functions in C. Consider #include main() { int i; for(i=1; i

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.

• .

Page 28: Functions in C. Consider #include main() { int i; for(i=1; i

• 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).

Page 29: Functions in C. Consider #include main() { int i; for(i=1; i

• 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.

Page 30: Functions in C. Consider #include main() { int i; for(i=1; i

Note

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

Page 31: Functions in C. Consider #include main() { int i; for(i=1; i

Is the same as

int IsOdd(int arg) { if(arg%2==0) { return 0; } else { return 1; } }