230 likes | 379 Views
Functions. Introduction. Most programs are much larger than you have worked with so far Its good practice to break these programs into smaller modules Makes program more manageable “Divide and Conquer” In C, these modules are called “Functions”. Why functions?.
E N D
Introduction • Most programs are much larger than you have worked with so far • Its good practice to break these programs into smaller modules • Makes program more manageable • “Divide and Conquer” • In C, these modules are called “Functions”
Why functions? • Writing functions avoids rewriting the same code over and over. • IT become easy to keep track of what you are doing .
Types of Functions • Programmer-defined functions • Already available functions (Standard Library)
Function Result Parameters Function Input and Output • Return a value • Display or print • Modify memory
How Functions work? main worker 1 worker 2 worker 3 worker 4 worker 5
Function Mechanics • Functions generally have the following properties: • Each function has a name. • A function name is made up and assigned by the programmer and follows the naming rules. • Each function name has one set of parentheses immediately following it. • The body of each function, starting immediately after the closing parenthesis of the function name, must be enclosed by braces. • Function may take input. • Function may return the value. • Prototyping of the function.
Example: function (without parameters) main() { message();//function gets called ; cout<<“\n cry and you stop the monotony!” } message() //function definition { cout<<“\n smile and the world smiles with you..” }
Function Definition • Implementation of task to be performed by a function • Two parts: • Function signature • Function body int Square (int x) { return x * x; }
Function Prototype • Just below pre-processor directives • Tells compiler • type of data returned by function • Number of parameters expected by function • Types of parameters • Order of parameters • Used to validate function call int Square (int);
Function Call • Calling function where its functionality is required • Pass arguments as defined through parameter list int result; result = Square (a);
Sample function Return type parameters Function name int add2ints(int a, int b) { return(a+b); } Functionbody
Testing add2nums int main(void) { int y,a,b; cout << "Enter 2 numbers\n"; cin >> a >> b; y = add2nums(a,b); cout << "a is " << a << endl; cout << "b is " << b << endl; cout << "y is " << y << endl; return(0); }
int add2nums( int firstnum, int secondnum ) { int sum; sum = firstnum + secondnum; return(sum); }
What happens here? int add2nums(int a, int b) { a=a+b; return(a); } … int a,b,y; … y = add2nums(a,b);
Pass by Value • Different location in memory • Changes to the parameters inside the function body have no effect outside of the function.
Pass by Value: Example 1 For example, consider the following code: int sum(int a, int b){ a = a + b; return a; } void main(){ int x, y, z; x = 3; y = 5; z = sum(x,y); } What is the value of x, y, and z at the end of the main() program?
Pass by Value: Example 1 • The answer: 3, 5, and 8. (x,y,z) • Even though the value of parameter a is changed, the corresponding value in variable x does not change. • This is why this is called pass by value. • The value of the original variable is copied to the parameter, but changes to the value of the parameter do not affect the original variable. • In fact, all information in local variables declared within the function will be lost when the function terminates. • The only information saved from a pass by value function is in the return statement.
Pass by Value: Example 2 void Increment(int Number) { Number = Number + 1; cout << "Number =" << Number << endl; } void main() { int I = 10; cout << "I is: " << I << endl; Increment(I); cout << "I is: " << I << endl; }
Passing Values to Functions • Many real-world functions you perform require that you provide information • A particular task might always be carried out in the same way, but with specific data • Consider a program that computes the amount of sales tax due on an item • You can write the prototype for computeTax() in one of two ways: void computeTax(int); OR void computeTax(int price);
Arguments to Functions and Return Types of Functions • A function can contain a variety of combinations of actions • Some functions contain local variables declared within the function body • Some functions return and receive nothing • Others return values, receive values, or both • Functions may receive any number of variables as parameters, but may return, at most, only one variable of one type