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)
8 views
User Defined Function
programming
Uploaded by
Mary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save User Defined Function For Later
Download
Save
Save User Defined Function For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
0 ratings
0% found this document useful (0 votes)
8 views
User Defined Function
programming
Uploaded by
Mary
Copyright
© © All Rights Reserved
Available Formats
Download as PDF or read online on Scribd
Download now
Download
Save User Defined Function For Later
Carousel Previous
Carousel Next
Save
Save User Defined Function For Later
0%
0% found this document useful, undefined
0%
, undefined
Embed
Share
Print
Report
Download now
Download
You are on page 1
/ 8
Search
Fullscreen
5/14/24, 8:09AM User-Defined Function in C- GesksforGeeks User-Defined Function in C Last Updated : 01 Jun, 2023 A user-defined function is a type of function in C language that is defined by the user himself to perform some specific task. It provides code reusability and modularity to our program. User- defined functions are different from built-in functions as their working is specified by the user and no header file is required for their usage In this article, we will learn about user-defined function, function prototype, function definition, function call, and different ways in which we can pass parameters to a function. How to use User-Defined Functions in C? To use a user-defined function, we first have to understand the different parts of its syntax. The user-defined function in C can be divided into three parts: 1. Function Prototype 2, Function Definition 3. Function Call C Function Prototype A function prototype is also known as a function declaration which specifies the funetion’s name, function parameters, and return type. The function prototype does not contain the body of the function. It is basically used to inform the compiler about the existence of the user- defined function which can be used in the later part of the program. Intps:iwww geekstorgeeks orgluser-defined-{unction-inclref=ibp 185/14/24, 8:09AM User-Defined Function in G - GeekstorGecks Syntax return_type function_name (typel arg1, type2 arg2, ... typeN arg); We can also skip the name of the arguments in the function prototype. So, return_type function.name (typel , type2 , +.» typeN); Function Prototype { int heading ( void )<— C Function Definition Once the function has been called, the function definition contains the actual statements that will be executed. All the statements of the function definition are enclosed within { } braces. Syntax return_type function_name (type arg1, type2 arg2 .... typeN argN) { // actual statements to be executed // return value if any nitpsww.gecksforgeeks.orpluser-deined-unctonn-c/xef-op 2185/14/24, 8:09AM User-Defined Function in C- GesksforGeeks Note: If the function call is present after the function definition, we can skip the function prototype part and directly define the function. C Function Call In order to transfer control to a user-defined function, we need to call it. Functions are called using their names followed by round brackets. Their arguments are passed inside the brackets. Syntax function_name(arg1, arg2, «+. argN); Example of User-Defined Function The following C program illustrates how to use user-defined functions in our program, // © Program to illustrate the use of user-defined function #include
// Function prototype int sum(int, int); // Function definition int sum(int x, int y) { int sum; sum = x + y3 return x + y3 } // Driver code int main() { int x = 10, y = 115 // Function call int result = sum(x, y)5 Intps:iwww geekstorgeeks orgluser-defined-{unctioninc!ref=ibp 38‘5714724, 809.8M User-Defined Function in C- GeekstorGeeks printf("Sum of %d and %d = Sd", x, y, result); return @; Output Sum of 10 and 11 = 21 Components of Function Definition There are three components of the function definition: 1. Function Parameters 2. Funetion Body 3. Return Value 1. Function Parameters Function parameters (also known as arguments) are the values that are passed to the called function by the caller. We can pass none or any number of function parameters to the function. We have to define the function name and its type in the function definition and we can only pass the same number and type of parameters in the function call. Example int foo (int a, int b); Here, a and b are function parameters. Note: C language provides a method using which we can pass variable number of arguments to the function. Such functions are called variadic function, Intps:iwww geekstorgeeks orgluser-defined-{unction-inclref=ibp 4185/14/24, 8:09AM User-Defined Function in C- GesksforGeeks 2. Function Body The function body is the set of statements that are enclosed within { } braces. They are the statements that are executed when the function is called. Example int foo (int a, int b) { int sum = a + b5 return sum; Here, the statements between { and } is function body. 3. Return Value The return value is the value returned by the function to its caller. A function can only return a single value and it is optional. If no value is to be returned, the return type is defined as void. The return keyword is used to return the value from a function, Syntax return (expression) ; Example int foo (int a, int b) { return a + b; Note: We can use pointers or structures to return multiple values from a function in C. Passing Parameters to User-Defined Functions Intps:iwww geekstorgeeks orgluser-defined-{unction-inclref=ibp5/14/24, 8:09AM User-Defined Function in C- GesksforGeeks We can pass parameters to a function in C using two methods: 1. Call by Value 2. Call by Reference 1. Call by value In call by value, a copy of the value is passed to the function and changes that are made to the function are not reflected back to the values. Actual and formal arguments are created in different memory locations. y Example 7] C program to show use of // call by value #include
void swap(int a, int b) { int temp ab b = temp; + /1 Driver code int main() { int x = 10, y = 20; printf("Values of x and y before swap are: yds swap(x, y)5 printf("Values of x and y after swap are: Xd, Xd", x, y)s return @; Bd, d\n", x, Output Intps:iwww geekstorgeeks orgluser-defined-{unction-inclref=ibp 585/14/24, 8:09AM User-Defined Function in C- GesksforGeeks Values of x and y before swap are: 10, 28 Values of x and y after swap are: 10, 20 Note: Values aren't changed in the call by value since they aren t passed by reference. 2. Call by Reference Ina call by Reference, the address of the argument is passed to the function, and changes that are made to the function are reflected back to the values. We use the pointers of the required type to receive the address in the function. Example // © program to implement // Call by Reference include
void swap(int* a, int* b) { int temp = *a; ta = *b; *b = temp; + // Driver code int main() { int x = 10, y = 205 printf("Values of x and y before swap are: %d, %d\n", x, ys swap(&x, ay); print#("Values of x and y after swap are: %d, Xd", x, ys return @; intps:iwww geekstorgeeks orgluser-defined-{unctioninclref=ibp 185/14/24, 8:09 AM User-Defined Function in C- GesksforGeeks Output Values of x and y before swap are: 10, 20 Values of x and y after swap are: 20, 10 For more details, refer to this article — Difference between Call by Value and Call by Reference Advantages of User-Defined Functions The advantages of using functions in the program are as follow + One can avoid duplication of code in the programs by using functions. Code can be written more quickly and be more readable as a result + Code can be divided and conquered using functions. This process is known as Divide and Conguer. It is difficult to write large amounts of code within the main function, as well as testing and debugging. Our one task can be divided into several smaller sub-tasks by using functions, thus reducing the overall complexity. + For example, when using pow, sqrt, etc. in C without knowing how it is implemented, one can hide implementation details with functions. + With little to no modifications, functions developed in one program can be used in another, reducing the development time Intps:iwww geekstorgeeks orgluser-defined-{unction-inclref=ibp ae
You might also like
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
From Everand
The Subtle Art of Not Giving a F*ck: A Counterintuitive Approach to Living a Good Life
Mark Manson
4/5 (6135)
Principles: Life and Work
From Everand
Principles: Life and Work
Ray Dalio
4/5 (628)
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
From Everand
The Gifts of Imperfection: Let Go of Who You Think You're Supposed to Be and Embrace Who You Are
Brene Brown
4/5 (1148)
Never Split the Difference: Negotiating As If Your Life Depended On It
From Everand
Never Split the Difference: Negotiating As If Your Life Depended On It
Chris Voss
4.5/5 (935)
The Glass Castle: A Memoir
From Everand
The Glass Castle: A Memoir
Jeannette Walls
4/5 (8215)
Grit: The Power of Passion and Perseverance
From Everand
Grit: The Power of Passion and Perseverance
Angela Duckworth
4/5 (631)
Sing, Unburied, Sing: A Novel
From Everand
Sing, Unburied, Sing: A Novel
Jesmyn Ward
4/5 (1253)
The Perks of Being a Wallflower
From Everand
The Perks of Being a Wallflower
Stephen Chbosky
4/5 (8365)
Shoe Dog: A Memoir by the Creator of Nike
From Everand
Shoe Dog: A Memoir by the Creator of Nike
Phil Knight
4.5/5 (860)
Her Body and Other Parties: Stories
From Everand
Her Body and Other Parties: Stories
Carmen Maria Machado
4/5 (877)
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
From Everand
The Hard Thing About Hard Things: Building a Business When There Are No Easy Answers
Ben Horowitz
4.5/5 (361)
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
From Everand
Hidden Figures: The American Dream and the Untold Story of the Black Women Mathematicians Who Helped Win the Space Race
Margot Lee Shetterly
4/5 (954)
Steve Jobs
From Everand
Steve Jobs
Walter Isaacson
4/5 (2923)
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
From Everand
Elon Musk: Tesla, SpaceX, and the Quest for a Fantastic Future
Ashlee Vance
4.5/5 (484)
The Emperor of All Maladies: A Biography of Cancer
From Everand
The Emperor of All Maladies: A Biography of Cancer
Siddhartha Mukherjee
4.5/5 (277)
A Man Called Ove: A Novel
From Everand
A Man Called Ove: A Novel
Fredrik Backman
4.5/5 (4973)
Angela's Ashes: A Memoir
From Everand
Angela's Ashes: A Memoir
Frank McCourt
4.5/5 (444)
Brooklyn: A Novel
From Everand
Brooklyn: A Novel
Colm Toibin
3.5/5 (2061)
The Art of Racing in the Rain: A Novel
From Everand
The Art of Racing in the Rain: A Novel
Garth Stein
4/5 (4281)
The Yellow House: A Memoir (2019 National Book Award Winner)
From Everand
The Yellow House: A Memoir (2019 National Book Award Winner)
Sarah M. Broom
4/5 (100)
The Little Book of Hygge: Danish Secrets to Happy Living
From Everand
The Little Book of Hygge: Danish Secrets to Happy Living
Meik Wiking
3.5/5 (447)
Yes Please
From Everand
Yes Please
Amy Poehler
4/5 (1988)
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
From Everand
Devil in the Grove: Thurgood Marshall, the Groveland Boys, and the Dawn of a New America
Gilbert King
4.5/5 (278)
The World Is Flat 3.0: A Brief History of the Twenty-first Century
From Everand
The World Is Flat 3.0: A Brief History of the Twenty-first Century
Thomas L. Friedman
3.5/5 (2283)
Bad Feminist: Essays
From Everand
Bad Feminist: Essays
Roxane Gay
4/5 (1068)
The Woman in Cabin 10
From Everand
The Woman in Cabin 10
Ruth Ware
3.5/5 (2641)
A Tree Grows in Brooklyn
From Everand
A Tree Grows in Brooklyn
Betty Smith
4.5/5 (1936)
The Outsider: A Novel
From Everand
The Outsider: A Novel
Stephen King
4/5 (1994)
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
From Everand
The Sympathizer: A Novel (Pulitzer Prize for Fiction)
Viet Thanh Nguyen
4.5/5 (125)
Team of Rivals: The Political Genius of Abraham Lincoln
From Everand
Team of Rivals: The Political Genius of Abraham Lincoln
Doris Kearns Goodwin
4.5/5 (1912)
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
From Everand
A Heartbreaking Work Of Staggering Genius: A Memoir Based on a True Story
Dave Eggers
3.5/5 (692)
Wolf Hall: A Novel
From Everand
Wolf Hall: A Novel
Hilary Mantel
4/5 (4074)
On Fire: The (Burning) Case for a Green New Deal
From Everand
On Fire: The (Burning) Case for a Green New Deal
Naomi Klein
4/5 (75)
Fear: Trump in the White House
From Everand
Fear: Trump in the White House
Bob Woodward
3.5/5 (830)
Manhattan Beach: A Novel
From Everand
Manhattan Beach: A Novel
Jennifer Egan
3.5/5 (901)
Rise of ISIS: A Threat We Can't Ignore
From Everand
Rise of ISIS: A Threat We Can't Ignore
Jay Sekulow
3.5/5 (143)
John Adams
From Everand
John Adams
David McCullough
4.5/5 (2544)
The Light Between Oceans: A Novel
From Everand
The Light Between Oceans: A Novel
M L Stedman
4.5/5 (790)
The Unwinding: An Inner History of the New America
From Everand
The Unwinding: An Inner History of the New America
George Packer
4/5 (45)
Little Women
From Everand
Little Women
Louisa May Alcott
4/5 (105)
The Constant Gardener: A Novel
From Everand
The Constant Gardener: A Novel
John le Carré
3.5/5 (109)