Download presentation
Presentation is loading. Please wait.
Published byMae Stanley Modified over 6 years ago
1
Module 4 Functions – function definition and function prototype.
Function call by value and call by reference. Pointer to a function Recursive functions.
2
Functions A function is a group of statements that together perform a task. Every C program has at least one function, which is main() A function is a group of statements that together perform a task. Every C program has at least one function, which is main(), and all the most trivial programs can define additional functions.
3
USES OF C FUNCTIONS: C functions are used to avoid rewriting same logic/code again and again in a program. There is no limit in calling C functions to make use of same functionality wherever required. can call functions any number of times in a program and from any place in a program. A large C program can easily be tracked when it is divided into functions. The core concept of C functions are, re-usability, dividing a big task into small pieces to achieve the functionality and to improve understandability of very large C programs.
4
There are two types of functions in C programming:
Standard library functions User defined functions
5
Standard library functions
built-in functions in C programming To handle tasks such as mathematical computations, I/O processing, string handling etc. These functions are defined in the header file. When include the header file, these functions are available for use. Example printf() – stdio.h
6
User-defined functions
C allows programmers to define functions created by the user and is called user-defined functions.
7
There are 3 aspects in each C function.
Function declaration or prototype – This informs compiler about the function name, function parameters and return value’s data type. Function call – This calls the actual function Function definition – This contains all the statements to be executed.
8
Synatx C functions aspects syntax function definition
Return_type function_name (arguments list) { Body of function; } function call function_name (arguments list); function declaration return_type function_name (argument list);
10
General Form return_type function_name( parameter list ) { body of the function }
11
Return Type − A function may return a value
Return Type − A function may return a value. The return_type is the data type of the value the function returns. Some functions perform the desired operations without returning a value. In this case, the return_type is the keyword void. Function Name − This is the actual name of the function. The function name and the parameter list together constitute the function signature. Parameters − A parameter is like a placeholder. When a function is invoked, you pass a value to the parameter. This value is referred to as actual parameter or argument. The parameter list refers to the type, order, and number of the parameters of a function. Parameters are optional; that is, a function may contain no parameters. Function Body − The function body contains a collection of statements that define what the function does.
12
Example int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result; }
13
Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately. Syntax: return_type function_name( parameter list ); Example : int max(int num1, int num2); Parameter names are not important in function declaration only their type is required int max(int, int); Function declaration is required when define a function in one source file and call that function in another file. In such case, should declare the function at the top of the file calling the function.
14
Calling a Function To use a function, it is necessary to call that function to perform the defined task. When a program calls a function, The program control is transferred to the called function. A called function performs a defined task when its return statement is executed or when its function-ending closing brace is reached, it returns the program control back to the main program. To call a function, simply need to pass the required parameters along with the function name, and if the function returns a value, then we can store the returned value.
15
#include <stdio.h> int max(int num1, int num2); int main () { int a = 100; int b = 200; int ret; ret = max(a, b); printf( "Max value is : %d\n", ret ); return 0; } int max(int num1, int num2) { int result; if (num1 > num2) result = num1; else result = num2; return result;
16
Passing arguments to a function
17
Return Statement
18
Passing Argument to Function :
Two Ways of Passing Argument to Function in C Language : Call by Reference Call by Value
19
#include<stdio.h> void interchange(int number1,int number2) { int temp; temp = number1; number1 = number2; number2 = temp; } int main() { int num1=50,num2=70; interchange(num1,num2); printf("\nNumber 1 : %d",num1); printf("\nNumber 2 : %d",num2); return(0);
20
Explanation : Call by Value
While Passing Parameters using call by value , xerox copy of original parameter is created and passed to the called function. Any update made inside method will not affect the original value of variable in calling function. In the above example num1 and num2 are the original values and xerox copy of these values is passed to the function and these values are copied into number1,number2 variable of sum function respectively. As their scope is limited to only function so they cannot alter the values inside main function.
21
Call by Reference/Pointer/Address :
#include<stdio.h> void interchange(int *num1,int *num2) { int temp; temp = *num1; *num1 = *num2; *num2 = temp; } int main() { int num1=50,num2=70; interchange(&num1,&num2); printf("\nNumber 1 : %d",num1); printf("\nNumber 2 : %d",num2); return(0);
22
Explanation : Call by Address
While passing parameter using call by address scheme , we are passing the actual address of the variable to the called function. Any updates made inside the called function will modify the original copy since we are directly modifying the content of the exact memory location.
23
Summary of Call By Value and Call By Reference :
Point Call by Value Call by Reference Copy Duplicate Copy of Original Parameter is Passed Actual Copy of Original Parameter is Passed Modification No effect on Original Parameter after modifying parameter in function Original Parameter gets affected if value of parameter changed inside function
24
Call by Value example 2 #include <stdio.h>
void call_by_value(int x) { printf("Inside call_by_value x = %d before adding 10.\n", x); x += 10; printf("Inside call_by_value x = %d after adding 10.\n", x); } int main() { int a=10; printf("a = %d before function call_by_value.\n", a); call_by_value(a); printf("a = %d after function call_by_value.\n", a); return 0;
25
Output a = 10 before function call_by_value.
Inside call_by_value x = 10 before adding 10. Inside call_by_value x = 20 after adding 10. a = 10 after function call_by_value.
26
Call by refernce #include <stdio.h>
void call_by_reference(int *y) { printf("Inside call_by_reference y = %d before adding 10.\n", *y); (*y) += 10; printf("Inside call_by_reference y = %d after adding 10.\n", *y); } int main() { int b=10; printf("b = %d before function call_by_reference.\n", b); call_by_reference(&b); printf("b = %d after function call_by_reference.\n", b); return 0’;
27
Output b = 10 before function call_by_reference.
Inside call_by_reference y = 10 before adding 10. Inside call_by_reference y = 20 after adding 10. b = 20 after function calls_by_reference.
29
Pointer to a function declare Pointer to function
<function return type>(*<Pointer_name>)(function argument list) For example double (*p2f)(double, char) Here double is a return type of function, p2f is pointer name & (double, char) is an argument list for the function. Which means the first argument for this function should be double and the second one would be of char type.
30
#include<stdio.h>
int sum (int num1, int num2) { return sum1+sum2; } int main() int (*f2p) (int, int); f2p = sum; int op1 = f2p (10, 13); int op2 = sum (10, 13); printf("Output 1 – for function call via Pointer: %d",op1); printf("Output2 – for direct function call: %d", op2); return 0;
31
output Output 1 – for function call via Pointer: 23
Output2 – for direct function call: 23
32
C - Recursion Recursion is the process of repeating items in a self-similar way. if a program allows to call a function inside the same function, then it is called a recursive call of the function. void recursion() { recursion(); /* function calls itself */ } int main() { recursion();
34
Number Factorial #include <stdio.h> int factorial( int i) { if(i <= 1) { return 1; } return ( i * factorial(i - 1)); int main() { int i = 15; printf("Factorial of %d is %d\n", i, factorial(i)); return 0;
35
Fibonacci Series #include <stdio.h> int fibonacci(int i) { if(i == 0) { return 0; } if(i == 1) { return 1; return fibonacci(i-1) + fibonacci(i-2); int main() { int i; for (i = 0; i < 5; i++) { printf("%d\t\n", fibonacci(i));
36
Sum of Natural Numbers Using Recursion
#include <stdio.h> int sum(int n); int main) { int number, result; printf("Enter a positive integer: "); scanf("%d", &number); result = sum(number); printf("sum=%d", result); } int sum(int num) if (num!=0) return num + sum(num-1); // sum() function calls itself else return num;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.