C Programming Language

Department of Electrical Engineering C Programming Language Functions Dr. Manar Mohaisen Office: F208 Email: [email protected] School of IT Eng...
Author: Gavin Small
2 downloads 0 Views 109KB Size
Department of Electrical Engineering

C Programming Language Functions

Dr. Manar Mohaisen Office: F208 Email: [email protected]

School of IT Engineering

Korea University of Technology & Education

Department of Electrical Engineering

Review of Precedent Lecture ₪ Character Strings and Formatted I/O ₪ Single-character I/O ₪ Input Validation

Korea University of Technology & Education

Department of Electrical Engineering

Class Objectives ₪ Introduction ₪ Explain How to Create Simple Functions ₪ Explain Types of Function Arguments ₪ Explain the Use of the & Operator ₪ Can Functions Modify their Arguments? ₪ Discussions

Korea University of Technology & Education

Department of Electrical Engineering

Introduction ₪ Functions ■ A function is a self-contained unit of program designed to accomplish a particular task. ■ Why do we need functions ► Avoid repetitions; a function can be then used several times in the same program or even reused in other programs. ► Functions make program easier to read, debug and build over it. ► The possibility of creating functions makes it possible to work in team.

Korea University of Technology & Education

Department of Electrical Engineering

Creating a Simple Function ₪ Example

# include # include # define LIMIT 10 double mean_fnc(int sequence[]);// function declaration void main() { int a[LIMIT], ii = 0; // an array of integers double result; for(ii = 0; ii < LIMIT; ii++) a[ii] = rand(); // generate a random integer result = mean_fnc(a); printf(“%f\n", result); } double mean_fnc(int sequence[]) // function definition { int ii = 0, out = 0; for(ii = 0; ii < LIMIT; ii++) out += sequence[ii]; out = out / LIMIT;

Output

return out; 17849

}

Korea University of Technology & Education

Department of Electrical Engineering

Functions ₪ In General, Each Function Has: ■ Function prototype (declaration): Tells the compiler what sort of function is mean_fnc; what arguments does it take and what outputs does it return, if any. The function declaration is followed by a semi-colon. ■ Function call: Causes the function to be executed. ■ Function definition: Specifies what the function does.

₪ Function Name ■ Is the name that precedes the left parenthesis. Ex., main()

₪ Function Type ■ Is the identifier that precedes the function name and indicates the type of the returned value, if any. ► void main(void), int square(int a) ● void functions don’t return values.

Korea University of Technology & Education

Department of Electrical Engineering

Functions – contd. ₪ Function Arguments ■ Arguments are placed between the parentheses ► int mean_fnc(int sequence[]); ► void main(void); the function does not have any argument.

₪ Place of Function Declaration ■ The declaration of a function can be inside another function. For example, mean_fnc can be declared inside main(), but it will be local function to the main function. [Not recommended] # include int dummy_fnc(int c[]){ // Can’t see the mean_fnc function // do something } void main() { int mean_fnc(int sequence[]); // function declaration // mean_fnc is local to main } int mean_fnc(int sequence[]) // function definition { // do something; } Korea University of Technology & Education

Department of Electrical Engineering

Function Arguments #include #include // for strlen() ₪ Example #define NAME "GIGATHINK, INC." /* show_n_char() definition */ #define ADDRESS "101 Megabuck Plaza" void show_n_char(char ch, int num) #define PLACE "Megapolis, CA 94904" { #define WIDTH 40 int count; #define SPACE ' ' for(count = 1; count b) return a; else return b; }

int imax(int a, int b) { return( (a > b)? a : b ); }

■ Calling a function that returns a value y = 3 * imax(a, b) – 7; y = imax(a, b); y = imax(a, (b – 7)/2); double z = 10.; y = imax(a, (int) z);

Korea University of Technology & Education

Department of Electrical Engineering

Recursion ₪ Definition ■ Recursion is the process when a function calls itself. ■ Recursion can be seen as a loop where at each iteration the function calls itself. ■ Whenever possible, use loops instead of recursion to make your code easier to understand.

Korea University of Technology & Education

Department of Electrical Engineering

Dealing with Addresses: The & Operator ₪ Pointer ■ A pointer is a variable used to store an address. ► printf(“%d %p\n”, sum, &sum); might produce 10 0012FFC1 ● Note that %p is the specifier for address. ● The unary & operator gives the address where the variable is stored. Example: &a is the address where a is stored. ► Pointers are used as arguments when functions are supposed to alter the argument variables.

■ When Formal arguments are used without the & operator, ► The called function just gets a copy of its arguments without altering the variables in the calling function.

Korea University of Technology & Education

Department of Electrical Engineering

The & Operator – contd. #include void mikado(int); int main(void) { int pooh = 2, bah = 5;

/* declare function */

/* local to main() */

printf("In main(), pooh = %d and &pooh = %p\n", pooh, &pooh); printf("In main(), bah = %d and &bah = %p\n", Output bah, &bah); mikado(pooh); In main(), pooh = 2 and &pooh = 0012FF7C return 0; } void mikado(int bah) { int pooh = 10;

In main(), bah = 5 and &bah = 0012FF78 In mikado(), pooh = 10 and &pooh = 0012FF1C In mikado(), bah = 2 and &bah = 0012FF28

/* define function */ /* local to mikado()*/

printf("In mikado(), pooh = %d and &pooh = %p\n", pooh, &pooh); printf("In mikado(), bah = %d and &bah = %p\n", bah, &bah); } Korea University of Technology & Education

local variable to mikado(). No relation with the local variable to main() pooh. Copy of the main variable bah, stored in a different address.

Department of Electrical Engineering

Introduction to Pointers # include void interchange(int u, int v);

Output Originally x = 5 and y = 10 In main(), &x = 0012FF7C and &y = 0012FF78 Now x = 5 and y = 10 In interchange(), &x = 0012FF24 and &y = 0012FF28

void main(void) { int x = 5, y = 10; printf("Originally x = %d”, x); printf(“ and y = %d.\n“, y); printf(“In main() &x = %p and &y = %p\n”, &x, &y); interchange(x, y); printf(“Now x = %d”, x); printf(“ and y = %d.\n“, y); } void interchange(int x, int y) { int temp; printf(“In interchange() &x = %p and &y = %p\n”, &x, &y); temp = x; x = y; y = temp; }

Korea University of Technology & Education

Local copies of the arguments are interchanged but values of the arguments in the main() function remain intact.

What is the solution? Solution is Pointers.

Department of Electrical Engineering

Introduction to Pointers – contd. ₪ Pointer ■ int * ptr;

(the int type is the type of data ptr points to)

■ ptr = &x; ► ptr is a pointer that will point to the address of x. ► You can reassign ptr. Ex.: ptr = &y; ● Thus, ptr is a variable ► x = *ptr; (x gets the value stored in the memory address that ptr points to).

■ Another example ► int x = 30;

(x is declared and assigned the value 30)

► int * ptr = &x;

(ptr is declared as an int pointer and assigned the address of x)

► int val = *ptr;

(val is declared and assigned the value that ptr points to, i.e., 30). The * (pronounced asterisk) is referred to as the indirection operator.

Korea University of Technology & Education

Department of Electrical Engineering

Introduction to Pointers – contd. #include void interchange(int * u, int * v); //arguments are pointers int main(void) { int x = 5, y = 10; printf("Originally x = %d and y = %d.\n", x, y); interchange(&x, &y); /* send addresses to function printf("Now x = %d and y = %d.\n", x, y);

*/

return 0; } void interchange(int * u, int * v) { int temp; temp = *u; *u = *v; *v = temp; }

Korea University of Technology & Education

address (&x and &y)

0012FF1C

0012FF28

value in the memory address

5

10

variable name

x

y

Department of Electrical Engineering

Functions – Homework ₪ Functions in Separate Files File: vectorMath.c

File: vectorMath.h

Definition of the functions declared in vectorMath.h

double power_fnc(int a[]); int sum_fnc(int a[]); double var_fnc(int a[]); double std_fnc(int a[]); int max(int a[]); int max(int a[]); int mean_fnc(int a[]);

[Homework]

File: main.c

# include // standard library # include // standard library # include “vectorMath.h” // user library void main() { int a[15]; for(ii = 0; ii < 15; ii++) a[ii] = rand(); // generate a random integer // Call the functions declared in vectorMath.h, // print a[], and the outputs of all the functions on the // screen } Korea University of Technology & Education

Department of Electrical Engineering

Summary & Discussion ₪ Explained How to Create Simple Functions ₪ Explained Types of Function Arguments ₪ Explained the Use of the & Operator ₪ Answered the question ■ Can Functions Modify their Arguments?

₪ Discussion

Korea University of Technology & Education