180 likes | 348 Views
Introduction to. C programming. C language – Functions. Functions return-type function-name (argument declarations) { declarations; statements; }. C language – Functions. Example - 1. #include <stdio.h> Int square(int); /* function prototype */ main() {
E N D
Introduction to C programming
C language – Functions • Functions return-type function-name (argument declarations) { declarations; statements; }
C language – Functions • Example - 1 #include <stdio.h> Int square(int); /* function prototype */ main() { int i, num; /* declaration */ for (i=1, i<=9; i++) i = square(x); } int square (int mynum) { int y; y = mynum*mynum; return y; } Type must match
C language – Functions • Example - 1 • Note • This is passed by Value • mynum (in function) is a copy of x (in main function) • Change of mynum value does not change x in main
C language – Functions • Example - 2 #include <stdio.h> Int max(int, int, int); /* function prototype */ main() { int a, b, c, large; /* declaration */ large = max (a, b, c); } int square (int x, int y, int z) { int max = x; if (y>x) max = y; if (z>max) max = z;; return max; }
C language – Functions • Scope - 1 • The extent to which an identifier can be referenced • Local to function • Local to file • Global to entire program • Identifier local to function • The variable value is visible to this function only int square (int x, int y, int z) { int value = 5; }
C language – Functions • Scope - 2 • Identifier local to file • The variable maxval is visible in the file #include <stdio.h> int maxval = 10; /* visible to file */ main() { int i, total; /* local variable */ for(i = 0; i < maxval; i++) total += i; } int test (int x) { int value = 5; if (value < maxval) printf(“value is %d”, value); }
C language – Functions • Scope - 3 • Identifier global to program • The variable SIZE is visible in the file File 1 File 2 #include <stdio.h> int SIZE = 100; /* global to file */ main() { int value; /* local variable */ } #include <stdio.h> extern int SIZE; /* global to file */ Other functions
C language – Functions • Scope - example #include <stdio.h> int value; /* visible to file */ static int static_value; /* visible to file */ main() { int value; /* local variable */ large = max (a, b, c); } int square (int x, int y, int z) { int value = 5; }
C language – Functions • Preprocessor • It contains information that will first be separately processed before compilation • 3 types used frequently • #include – file inclusion • e.g. #include <stdio.h> • #define – macro substitution • e.g. #define max(A,B) ((A) > (B) ? (A) : (B)) • #if #endif – conditional inclusion • e.g. #if !define(NULL) #define NULL 0 #endif
C language – Functions • Recursion • A programming method by which a functions calls itself • Adv: A powerful concept • Dis: can strain a computer’s memory resources • Implementation ideas • Divide a problem into a slightly smaller version of the original problem • The recursive function returns a value when a base case is reached • E.g. • Factorial • Quick sort
C language – Functions • Library functions - 1 • Character handling functions • int isdigit(int c) • int isalpha(int c) • int isalnum(int c) • int isxdigit(int c) • int islower(int c) • int isupper(int c) • int tolower(int c) • int toupper(int c) • int isspace(int c) • int iscntrl(int c) • int ispunct(int c) • int isprint( int c) • int isgraph(int c)
C language – Functions • Library functions - 2 • String conversion functions • double atof(const char *nPtr) • int atoi(const char *nPtr) • long atol(const char *nPtr) • double strtod(const char *nPtr, char **endPtr) • long strtol(const char *nPtr, char **endPtr, int base) • unsigned long strtoul(const char *nPtr, char **endPtr, int base)
C language – Functions • Library functions - 3 • Standard I/O functions • int getchar (void) • char *gets(char *s) • int putchar(int c) • int puts(const char *s) • int sprintf(char *s, const char *format, ...) • int sscanf(char *s, const char *format, ...)
C language – Functions • Library functions - 4 • String manipulation functions • char *strcpy(char *s1, const char *s2) • char *strncpy(char *s1, const char *s2, size_t n) • char *strcat(char *s1, const char *s2) • char *strncat(char *s1, const *s2, size_t n)
C language – Functions • Library functions - 5 • Comparison functions • int strcmp(const char *s1, const char *s2) • int strncmp(const char *s1, const char *s2, size_t n)
C language – Functions • Library functions - 6 • Search functions • char *strchr(const char *s, int c) • size_t strcspn(const char *s1, const char *s2) • size_t strspn(const char *s1, const char *s2) • char *strpbrk(const char *s1, cost char *s2) • char *strrchr(const char *s, int c) • char *strstr(const char *s1, const char *s2) • char *strtok(char *s1, const char *s2)
C language – Functions • Library functions - 7 • Memory functions • void *memcpy(void *s1, const void *s2, size_t n) • void *memmove(void *s1, const void *s2, size_t n) • int memcmp(const void *s1, const void *s2, size_t n) • void *memchr(const void *s, int c, size_t n) • void *memset(void *s, int c, size_t n)