2.52k likes | 5.99k Views
C Functions. Programmer-defined functions – Functions written by the programmer to define specific tasks. Functions are invoked by a function call . The calling function asks the called function to perform a task.
E N D
C Functions • Programmer-defined functions – Functions written by the programmer to define specific tasks. • Functions are invoked by a function call. • The calling function asks the called function to perform a task. • When the task is done, the called function reports back to the calling function.
Functions • Functions allow the programmer to modularize a program. • All variables declared in functions are local variables – they are known only in the function in which they are defined. • Format: return-value-type function-name( parameter-list) { declarations statements }
NOTE: • function-name valid user defined identifier • return-value-type data type of the result returned to the caller • ( parameter-list ) list of parameters passed to the function • The return-value-type void indicates that a function does not return a value. • By default, this return-value-type is int. • If a function does not receive any parameters, the parameter list is void.
main() Function main() follows the similar syntax. void main() { /* declarations */ /* statements */ }
Returning control to the calling function • Control is returned when the function ending right brace } is reached. • The statement return; also passes control to the calling function. • If a function returns a result to the calling function, the statement return expression; is written in the body of the called function to return the value of the expression to the caller.
int main() { /* declarations */ /* statements */ return 0; } Here, return-value-type of main() is int.
Function Prototypes • A function prototype tells the compiler: type of data returned number of parameters the function will receive data types of parameters order in which these parameters are expected. • The compiler uses function prototypes to validate function calls. • A function call that not match the function prototype causes a syntax error. • The function prototype must match the function definition.
Program to find the maximum of two integers #include <stdio.h> int maximum(int, int); int main() { int a,b; printf(“Enter two integers: \n”); scanf(“ %d %d “, &a,&b); printf(“Maximum is %d \n”,maximum(a,b)); return 0; } int maximum(int x, int y) { int max = x; if ( y > max) max = y; return max; }
Call by Value • Call by value • When arguments are passed call by value, a copy of the argument’s value is made and passed to the called function. • Changes to the copy do not affect an original variable’s value in the calling function.
Recursion • A recursive function is a function that calls itself through another function. • Factorial of an integer n! n! = n (n-1)(n-2)(n-3)…….1 • 1! = 1 • 2! = 2 (1) = 2 • 3! = 3 (2) (1) = 6 • 3! is also 3 (2!) = 6 because 2! = 2(1) • 4! = 4 (3) (2) (1) = 24 • 4! is also 4 (3!) = 24
Expressing factorial of n as a recursive function 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Therefore, n! = n (n-1)!
Program to find the factorial of an integer #include <stdio.h> int factorial( int ); int main() { int j; printf(“Enter an integer \n”); scanf(“%d”,&j); printf(“Factorial is %d \n”,factorial(j)); return 0; } int factorial( int number) { if (number <= 1) return 1; else return (number * factorial (number - 1)); }
Storage Classes • A storage class determines a storage duration and scope. • Storage duration – Period during which the identifier exists in memory • Storage scope – Where the identifier can be referenced in the program • Some identifiers can be referenced through out the program. • Other identifiers are referenced only in certain portions of the program.
C provides 4 storage class specifiers : auto, register, extern, static • The 4storage class specifiers can be split into 2 storage durations: automatic storage duration static storage duration
Automatic Storage Duration • Variables with automatic storage duration are created when the block in which they are declared is entered. They exist while the block is active. They are destroyed when the block is exited. • Keywords auto and register are used to declare variables of automatic storage duration. • Only variables can have automatic storage duration. • Example: Function’s local variables. • Local variables have automatic storage duration by default.
Static Storage Duration • Static storage duration – Identifiers exist from the point at which the program begins execution. • extern – Global variables and function names • Global variables are created by placing the declarations outside any function definition. They retain their values through out the execution of the program. • static - Static variables retain their value when the function is exited. • static int number = 1; • Static local variables are known only in the function in which they are defined.