3.2 - Functions
What is Function?
- An activity that is natural to or the purpose of a person or thing. "bridges perform the function of providing access across water"
- A relation or expression involving one or more variables. "the function (bx + c)"
- In programming languages it can be something which performs a specific service or a task
- Generally a function has 3 properties
– Takes Input
– Perform Operation
– Generate Output
Writing a Function..
Syntax
return_data_type function_name(arg_1, arg_2, ..., arg_n) { /* Function Body */ }Example
int function(int arg_1, int arg_2) { /* Function Body */ }How to Call a Function:
#includeint main() { int x, y; x = 2; y = foo(x); //The Fucntion get's Called here.. printf(“y is %d\n”, y); return 0; } int foo(int x) { int ret = 0; ret = x + 1; return ret; }
Advantages of using Function:
- Re usability
- Functions can be stored in library & re-used
- When some specific code is to be used more than once, at different places, functions avoids repetition of the code.
- Divide & Conquer
- A big & difficult problem can be divided into smaller sub-problems and solved using divide & conquer technique
- Modularity can be achieved.
- Code can be easily understandable & modifiable.
- Functions are easy to debug & test.
- One can suppress, how the task is done inside the function, which is called Abstraction
Detailed look of function:
#includeint main() // The main function { int num1 = 10, num2 = 20; int sum = 0; sum = add_numbers(num1, num2); //The function call printf(“Sum is %d\n”, sum); return 0; } int add_numbers(int num1, int num2) //'int' is a Return type { int sum = 0; sum = num1 + num2; // Function return sum; // Return from function }
Function & Stack
Functions – Parameter Passing Types:
- Difference between Pass by Value & Pass by reference
- This method copies the actual value of an argument into the formal parameter of the function.
- In this case, changes made to the parameter inside the function have no effect on the actual argument.
Pass by Reference:
- This method copies the address of an argument into the formal parameter.
- Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.
#includeint add_numbers(int num1, int num2); int main() { int num1 = 10, num2 = 20, sum; sum = add_numbers(num1, num2); printf(“Sum is %d\n”, sum); return 0; } int add_numbers(int num1, int num2) { int sum = 0; sum = num1 + num2; return sum; }
Example for Pass by Value:
#includevoid modify(int *num_ptr) { *num_ptr = *num_ptr + 1; } int main() { int num = 10; printf(“Before Modification\n”); printf(“num1 is %d\n”, num); modify(&num); printf(“After Modification\n”); printf(“num1 is %d\n”, num); return 0; }
Working - Pass by Value :
Working - Pass by Reference :
Advantages of Pass by Reference :
- Return more than one value from a function
- Copy of the argument is not made, making it fast, even when used with large variables like arrays etc.
- Saving stack space if argument variables are larger (example – user defined data types)
Passing Array in Function
#includevoid print_array( int array[] ); int main() { int array[5] = {10, 20, 30, 40, 50}; print_array(array); return 0; } void print_array(int *array[]) { int iter; for (iter = 0; iter < 5; iter++) { printf(“Index %d has Element %d\n”, iter, array[iter]); } }
Functions – return type
- Local return
- Void return
...
Post a Comment