cs31 lecture 6&7

Not x a useful feature create an alias on a type

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: or a variable! not x A useful feature: create an alias on a type typedef const unsigned int cuint_t; cuint_t myvariable = 42; cuint_t x Can create as many alias on the same type as we want 11 Another example typedef float number_t; number_t anumber; void setANumber(float x) { anumber = x / 2.0; anumber } int main() { int setANumber(42); setANumber(42); std::cout << anumber << std::endl; std::cout return 0; return } 12 Calling a function x Syntax: <function name> ([arg1] [,arg2]) x Functions are expressions of their return type Ex: a function returning int can be used in an int expression x Be careful: void is not an expression type! Cannot put a function of void type in an expression x All arguments must be passed, and of matching type x Arguments may be evaluated in any order 13 Exiting a function x The ‘return xxx’ statement will exit the function The x For void functions, ‘return’ exits the function x The end of the control flow in the function is also the end The of the function of x For functions returning data, all paths in the control flow For that leads to the end of the function must lead to a return statement statement x Sometimes, the compiler may add ‘return xxx’ for int Sometimes, functions functions 14 Exiting a function x The ‘void exit(int val)’ function exits all functions (including The main) when executed main) x break and continue do not exit a function, they only affect break the immediately surrounding loop/switch control flow the x goto will jump to anywhere in the code, including outside goto the function (but should never be used!!) the 15 Another example typedef unsigned int myuint_t; int mydivide(int x) { myuint_t tmp; myuint_t std::cin >> tmp; std::cin if (tmp == 0) { if std::cout << “Error: cannot divide by 0!” << std::endl; std::cout exit(1); exit(1); } if (tmp % 2 == 0 && x == 2) // tmp is even, divide by 2 if x >>= tmp; // bit-shift by tmp >>= else else x /= tmp; /= return x; return } 16 Some observations x If the argument is of type int, float, double, … (a scalar If type) then the argument is copied in a “new” variable in the function (same name, same input value) the x Changing its value inside the function does not change it Changing outside the function (it is a copy) outside x Precise rules about the data being input, output, and Precise modified by a function modified Pure vs. side-effect functions 17 More on function calling x Typical usage: call a function in the code x Recursion: call the function from itself int fib(int n) { if (n < 2) if return n; return return fib(n-1) + fib(n - 2); return } x Pros: natural way of programming math. recurrences x Cons: memory usage 18 The (memory) life of a function x A function has a...
View Full Document

This note was uploaded on 04/03/2014 for the course CS 31 taught by Professor Melkanoff during the Fall '00 term at UCLA.

Ask a homework question - tutors are online