Functions in C. Function Terminology Identifier scope Function declaration, definition, and use...

16
Functions in C

Transcript of Functions in C. Function Terminology Identifier scope Function declaration, definition, and use...

Page 1: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Functions in C

Page 2: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Function Terminology

• Identifier scope

• Function declaration, definition, and use

• Parameters and arguments

• Parameter order, number, and type

• Function prototype

• Input parameter, output parameter

• Pass by value, pass by reference

Page 3: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Sample Programint a = 1;

int findMax(int x, int y);

int main(void){const int j = 10;int k;k = findMax(a, j);return (0);} // End main int findMax(int x, int y){int z;if (x < y) z = y; else z = x;a = z;return (z);} // End findMax

Page 4: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Identifier Scope (i.e., visibility) int a = 1;

int findMax(int x, int y);

int main(void){const int j = 10;int k;k = findMax(a, j);return (0);} // End main int findMax(int x, int y){int z;if (x < y) z = y; else z = x;a = z;return (z);} // End findMax

Global scopefor variable a

Local scopefor j, k

Local scopefor x, y, z

Reading from aglobalvariable

Writing toa globalvariable

Page 5: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

• Declaring a function tells the compiler the function exists. The parameters tell the order, number, and type of arguments the function expects and the type of the value the function returns. The identifier for each parameter is optional but recommended. A function declaration is also called a function prototypefloat minValue (float x, float y);

• Defining a function tells the compiler what the function doesfloat minValue (float x, float y){if (x < y) return x; else return y;} // End minValue

• Using (calling) a function tells the compiler to switch control to this function and pass the argument values to the function c = minValue(a,b);

Syntax for Function Declaration, Definition, and Use

Page 6: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Example of Declaration, Definition, Use

int a = 1;

int findMax(int x, int y);

int main(void){const int j = 10;int k;K = findMax(a, j);return (0);} // End main int findMax(int x, int y){int z;if (x < y) z = y; else z = x;a = z;return (z);} // End findMax

Function declaration(prototype)

Function definition(and declaration)

Function use(and implicit declaration)

Function definition(and declaration)

Page 7: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Coding Standards for Functions

• Function names follow the same naming standard as variables

• Explicitly declare all programmer-defined functions before defining or using them. This is done by placing all function prototypes just above the definition of the function main

• If the function main is defined in a source code file, it's definition should occur before the definition of any other functions in the file

Page 8: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Location of Function Parts in a File// Include files#include <stdio.h>

// Global constants and types

// Function prototypes

int main (void){// Function calls

return (0);} // End main

// Function definitions

Page 9: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Function Parameters and Arguments

• All arguments in C are "pass by value". This means that the only way to get a value out of a function is by using the return keyword and giving it the value

• Consequently, a function can never return more than one value and all function parameters are really only input parameters

• However, "pass by reference" can be implemented in C through the use of pointers (i.e., memory addresses)

• This allows function parameters to "act like" output parameters

Page 10: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Output Parameter Example

#include <stdio.h>

int swap (int *x, int *y);

int main (void){int a = 27;int b = 56;// swap(a, b); // Wrongswap (&a, &b); // Rightreturn(0);} // End main

"int *" is a pointer type, sothe type of x or y is a pointer,which is a memory address

The * here is NOT anoperator in this case. Itis a decoration that goeswith the type

The & is the "address of"operator. It returns the memoryaddress of the variable

This is read as "the address of a"

Page 11: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Output Parameter Example (continued)

int swap (int *x, int *y){int temp;temp = *x;*x = *y;*y = temp;return(0);} // End swap

"int *" is a pointer type, sothe type of x or y is a pointer,which is a memory address

The * here is NOT anoperator in this case. Itis a decoration on thevariable name

This * here IS an operator. Itis called the indirectionoperator.

In the location named temp, store the value pointed to by xIn the location pointed to by x, store the value pointed to by yIn the location pointed to by y, store the value in temp

Page 12: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Interpreting the & Operator

• & is the address operator (among other uses in C)• When applied to a variable, the & returns the memory address of that

variable. That memory address value is then passed as an argument to the function swap (&a, &b);

• This memory address can be thought of as the address of the "mailbox" with the variable name written on the outside (i.e., this is the "mailbox" of currentAmount)

• By passing a variable's "mailbox" address as a parameter to a function, the function can then put a value into the "mailbox"

• This is the first step the programmer needs to do to implement "pass by reference"

• After the function call is complete, the value of the variable is the value that the function put in the "mailbox"

Page 13: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Interpreting the * Operator• * is the indirection operator (among other uses in C)• When applied to a variable on the right side of an assignment statement, the * operator returns the value located in the "mailbox" of the memory address contained in the variable.temp = *x;

• When applied to a variable on the left side of an assignment statement, the * operator stores the value on the right side of the assignment statement into the "mailbox" of the memory address contained in the variable on the left side.*y = temp;

• If the * indirection operator occurs on both sides of the assignment statement, it means the following for the example below. The value pointed to by y is stored in the memory location pointed to by x *x = *y;

Page 14: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Interpreting the * Operator (continued)

• In the parameter list of a function, an output parameter is created by declaring the parameter as a pointer to the value that the function will "return"

int swap (int *x, int *y); • This is the second step the programmer needs to do to

implement "pass by reference"

Page 15: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

Standard C Library Functions

• A program can call on a large number of functions from the Standard C library

• These functions perform essential services such as input and output

• They also provide efficient implementations of frequently used operations

• All the functions in the C library are declared in one of the standard header files. One of these files is stdio.h

• Check out references to the Standard C Library for more information

Page 16: Functions in C. Function Terminology Identifier scope Function declaration, definition, and use Parameters and arguments Parameter order, number, and.

printf and scanf Functions

• The printf and scanf functions are declared in the source code file named stdio.h that is located in the standard …\include folder location. Their prototypes are inserted into a program's source code file by using #include <stdio.h>

• The printf and scanf functions are defined in the object code library file named libc.a that is located in the standard …\lib folder location

• The printf and scanf functions are used by placing them in a program's source code fileprintf("The value is %d", accountValue);scanf("%f", &salesAmount);