Download presentation
Presentation is loading. Please wait.
Published byDorthy Farmer Modified over 8 years ago
1
Tarik Booker CS 242
2
What we will cover… Functions Function Syntax Local Variables Global Variables The Scope of Variables Making Functions Available for Use Passing Arguments by Value Breaking down a program into functions Overloading Functions
3
Functions What is a function? Collection of statements grouped together to perform an operation Way to organize things Easier to reuse code Makes code nicer to read You have already used functions before in class Built-in functions ○ printf() ○ scanf() ○ pow() You will now create your own functions!
4
Function Syntax In order to create our own function, we must define it. Example: return_type function_name(list of parameters) { // Commands }
5
Function Syntax (2) More detail: The function add is defined as finding the addition of two integers Function Definition: int add(int x, int y) { return x+y; }
6
Function Syntax (3) Let’s look at that again: int add(int x, int y) { return x+y; } Return type Function Name Parameters Return Value
7
Function Syntax (4) There are 3 different parts of a function Function Name – specifies all parts of the function Return type – the data type the value of the function returns No return value type? Use void Parameter list ○ A function’s type, order, and number of parameters ○ Parameters are optional ○ A function may contain no parameters
8
Function Syntax (5) Return statement Required for value-returning functions Function terminates when a return statement is executed Value-returning function Function returns a value Void Function Function doesn’t return a value
9
Void Functions Again, void functions simply are functions that have no return value Use void in the return data type
10
Calling a Function We have covered creating a function Declaring it Now we want to use the function We call it We invoke it If a function returns a value, we can treat the function like a value int larger = max(3,4); ○ We have assigned the result of the function to integer larger If the function returns void, we must treat it like a statement printf(“Hello!”); Void function Note: you can treat a value-returning function like a statement, but the returning value will be ignored.
11
Calling a Function (2) When a program calls a function, program control is transferred to the called function The function returns control when return statement is executed, or ending curly brace is reached
12
Calling a function (3) Each time a function is invoked, the system creates an activation record Also called activation frame Stores parameters and variables for the function Places the activation record in a call stack ○ Stored in an area of memory
13
Passing Arguments by Value Functions can work with parameters printf() prints any string add() adds two integers When calling a function, you will typically provide arguments Must be given in the same order as their respective parameters ○ Called parameter order association
14
Passing Arguments by Value (2) Look at this function: void nprintchar(char ch, int n) { for(int i=0; i<n; ++i) printf(“%c”,ch); } This prints the character n number of times You must invoke it in the proper order, however: ○ nprintchar(‘A’, 40); ○ nprintchar(3,’A’); Doesn’t work!
15
Passing Arguments by Value (3) When you invoke a function with an argument, the value of the argument is passed to the parameter Called pass-by-value If argument is a variable, the value of the variable is passed to the function
16
Function Prototypes You can declare a function either before main, or after it If you define a function after main, you must use a function prototype. All this is, is a copy of the full name of the function with a semicolon at the end. Need to tell the compiler that a function will be used (eventually) of this category
17
Function Prototypes (2) Ex The add function: int add(int x, int y) { return x+y; } If this was defined after main, you need to add the prototype before it: ○ int add(int x, int y);
18
Overloading Functions You can create different functions with the same name, as long as their parameters are different Called overloading a function Ex: We used the add function to determine the sum of two integers Now we can make an add function to determine the sum of two doubles!
19
Overloading Functions (2) Let’s create an add function for doubles! double add(double num1, double num2) { return num1 + num2; } This is perfectly legal.
20
Overloading Functions (3) Function overloading is a great way to create function that do the same thing for different data types Great for breaking up programs Can also overload operators ○ (not covered in this class)
21
Overloading Functions (4) Now we have add defined for both integers and doubles add(3, 4)calls the integer version add(3.0, 4.0) calls the double version What about add(3, 4.0)? ○ What version does this call? Compiler tries to find best match ○ 3 is converted to 3.0 and calls the double version ○ add(3.0, 4) also calls the double version
22
Overloading Functions (5) Note: Do not do this: double add(int num1, double num2) { //… } double add(double num1, int num2) { //… } There is no best match for the function Compiler cannot determine what to use ○ Do not do!
23
Overloading Functions (6) Important! You can overload functions by using the same function name with different parameter lists You cannot overload a function by simply changing the return type! ○ DO NOT DO!
24
The Scope of Variables Variable Scope – how accessible a variable is Determined from where it is declared A variable declared in a function is only valid in that function ○ Variables declared in main are only accessible (valid) in main ○ Variables declared in a user-defined function are only accessible in that function Called local variables
25
The Scope of Variables (2) Parameters inside function are also considered local variables Local variables have limited scope ○ Meaning – accessible only in the small areas within their definitions Variables declared in the initial-action part of a for loop also have limited scope Only usable within the for loop
26
The Scope of Variables (3) If you want to create a variable that is accessible by all (functions at all times) you can create a global variable Defined outside of a function!!!!! Ex:int x; ○ (defined outside of main) Usable by all functions! Bad idea to use, though ○ All functions can modify it! ○ Hampers code readibility Why?
27
Breaking up problems When writing large programs, we should break up the program into smaller pieces Decompose the problem into subproblems ○ Subproblems can also be decomposed
28
Breaking up Programs (2) When should I break up a program and use functions? Whenever you repeat code (over and over) Whenever you want to make code easier to read Makes program simpler to understand You can reuse functions Easier to debug, test, and develop ○ You will respect that later… Great for facilitating teamwork!
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.