Location via proxy:
[ UP ]
[Report a bug]
[Manage cookies]
No cookies
No scripts
No ads
No referrer
Show this form
Open navigation menu
Close suggestions
Search
Search
en
Change Language
Upload
Sign in
Sign in
Download free for days
0 ratings
0% found this document useful (0 votes)
46 views
C Programming Functions
Functions in c programming
Uploaded by
Rimuru
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save C programming functions For Later
Download
Save
Save C programming functions For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
46 views
C Programming Functions
Functions in c programming
Uploaded by
Rimuru
AI-enhanced title
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save C programming functions For Later
Carousel Previous
Carousel Next
Save
Save C programming functions For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 18
Search
Fullscreen
x C Programming Functions ‘A function is a group of statements that togeth program has at least one function, which is mal programs can define additional functions. er perform a task. Every C in(), and all the most trivial ‘A function is a block of code that performs a specific task ‘Suppose, a program related to graphics needs to create a circle and color it depending upon the radius and color from the user. You can create two functions to solve this problem: = create a circle function + color function Defining a Function The general form of a function definition in C programming language is as follows — return_type function nane( paraneter Tist ) { body of the function A function definition in C programming consists of a function header and ‘a function body. Here are all the parts of a function — = 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 out returning a value. In this case, the return_type is the operations keyword void. «Function Name - This is the actual name of the function. The function name and the parameter list together constitute the function signature. es — o the parameter. This value Is referred to as actual ent, The parameter list refers to the type, order, and ters of a function. Parameters are optional; that Is, 2 you pass a value t parameter or argum number of the paramet function may contain no parameters. «Function Body ~ The function body contains a collection of statements that define what the function does. am) gunct } ae 2 Example for a function called max(). This function \ a eee et \d num2 and returns the maximum value takes two parameters num1 ani 7 between the two — : /* function returning the max between two nunbers */ | : \ int max(int num, int num2) { /* local variable declaration */ int result; if (num. > num2) result = num; else result = num; return result; t 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. A function declaration has the following parts — return_type function nane( paraseter 14st ; For the above defined function max(), the function declaration is as follows — Ant max int num, int nun); Parameter names are not important in function declaration only their ype is required, so the following is also a valid declaration — fnt maxCint, int);n is required when you define a function in one source Function declaratiot uld file and you call that function in another file. In such case, you sho declare the function at the top of the file calling the function. Calling a Function While creating a C function, you give a definition of what the function has to do. To use a function, you will have 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 and when its return statement is executed or when its function-ending closing brace is reached, it returns the progrem control back to the main program. To call a function, you simply need to pass the required parameters along with the function name, and if the function returns a value, then you can store the returned value. For example — eee Hinclude
/* function declaration */ int max(int numi, int num2); int main () { /* local variable definition */ int a = 1005 int b = 2003 int ret; /* calling a function to get max value */ ret = max(2, B)sreturning the max between to nunbers */ rum, int num2) { variable declaration */ sult; > numa) uma ; We have kept max() along with main() and complied the source code, Wnile running the final executable, it would produce the following result max value is : 260 Function Arguments If 2 function is to use arguments, it must declare variables the values of the arguments. These variables are called parametersof the function. that accept the format Formal parameters behave like other local variables inside th i le Function and are created upon entry into the function and destroyed up, On exit, While calling a function, there are tw passed to a function —Sr.No. Call Type & Description 1 Call by value TT his 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 argument. : Call by reference Thi a method copies the address of an argument into the formal E ‘ameter. Inside the function, the address is used to access the actual Tgument used in the call, This means that changes made to the Parameter affect the argument. By defa aa ate a call by value to pass arguments. In general, it means the unction cannot alter the arguments used to call the function. Types of function Depending on whether a function is defined by vy the user or alread) ‘luded compilers, there are two types of functions in C programming ° There are two types of function in C programming: * Standard library functions + User defined functions Standard library functions The standard library functions are built-in functions in C programming to handle tasks such as mathematical computations, /O processing, string handling etc. These functions are defined in the header file. When you include the header file, these functions are available for use. For example: is the screen is a standard library function to send formatted output to bassiey oie ). This function is defined in "stdio.h* header file (display output on the screen) jer “stdio.h, such inctions defined und s library func “stdio.h” in your program, There are other numerou: x as scanf(), fprintf(), getchar() etc. Once you include all these functions are available for use.in C programming more about standard library funcl Learn User-defined function ‘As mentioned earlier, C allow programmers tO define functions. Such functions created by the user are called user-defined functions. You can create as many user-defined functions as you want How user-defined function works? #include
void functionName() { d int main() { functionName() ;When the compiler encounters FunctionNane(); inside the main function, control of the program jumps to void functionName() And, the compiler starts ©xecuting the codes inside the user-defined function. The control of the program Jams to statement next to functionNane(); once all the Codes inside the function definition are executed. Advantages of user-defined function J. The program will be easier to understand, maintain and debug 2. Reusable codes that can be used in other programs 3. A large program can be divided into smaller modules. Hence, a large project can be divided among many programmers, 4. #include
5. 6. int addNumbers(int a, int b); // function prototype Z 8. int main() of 10. int n1,n2, sum; 11. 12. printf("Enters two numbers: 2B. scanf("%d %d",&n1,&n2) ; 14. ; a 15 sum = addNumbers(n1, n2); /1 function ca: 16. 17. printf("sum = %d", sum);ato _ : 1. i as, return 0; 7 7 = tb) // function definition ve i nt 22. int addNumbers(int a, i 23 | 24. int result; | 25. result = a*bj 26. return result; // return statement 2.) Function prototype ‘A function prototype is simply the declaration of a function that specifies function's name, parameters and return type. It doesn't contain function body. ‘A function prototype gives information to the compiler that the function may later be Used in the program, Syntax of function prototype returnType functionName(type1 argumenti, type2 argument2,. In the above example, int addvunbers(int a, int b); is the function which provides following information to the compiler. ae 1. name of the function is adaWunbers() 2. return type of the function is int 3. two arguments of type int are passed to the function The function prototype is not needed if the user-defined function is defined before the main() function Calling a function Control of the program is transferred to the user-defined function by calling it.Syntax of function call functionName(argumenti, argument2, . In the above example, function call is made usin Q ddNunberss{n1,n2); inside the main(), 19 addtiunbers(nt,n2); statement Function definition tic tains the block of code to ere if case, adding two numbers and returning it. oD ‘Syntax of function definition Feturntype functionName(typer argument, type2 argument2, .. { //body of the function Passing arguments to a function In programming, argument refers to the variable passed to the function. In the above example, two variables n1 and n2 are passed during function call The parameters a and b accepts the passed arguments in the function definition, These arguments are called formal parameters of the function The type of arguments passed to a function and the formal parameters must match, otherwise the compiler throws error. If nt is of char type, a also should be of char type. If n2 is of float type, variable b also should be of float type. ‘function can also be called without passing an argument.Return Statement terminates the ex« ym control and returns a value to ction ecution of ay the calling function after The return statement a red to th the calling function. The progral is transfe return statement. variable sum in In the above example, the value of variable result |S returned to the the main() function. Passing Arguments to a function Arguments are the values specified during the function call, for which the formal parameters are declared while defining the function. Functions | With Arguments Without Arguments | | _ declared and defined with [No parameters included. parameter list | values for parameter — No value passed during passed during call aoe L_te: | fe: 1/ declaration // declaration int display(); int sum (int , int y); I call Meall display(); sum(10, 20}; Type of User-defined Functions in C There can be 4 different types of user-defined functions, they are: 1. Function with no arguments and no return value 2. Function with no arguments and a return value 3. Function with arguments and no return value 4 Function with arguments and a return value Below, we will discuss about all these types, along with program examples.Function with no arguments and no return value Such functions can either be used to display information or they are completely dependent on user inputs. Below is an example of a function, which takes 2 Numbers as input from user, and display which is the greater number Corey void greatnum(y ig PU ae ease at iret anya SAGE CLA Sar Spy aCe Et ean eae ttt ean, Function with no arguments and a return value We have modified the above example to make the function greatnum() return the number which is greater amongst the 2 input numbers.rents) Poe Or Pet ee tet oe Ce FaaCTLTe) ( eR teen eT Se oT a ee eee ee) 1 eee Ce fi greaterNum = j; Ta et ee Function with arguments and no return value We are using the same function as example again and again, to demonstrate that to solve a problem there can be many different ways. This time, we have modified the above example to make the function greatum() take two intvalues as arguments, but it will not be returning anything seca Cociie i nie uae rus ramen} ti TreeTae tMum(int x, ant y) Comoe ue eat ee De ect eee Function with arguments and a return value This is the best type, as this make! e kes the function completely independent of inputs and outputs, and only the logic is defined inside the function int greatNum(int a, int b); EUCanESur6) cr Lar emer os Petree cee Sec nc oF Peer eLcit ease Peete Cee Dey L Pr ena cm ene ton Roa Pererigece tie Uae me UL) { Cee\ Nesting of Functions C language also allows nesting of functions i.e to use/call one function inside another ted functions, because it may lead to function's body. We must be careful while using nes! infinite nesting. Pastas} it Castor Tfunction2() also has a call for function) inside tt, then in that case, it will lead to an infinite nesting. They will Keep calling each other and the program will never terminate. Not able to understand? Lets consider that inside the main() function, function1() is called and its execution starts, then inside functior1(), we have a call for function2(), so fhe control of program will goto the function2(). But as function2() also has a call to function! () in ts body, it will call function1(), which wil again call function2), and this will go on for infinite times, until you forcefully ext from program execution What is Recursion? Recursion is a special way of nestin must have certain conditions in the function to break out recursion will occur infinite times. 19 functions, where a function calls itself inside it. We of the recursion, otherwiseExample: Factorial of a number using Recursion (seers Nhs RECS SCRE) DUS mnt aE) torial(a); Ceca yy b= Pune iren eta) is Tra Types of Function calls in C Functions are called by their names, we all know that, then what is this tutorial for? Well if the function does not have any arguments, then to call a function you can directly use its name. But for functions with arguments, we can call a function in two different ways, based on how we specify the arguments, and these two ways are 1. Call by Value 2. Call by ReferenceCall by Value on Calling a function by value means, we pass the eee f Hl Stored or copied into the formal parameters of the func! 7 7 are unchanged only the parameters inside the function chang! arguments which are lence, the original values SCS e(int Pam le) t cars cote } Picea acacia co pect Pretec ene st Mae se Qi Tora ct ner) value of x in calc function is 2 value of x in main is 1¢ In this case, the actual variable x is not changed. This is because we are passing the argument by value, hence a copy of x is passed to the function, which is updated during function execution, and that copied value in the function is destroyed when the function ends(goes out of scope). So the variable x inside the wain() function is never changed and hence, still holds a value of 10. But we can change this program to let the function modify the original x variable, by making the function calc() return a value, and storing that value in x. CoS err aS Furares eu raeo Faeyr}value of x is 20 Call by Reference ass the address(reference) he address of any variable as argu! ow knows where it is store of a variable as argument to any iment, then the function will .d and hence can easily In call by reference we P: function, When we pass have access to our variable, as it n update its value his case the formal parameter can be take fe will soon learn about them), Int 1 as a reference or a pointer(don't worry about pointers, w in both the cases they will change the values of the original variable. cans Ureerc CoS Reartetaculaat on Fura rule) { aaa Pre ee erie taco STs kUanda Pen woe Peo by void calc(int *P) i} value of x ts 28 y The najore difference between Actual & onal on peenents | Parameters in that Aactuah arguments are he Se infermasion \ calling programs paw Acktwod ae +0 Called cteona Were Vales are parsed toa called ction the Volues preeaent cn actuol angen ute copied 40 Forcmat orgoments > When a ‘function CX carttoal, tre Voluak Mat ace barsedh intro CAML Aire calles thro actual orgemens At the Lime of Cate ent, actuod leaps €4& arrignesl to Correrpondi fren, povrameterc “ Sesion ignition 1 4 \" ZO am Rg CAA At = 10, %5 220, Cteny Actuch « Sum = add nos Ci ,n2)) / fen corer ; « : eee, Prat fC Sumo} 7.4 and Yd ia! za\n) Mas ' int add nos Cit a , tat D) - mater ars the wrocedva Valet kp org, Romance ¢ Pp pen cr RE iif
You might also like
Hourglass Workout Program by Luisagiuliet 2
PDF
76% (21)
Hourglass Workout Program by Luisagiuliet 2
51 pages
12 Week Program: Summer Body Starts Now
PDF
87% (46)
12 Week Program: Summer Body Starts Now
70 pages
Read People Like A Book by Patrick King-Edited
PDF
58% (81)
Read People Like A Book by Patrick King-Edited
12 pages
Livingood, Blake - Livingood Daily Your 21-Day Guide To Experience Real Health
PDF
77% (13)
Livingood, Blake - Livingood Daily Your 21-Day Guide To Experience Real Health
260 pages
Cheat Code To The Universe
PDF
94% (79)
Cheat Code To The Universe
34 pages
Facial Gains Guide (001 081)
PDF
91% (45)
Facial Gains Guide (001 081)
81 pages
Curse of Strahd
PDF
95% (467)
Curse of Strahd
258 pages
The Psychiatric Interview - Daniel Carlat
PDF
91% (34)
The Psychiatric Interview - Daniel Carlat
473 pages
The Borax Conspiracy
PDF
91% (57)
The Borax Conspiracy
14 pages
COSMIC CONSCIOUSNESS OF HUMANITY - PROBLEMS OF NEW COSMOGONY (V.P.Kaznacheev,. Л. V. Trofimov.)
PDF
94% (214)
COSMIC CONSCIOUSNESS OF HUMANITY - PROBLEMS OF NEW COSMOGONY (V.P.Kaznacheev,. Л. V. Trofimov.)
212 pages
TDA Birth Certificate Bond Instructions
PDF
97% (284)
TDA Birth Certificate Bond Instructions
4 pages
The Secret Language of Attraction
PDF
86% (107)
The Secret Language of Attraction
278 pages
How To Develop and Write A Grant Proposal
PDF
83% (542)
How To Develop and Write A Grant Proposal
17 pages
Penis Enlargement Secret
PDF
60% (124)
Penis Enlargement Secret
12 pages
Workbook For The Body Keeps The Score
PDF
89% (53)
Workbook For The Body Keeps The Score
111 pages
Donald Trump & Jeffrey Epstein Rape Lawsuit and Affidavits
PDF
83% (1016)
Donald Trump & Jeffrey Epstein Rape Lawsuit and Affidavits
13 pages
KamaSutra Positions
PDF
78% (69)
KamaSutra Positions
55 pages
7 Hermetic Principles
PDF
93% (30)
7 Hermetic Principles
3 pages
27 Feedback Mechanisms Pogil Key
PDF
77% (13)
27 Feedback Mechanisms Pogil Key
6 pages
Frank Hammond - List of Demons
PDF
92% (92)
Frank Hammond - List of Demons
3 pages
Phone Codes
PDF
79% (28)
Phone Codes
5 pages
36 Questions That Lead To Love
PDF
91% (35)
36 Questions That Lead To Love
3 pages
How 2 Setup Trust
PDF
97% (307)
How 2 Setup Trust
3 pages
The 36 Questions That Lead To Love - The New York Times
PDF
94% (34)
The 36 Questions That Lead To Love - The New York Times
3 pages
100 Questions To Ask Your Partner
PDF
78% (36)
100 Questions To Ask Your Partner
2 pages
Satanic Calendar
PDF
25% (56)
Satanic Calendar
4 pages
The 36 Questions That Lead To Love - The New York Times
PDF
95% (21)
The 36 Questions That Lead To Love - The New York Times
3 pages
Jeffrey Epstein39s Little Black Book Unredacted PDF
PDF
75% (12)
Jeffrey Epstein39s Little Black Book Unredacted PDF
95 pages
14 Easiest & Hardest Muscles To Build (Ranked With Solutions)
PDF
100% (8)
14 Easiest & Hardest Muscles To Build (Ranked With Solutions)
27 pages
1001 Songs
PDF
69% (72)
1001 Songs
1,798 pages
The 4 Hour Workweek, Expanded and Updated by Timothy Ferriss - Excerpt
PDF
23% (954)
The 4 Hour Workweek, Expanded and Updated by Timothy Ferriss - Excerpt
38 pages
Zodiac Sign & Their Most Common Addictions
PDF
63% (30)
Zodiac Sign & Their Most Common Addictions
9 pages
Function
PDF
No ratings yet
Function
63 pages
UNIT 5-Functions
PDF
No ratings yet
UNIT 5-Functions
25 pages
Unit 2 Functions
PDF
No ratings yet
Unit 2 Functions
28 pages
Unit 4
PDF
No ratings yet
Unit 4
37 pages
Unit 4 Functions
PDF
No ratings yet
Unit 4 Functions
48 pages
Chapter 10 Functions
PDF
No ratings yet
Chapter 10 Functions
40 pages
Subprograms in C
PDF
No ratings yet
Subprograms in C
6 pages
Unit 3 P4 Functions
PDF
No ratings yet
Unit 3 P4 Functions
32 pages
Unit 6C
PDF
No ratings yet
Unit 6C
9 pages
Comprog11 Semifinals Compilation (1)
PDF
No ratings yet
Comprog11 Semifinals Compilation (1)
44 pages
UNIT III
PDF
No ratings yet
UNIT III
33 pages
Unit 5
PDF
No ratings yet
Unit 5
37 pages
Functions
PDF
No ratings yet
Functions
51 pages
3 Functions
PDF
No ratings yet
3 Functions
134 pages
UNIT 5
PDF
No ratings yet
UNIT 5
17 pages
Chapter 6
PDF
No ratings yet
Chapter 6
31 pages
Functions (1)
PDF
No ratings yet
Functions (1)
45 pages
Lab 9 Function Updated
PDF
No ratings yet
Lab 9 Function Updated
9 pages
Functions
PDF
No ratings yet
Functions
48 pages
Function in C
PDF
No ratings yet
Function in C
46 pages
C Functions Explanation
PDF
100% (2)
C Functions Explanation
11 pages
Pps Unit III
PDF
No ratings yet
Pps Unit III
66 pages
Functions in C
PDF
No ratings yet
Functions in C
18 pages
Vision 2024 Data Sturcture Programming Chapter 2 Recursion 791661499780242
PDF
No ratings yet
Vision 2024 Data Sturcture Programming Chapter 2 Recursion 791661499780242
23 pages
6 Functions in C
PDF
No ratings yet
6 Functions in C
44 pages
C Functions
PDF
No ratings yet
C Functions
43 pages
Lecture 5 - Function Part I
PDF
No ratings yet
Lecture 5 - Function Part I
44 pages
Notes 3
PDF
No ratings yet
Notes 3
13 pages
Copy of UNIT - V - Functions
PDF
No ratings yet
Copy of UNIT - V - Functions
39 pages
Est102 Module 4
PDF
No ratings yet
Est102 Module 4
29 pages
C Functions
PDF
No ratings yet
C Functions
39 pages
Ch 9 UDF
PDF
No ratings yet
Ch 9 UDF
53 pages
Function Of C
PDF
No ratings yet
Function Of C
28 pages
function in c
PDF
No ratings yet
function in c
22 pages
Functions
PDF
No ratings yet
Functions
16 pages
FOC Unit 4 _ 5 slides
PDF
No ratings yet
FOC Unit 4 _ 5 slides
42 pages
HW1248
PDF
No ratings yet
HW1248
17 pages
PSP PPT 2
PDF
No ratings yet
PSP PPT 2
30 pages
c Functions
PDF
No ratings yet
c Functions
5 pages
BPC1UNIT5
PDF
No ratings yet
BPC1UNIT5
22 pages
PPS Ch-9.Functions
PDF
No ratings yet
PPS Ch-9.Functions
40 pages
Module-2 Functions
PDF
No ratings yet
Module-2 Functions
42 pages
Copy of Copy of UNIT - V ---Functions
PDF
No ratings yet
Copy of Copy of UNIT - V ---Functions
39 pages
Lec 7 Functions
PDF
No ratings yet
Lec 7 Functions
19 pages
Functions IP Notes
PDF
No ratings yet
Functions IP Notes
13 pages
Lecture 8
PDF
No ratings yet
Lecture 8
33 pages
Unit 5
PDF
No ratings yet
Unit 5
35 pages
Lecture Slide of Function in C Program
PDF
No ratings yet
Lecture Slide of Function in C Program
71 pages
UNIT-3: Functions and Structures
PDF
No ratings yet
UNIT-3: Functions and Structures
51 pages
UNIT4
PDF
No ratings yet
UNIT4
24 pages
unit-2 (part B) C- Pro (1)
PDF
No ratings yet
unit-2 (part B) C- Pro (1)
17 pages
Unit 7 Functions
PDF
No ratings yet
Unit 7 Functions
114 pages
Unit 4
PDF
No ratings yet
Unit 4
62 pages
Adobe Scan 27 Feb 2025
PDF
No ratings yet
Adobe Scan 27 Feb 2025
5 pages
CS Unit 4 Notes
PDF
No ratings yet
CS Unit 4 Notes
39 pages
unit-4(Function)
PDF
No ratings yet
unit-4(Function)
10 pages
Functions in C
PDF
No ratings yet
Functions in C
17 pages
Mod 3
PDF
No ratings yet
Mod 3
72 pages