Fundamentals of functions in C program.pptx

ChandrakantDivate1 56 views 23 slides May 10, 2024
Slide 1
Slide 1 of 23
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23

About This Presentation

Fundamentals of functions in C program.pptx


Slide Content

C. P. Divate

A large program in c can be divided to many subprogram The subp rogr a m p o sses a se l f con t a i n com p one n ts a n d have w e l l d e f i n e p u rpo s e . The subp rogr a m i s ca l l ed a s a fu n ction Basically a job of function is to do something C program contain at least one function which is main(). Classification of Function Library fu n c t i on User define function - main() -printf() -scanf() -pow() -ceil()

It is much easier to write a structured program where a large program can be divided into a smaller, simpler task. Allowing the code to be called many times Easier to read and update It is easier to debug a structured program where there error is easy to find and fix

1: #include <stdio.h> 2: 3 : l o n g c u be ( l o n g x ); 4: 5 : l on g i npu t , a n swe r ; 6: 7: int main( void ) 8: { 9: printf(“Enter an integer value: ”); 10: scanf(“%d”, &input); 11 : a n swer = c u be( i np u t ); 12: printf(“\nThe cube of %ld is %ld.\n”, input, answer); 13: 14: return 0; 15: } 16: 1 7 : l o n g c u be ( l o n g x ) 18: { x _ c u be d = x * x * x; return x_cubed; 19 : l o n g x _ c u be d ; 20: 21: 22: 23: } Fu nc ti o n na mes i s c u be Variable that are requires is long Th e var i a b l e t o b e pas sed on is X(has single arguments)— va l u e c a n b e pas sed t o f u nc ti o n so i t c a n p e r fo r m t he spec i f i c t a s k. I t i s c a l l ed Output En t er a n i n t e ge r va l ue:4 Th e cube o f 4 i s 64. Return data type A r g u me n ts/ f o r m al p a r ameter Act u al p arameters

C program doesn't execute the statement in function until the function is called. When function is called the program can send the function information in the form of one or more argument. When the function is used it is referred to as the called function Functions often use data that is passed to them from the calling function Data is passed from the calling function to a called function by specifying the variables in a argument list. Argument list cannot be used to send data. Its only copy data/value/variable that pass from the calling function. The called function then performs its operation using the copies.

Provides the compiler with the description of functions that will be used later in the program Its define the function before it been used/called Function prototypes need to be written at the beginning of the program. Th e fun c t i o n p roto t y pe mus t hav e : A return type indicating the variable that the function will be return S ynt a x fo r F u nctio n Prototype return-type function_name( arg-type name-1,...,arg-type name-n); F u nctio n Prototyp e Exampl es double squared( double number ); void print_report( int report_number ); int get_menu_choice( void);

It is the actual function that contains the code that will be execute. Should be identical to the function prototype. Syntax of Function Definition return-type function_name( arg-type name-1,...,arg-type name-n) ---- Function header { declarations; statements; re t ur n (express i on); } F u nctio n Bo d y

F u nctio n Defi n it i on Exampl es f l oat conv e rs i o n (f l oat c e l s i u s ) { float fahrenheit; fahr e n he i t = c e l c i u s *33.8 return fahrenheit; } Th e fun c t i o n name‟s i s conv e rs i on This function accepts arguments celcius of the type float . The function return a float value. So, when this function is called in the program, it will perform its task which is to convert fahrenheit by multiply celcius with 33.8 and return the result of the summation. Note that if the function is returning a value, it needs to use the keyword return .

Can b e a ny o f C‟s d a ta t y pe: char int f l oat long……… Examples: / * Re t urns a type i n t. */ int func1(...) float func2(...) void func3(...) / * Re t urns a type f l oat . */ /* Returns nothing. */

Function can be divided into 4 categories: A function with no arguments and no return value A function with no arguments and a return value A function with an argument or arguments and returning no value A function with arguments and returning a values

A function with no arguments and no return value Called function does not have any arguments Not able to get any value from the calling function Not returning any value There is no data transfer between the calling function and called function. #include<stdio.h> # i n c l ude< c o n i o . h > void printline(); void main() { printf("Welcome to function in C"); printline(); p r i n t f (" Fu n c t i o n e a sy to l e a r n . " ); printline(); getch(); } void printline() { int i; printf("\n"); f o r ( i = ; i < 30 ; i ++ ) { p r i n t f (" - " ) ; } printf("\n"); }

A function with no arguments and a return value Does not get any value from the calling function Can g i ve a return va l ue to ca l l i n g pro gram #include <stdio.h> #include <conio.h> int send(); void main() { int z; z=sen d( ) ; pr i n t f ( " \ n You ente r ed : % d . ", z ); getch(); } int send() { int no1; pr i n t f (" E n t e r a n o : " ); scanf("%d",&no1); return(no1); } Enter a no: 46 You entered : 46.

A function with an argument or arguments and returning no value A fun c t i o n ha s argume n t/s A calling function can pass values to function called , but calling function not receive any value Data is transferred from calling function to the called function but no data is transferred from the called function to the calling function Generally Output is printed in the Called function A function that does not return any value cannot be used in an expression it can be used on l y a s i n d e pen d e n t sta t eme n t.

#include<stdio.h> #include<conio.h> vo i d ad d ( i n t x , i n t y ); void main() { add(30,15); add(63,49); add(952,321); getch(); } vo i d ad d ( i n t x , i n t y) { int result; resu l t = x+y ; pr i n t f ("Su m o f %d a n d %d i s % d . \ n \ n" , x ,y , resu l t ); }

A function with arguments and returning a values Argument are passed by calling function to the called function Called function return value to the calling function Mostly used in programming because it can two way communication Data returned by the function can be used later in our program for further calculation.

R e sult 85. R e sult 12 7 3 . #include <stdio.h> #include <conio.h> int add(int x,int y); void main() { int z; z=add(952,321); printf("Result %d. \n\n",add(30,55)); printf("Result %d.\n\n",z); getch(); } int add(int x,int y) { int result; resu l t = x + y ; re t urn(resu l t); } S e nd 2 i nt e ge r val u e x an d y to add() F u nct i o n ad d the two v a lu e s an d s e nd b a ck the r e su l t to the cal l i ng f u nct i on i nt i s the r e tu rn type o f f u nct i on R e tu rn state m e nt i s a k eywor d an d i n br a ck e t we can gi ve val u e s w h i ch we want to return.

Variable that declared occupies a memory according to it size It has address for the location so it can be referred later by CPU for manipulation Th e „ * ‟ an d „& ‟ O p era t or I n t x = 10 x 10 76858 Memory location name Va l ue a t memo ry l ocat i on Memory location address We can use the address which also point the same value.

#include <stdio.h> #include <conio.h> void main() { int i=9; p r i n t f ("Va l ue o f i : % d \ n" , i ); printf("Adress of i %d\n", &i); getch(); } & show t h e a d d ress o f t h e var i ab l e

#include <stdio.h> # i n c l ude < con i o.h > void main() { int i=9; pr i n t f ("Va l ue o f i : % d \ n" , i ); printf("Address of i %d\n", &i); printf("Value at address of i: %d", *(&i)); getch(); } * S y mbo l s c a ll ed the va l ue a t the a d dr e ss

#include <stdio.h> #include <conio.h> int Value (int x); i n t Refe r ence ( i n t *x); int main() { i n t Ba t u _P a h a t = 2; i n t L a n gka wi = 2; Value(Batu_Pahat); Reference(&Langkawi); printf("Batu_Pahat is number %d\n",Batu_Pahat); pr i n t f ( " L a n gka w i i s n u m be r %d" , L a n gka w i ); } int Value(int x) { x = 1; } int Reference(int *x) { *x = 1; } Pas s b y ref e re n ce You pass the var i ab l e a d dr e ss Give the function direct access to the variable Th e var i ab l e can b e m o d i f i ed i n s i d e the fun c t i on

#include <stdio.h> # i n c l ude < con i o.h > void callByValue(int, int); vo i d ca l l ByR e fer e n c e ( i n t *, i n t *); int main() { i n t x = 10 , y =20; printf("Value of x = %d and y = %d. \n",x,y); printf("\nCAll By Value function call...\n"); callByValue(x,y); printf("\nValue of x = %d and y = %d.\n", x,y); printf("\nCAll By Reference function call...\n"); callByReference(&x,&y); printf("Value of x = %d and y = %d.\n", x,y); getch(); re t urn ; }

vo i d ca l l ByVa l u e ( i n t x , i n t y) { int temp; temp=x; x=y; y=temp; printf("\nValue of x = %d and y = %d inside callByValue function",x,y); } vo i d ca l l ByR e fer e n c e ( i n t * x, i n t *y) { int temp; temp=*x; *x=*y; *y=temp; }
Tags