Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 6 Functions, Parameters and Arguments.

Similar presentations


Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 6 Functions, Parameters and Arguments."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 6 Functions, Parameters and Arguments

2 Structure of a C Program The Function main In C, all code is packaged in functions main is the function that the system calls when you run your program Every program must have one and only one main All functions may return a value Main statement Call function1 statement Call function 2 return Function 1 Function 2

3 Prototypes Prototypes allow Description of all functions to be in one place Main program containing overall structure comes first Maps to functional decomposition/top down development Speeds up compilation process

4 Structure of a C Program Main statement Call function1 statement Call function 2 return Function 1 Function 2 Prototype Function1 Prototype Function2

5 Function Prototypes int power(int, int); main() { int x, y, z; float a, b, c; c = power(a, b); z = power(a, b); z = power(x, y); } /* end main */ /* An exponentiation function */ int power(int b, int e) {... return ans; } /* end power */ Function prototypes allow the compiler to check that the data-types specified in function declarations and invocations are coded consistently.

6 Function Components A function definition consists of two parts; A Specification ( or header or interface); Specifies the function name, the return value and argument list A Body ( or block or implementation); Code to be executed when the function is called A sequence of statements enclosed in braces

7 Format return-type name (parameter list) Prototype int multiply(int,int) Function header int multiply(int num1, int num2) Usage total = multiply(n1,n2) (where total,n1 and n2 are declared as integer)

8 Parameters and arguments Example a function to add up two values and return the sum #include int mult(int,int) /* prototype*/ int main(void) { int total,n1=3,n2=5; total = mult(n1,n2); /*function called – returns total*/ printf(“The total is %d”,total); return 0; } int mult(int num1,int num2) { return(num1*num2); } parameters arguments

9 Example invocations: x = fname(arg1, arg2); fname(arg1, arg2); x = fname(); When functions are used well it is possible ignore exactly how a task is done since knowing what the function does is sufficient. Functions can allow code to be reused: making programs more robust: reducing coding errors and simplifying program maintenance. Function prototypes can also be used to improve the compile-time elimination of errors.

10 The scope of variables when using functions is one of the most important considerations in program design /* Prototypes */ int noddy(int) int bigears(int) /* Global Variables */ int z; main() { int x = 10; z = noddy(x);z=11, x=10 z = bigears(x);z=32, x=10 } /* end main */ /* function noddy */ int noddy(int b)b=10 { return ++b;retval is 11 } /* end noddy */ /* function bigears */ int bigears(int a)a=10 { a = z+a;a=21, z=11 return z+a;retval is 32 } /* end bigears */

11 Memory Addresses 00000000 z 00001010 00000000 00001010 x b 00000000 a 00001010

12 Passing by value Passing by value: The actual values passed as arguments to functions are a copy of the the value of the argument at the point of invocation. Independence: This is generally a desirable feature since it allows the programmer coding each function to work completely independently the code found elsewhere in the same program. (The only restriction being the function’s return type and the types and order of its arguments.) Complication: But the independence obtained from passing by value in this way can also cause problems. Eg suppose the writer of function bigears really intended to update the value of x in the program’s main function? Solution: make x a global variable similar to A ?

13 Global variables: misuse/abuse Global Variables The global-isation of otherwise trivial program variables is a recipe for disaster! Why not make every variable a global variable? Compromising functional independence is the reason why only important and widely used program variables and data structures should ever be declared globally - and only then in a carefully planned & managed way! A better solution: pass the arguments you want the function to update by reference rather than by value.

14 Passing by reference This modified noddy & bigears program has a different effect on the value of x in main. /* Global Variables */ int z; main() { int x = 10; z = noddy(x);z=11, x=10 z = bigears(&x);z=32, x=21 } /* end main */ /* function noddy */ int noddy(int b)b=10 { return ++b;retval is 11 } /* end noddy */ /* function bigears */ int bigears(int *a)*a=10 { *a = z + *a;*a=21, z=11 return z + *a;retval is 32 } /* end bigears */

15 Memory Addresses 00000000 z 00001010 00000000 00001010 x b f7fffbf9 a

16 Pointers What is a C pointer? Every variable in a C program (whatever its type: int, float, double, char, array or structure) occupies an area of storage (or computer memory) reserved for it at compile-time. For the primitive data types the exact amount of storage used per variable is platform dependent and not specified in the C language. But C compilers always know exactly how much storage to reserve for each primitive data type, on a particular machine (lecture1: sizeof an int was 4 bytes...). The variable declaration statement is the point at which the compiler reserves the storage and the address of the variable (relative to the start of the compiled program) is fixed at that time. C pointers access the contents of variables via their memory location (or address) rather than by name!

17 Pointer Creation Pointers have to be declared, just like other variables: int j, k, *ip; float g, h, *fp; double m, n, *dp; char a, b, *cp; Pointer variable names prefixed by * when declared Good idea to give some indication that the variable is a pointer when choosing a name - but not obligatory. Declaration of each pointer reserves an area of storage for holding an address: this address will be the reference that the pointer holds to its target object. Each pointer variable carries info about the size (in bytes) of the type of object to which it can refer. (Each pointer variable knows the value returned by sizeof for an object of its target data-type.)

18 Pointer Operations The address stored as the pointer’s reference can be updated using the unary & operator eg: int x, y, *ipA *ipB; x = 15; ipA = &x; &x gives ?$*!£~? ipB = ipA; ipB ’s reference is ?$*!£~? where ‘?$*!£~?’ is the meaningless number which corresponds to the address of integer variable x. ipA and ipB both now point to x. Once the pointer’s reference has been updated the pointer can be used to manipulate the target address location using the unary dereferencing operator * eg: y = *ipA; y holds 15

19 Pointer Operations A dereferenced pointer can be used anywhere that the target object could be used: printf(“%d”, ip); gives >?$*!£~? whereas printf(“%d”, *ip); gives >15 Pointer variables can be subjected to pointer arithmetic eg: int n[10], *ip; char g[10], *cp; ip = &n[0]; cp = &g[0]; ++ip; ip holds the address of n[1] ++cp; cp holds the address of g[1] Note, pointer arithmetic is consistent with the pointer variable’s type: so ip advances 4 bytes with every ++ip whereas cp advances only one byte for every ++cp.

20 Functions separate program into task-specific chunks Prototypes aid compilation and readability Return value updates linked variable in calling function Variables passed by value are copied into local variables Passing by reference allows linked variables to be updated Uses pointers Pointers point to the address of another variable


Download ppt "Introduction to C Programming CE00312-1 Lecture 6 Functions, Parameters and Arguments."

Similar presentations


Ads by Google