Download presentation
Presentation is loading. Please wait.
1
Functions The fruitful & parameterized functions
2
Passing argument to function Argument/parameter is a piece of data passed to function. Function uses argument then the variables must be declared in the header of function definition of receive the values.
3
Types of argument/parameters Two types of argument/parameters – Formal argument/parameters – actual argument/parameters
4
Formal argument/parameters The parameters used in the header of function definition are called formal parameters of the function. These parameters are used to receive values from the calling function.
5
Actual argument/parameters The parameters used in the function call are called actual argument/parameters. These are used the actual values that are passed to the function. The actual parameters may be in the form of constant values or variable
6
Difference between formal & actual parameters. Formal parameters Actual parameters 1.They are used in the function header.1.They are used in the function call. 2. They are used to receive the values that are passed to the function through function call. 2. They are the actual values that are passed to the function definition through the function call 3. They are treated as local variables of the function in which they are used in the function header. 3. They may be constant values or variables names (local or global)
7
Example #include void Pak ( int ); // prototype Void main() { clrscr(); Pak (3);// function call getch(); } Pak(int n)// function definition { for(i=1;i<=n;i++) printf(“Pakistan\n”); }
8
Passing argument by value Void main() { int m=5, n=10; temp (m,n); -------- } temp(int x, int y ) { x=x+10; y=y+10; } Variables in RAM m n x y 5 10 15 20
9
Example #include void draw_asterisks ( void); // prototype Void main() { clrscr(); draw_asterisks ( );// function call getch(); }
10
Cont.. void draw_asterisks ( void)// function definition { int u,i; for(u=9;u>=1; u++ ) { for(i=1;i<=u; i++ ) printf(“*”); printf(“\n”); }
11
Return value from a function When a function completes its execution, it can return a single value to the calling function (or program). It may be formula or any value. For example: int sum(int, int );//prototype int sum (int x, int y) { return x+y; }
12
Return statement The return statement is used in the body of the function to return ta value as well as execution control to the calling function. It is used at the last of the function. Syntax: Return experssion;
13
Using more than one function #include flaot add ( float, float); flaot sub ( float, float); flaot Mul ( float, float); flaot Div ( float, float); void main () { float x, y; int op; clrscr();
14
printf(“Enter the first number :”); scanf(“%d”,&x); printf(“Enter the second number :”); scanf(“%d”,&y); printf(“ 1 – Add numbers\n”); printf(“ 2 – Subtract numbers\n”); printf(“ 3 – Multiply numbers\n”); printf(“ 4 – Divide numbers\n”); printf(“ Select your choice :”); scanf(“%d”,&op);
15
switch( op ) { case 1: printf(“%d+%d=%d”, add(x,y) ); break; case 2: printf(“%d-%d=%d”, sub(x,y) ); break; case 3: printf(“%d*%d=%d”, Mul(x,y) ); break; case 4: printf(“%d / %d=%d”, Div (x,y) ); break; default: printf(“ invalid option”); } getch(); }
16
float add (float a, float b) { return a+b; } float asub (float a, float b) { return a-b; } float Mul (float a, float b) { return a*b; } float Div(float a, float b) { return a/b; }
17
Program task1…… Write a program that swap (exchanges) two values by passing values as arguments to the function.
18
Program task2…. Write a program that inputs two number and passes these number to a function. The function finds the greatest number and displays the result.
19
Program task3…. Write a program that inputs two numbers and passes these numbers to a function. The function displays the result of first number raised to the power of second number.
20
#include void power (int, int ); void main () { int a, b; printf(“Enter first number :”); scanf(“%d”,,&a); printf(“Enter second number :”); scanf(“%d”,,&b); Power (a, b); getch(); }
21
void power(int n, int p) { int res, c; res=c=1; while(c<=p) { res=res*n; c=c+1; } printf(“%draised to the power %d is %d”,n,p,res); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.