Learn C Language 

Learn C Programming Language Step By Step. It’s easy to learn c programming language with real-life examples and hands-on. We are trying our best to deliver quality content.

Functions In C

 

[adinserter name="Block 7"]

« Previous Page                                                                                                                               Next Page »

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.

You can divide up your code into separate functions. How you divide up your code among different functions is up to you, but logically the division is such that each function performs a specific task.

A function declaration tells the compiler about a function’s name, return type, and parameters. A function definition provides the actual body of the function.

The C standard library provides numerous built-in functions that your program can call. For example, strcat() to concatenate two strings, memcpy() to copy one memory location to another location, and many more functions.

A function can also be referred to as a method or a sub-routine or a procedure, etc.

Function Definition

The general form of a function definition in C programming language is as follows −

[et_pb_dmb_code_snippet title=”Function Definition” code=”cmV0dXJuX3R5cGUgZnVuY3Rpb25fbmFtZSggcGFyYW1ldGVyIGxpc3QgKSB7CiAgIGJvZHkgb2YgdGhlIGZ1bmN0aW9uCn0=” _builder_version=”4.0.9″]cmV0dXJuX3R5cGUgZnVuY3Rpb25fbmFtZSggcGFyYW1ldGVyIGxpc3QgKSB7CiAgIGJvZHkgb2YgdGhlIGZ1bmN0aW9uCn0=[/et_pb_dmb_code_snippet]
  • 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 the 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.

Example

Given below is the source code for a function called max(). This function takes two parameters num1 and num2 and returns the maximum value between the two −

[et_pb_dmb_code_snippet code=”LyogZnVuY3Rpb24gcmV0dXJuaW5nIHRoZSBtYXggYmV0d2VlbiB0d28gbnVtYmVycyAqLwppbnQgbWF4KGludCBudW0xLCBpbnQgbnVtMikgewoKICAgLyogbG9jYWwgdmFyaWFibGUgZGVjbGFyYXRpb24gKi8KICAgaW50IHJlc3VsdDsKIAogICBpZiAobnVtMSA+IG51bTIpCiAgICAgIHJlc3VsdCA9IG51bTE7CiAgIGVsc2UKICAgICAgcmVzdWx0ID0gbnVtMjsKIAogICByZXR1cm4gcmVzdWx0OyAKfQ==” _builder_version=”4.0.9″]LyogZnVuY3Rpb24gcmV0dXJuaW5nIHRoZSBtYXggYmV0d2VlbiB0d28gbnVtYmVycyAqLwppbnQgbWF4KGludCBudW0xLCBpbnQgbnVtMikgewoKICAgLyogbG9jYWwgdmFyaWFibGUgZGVjbGFyYXRpb24gKi8KICAgaW50IHJlc3VsdDsKIAogICBpZiAobnVtMSA+IG51bTIpCiAgICAgIHJlc3VsdCA9IG51bTE7CiAgIGVsc2UKICAgICAgcmVzdWx0ID0gbnVtMjsKIAogICByZXR1cm4gcmVzdWx0OyAKfQ==[/et_pb_dmb_code_snippet]

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 −

[et_pb_dmb_code_snippet code=”cmV0dXJuX3R5cGUgZnVuY3Rpb25fbmFtZSggcGFyYW1ldGVyIGxpc3QgKTsK” _builder_version=”4.0.9″]cmV0dXJuX3R5cGUgZnVuY3Rpb25fbmFtZSggcGFyYW1ldGVyIGxpc3QgKTsK[/et_pb_dmb_code_snippet]

For the above-defined function max(), the function declaration is as follows −

[et_pb_dmb_code_snippet code=”aW50IG1heChpbnQgbnVtMSwgaW50IG51bTIpOwo=” _builder_version=”4.0.9″]aW50IG1heChpbnQgbnVtMSwgaW50IG51bTIpOwo=[/et_pb_dmb_code_snippet]

Parameter names are not important in function declaration only their type is required, so the following is also a valid declaration −

[et_pb_dmb_code_snippet code=”aW50IG1heChpbnQsIGludCk7Cg==” _builder_version=”4.0.9″]aW50IG1heChpbnQsIGludCk7Cg==[/et_pb_dmb_code_snippet]

A function declaration is required when you define a function in one source file and you call that function in another file. In such a case, you should declare the function at the top of the file calling the function.

[adinserter block="3"]

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 program 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 −

[et_pb_dmb_code_snippet code=”I2luY2x1ZGUgPHN0ZGlvLmg+CiAKLyogZnVuY3Rpb24gZGVjbGFyYXRpb24gKi8KaW50IG1heChpbnQgbnVtMSwgaW50IG51bTIpOwogCmludCBtYWluICgpIHsKCiAgIC8qIGxvY2FsIHZhcmlhYmxlIGRlZmluaXRpb24gKi8KICAgaW50IGEgPSAxMDA7CiAgIGludCBiID0gMjAwOwogICBpbnQgcmV0OwogCiAgIC8qIGNhbGxpbmcgYSBmdW5jdGlvbiB0byBnZXQgbWF4IHZhbHVlICovCiAgIHJldCA9IG1heChhLCBiKTsKIAogICBwcmludGYoICJNYXggdmFsdWUgaXMgOiAlZFxuIiwgcmV0ICk7CiAKICAgcmV0dXJuIDA7Cn0KIAovKiBmdW5jdGlvbiByZXR1cm5pbmcgdGhlIG1heCBiZXR3ZWVuIHR3byBudW1iZXJzICovCmludCBtYXgoaW50IG51bTEsIGludCBudW0yKSB7CgogICAvKiBsb2NhbCB2YXJpYWJsZSBkZWNsYXJhdGlvbiAqLwogICBpbnQgcmVzdWx0OwogCiAgIGlmIChudW0xID4gbnVtMikKICAgICAgcmVzdWx0ID0gbnVtMTsKICAgZWxzZQogICAgICByZXN1bHQgPSBudW0yOwogCiAgIHJldHVybiByZXN1bHQ7IAp9″ _builder_version=”4.0.9″]I2luY2x1ZGUgPHN0ZGlvLmg+CiAKLyogZnVuY3Rpb24gZGVjbGFyYXRpb24gKi8KaW50IG1heChpbnQgbnVtMSwgaW50IG51bTIpOwogCmludCBtYWluICgpIHsKCiAgIC8qIGxvY2FsIHZhcmlhYmxlIGRlZmluaXRpb24gKi8KICAgaW50IGEgPSAxMDA7CiAgIGludCBiID0gMjAwOwogICBpbnQgcmV0OwogCiAgIC8qIGNhbGxpbmcgYSBmdW5jdGlvbiB0byBnZXQgbWF4IHZhbHVlICovCiAgIHJldCA9IG1heChhLCBiKTsKIAogICBwcmludGYoICJNYXggdmFsdWUgaXMgOiAlZFxuIiwgcmV0ICk7CiAKICAgcmV0dXJuIDA7Cn0KIAovKiBmdW5jdGlvbiByZXR1cm5pbmcgdGhlIG1heCBiZXR3ZWVuIHR3byBudW1iZXJzICovCmludCBtYXgoaW50IG51bTEsIGludCBudW0yKSB7CgogICAvKiBsb2NhbCB2YXJpYWJsZSBkZWNsYXJhdGlvbiAqLwogICBpbnQgcmVzdWx0OwogCiAgIGlmIChudW0xID4gbnVtMikKICAgICAgcmVzdWx0ID0gbnVtMTsKICAgZWxzZQogICAgICByZXN1bHQgPSBudW0yOwogCiAgIHJldHVybiByZXN1bHQ7IAp9[/et_pb_dmb_code_snippet]

We have kept max() along with the main() and compiled the source code. While running the final executable, it would produce the following result −

[et_pb_dmb_code_snippet code=”TWF4IHZhbHVlIGlzIDogMjAwCg==” _builder_version=”4.0.9″]TWF4IHZhbHVlIGlzIDogMjAwCg==[/et_pb_dmb_code_snippet]

Function Arguments

If a function is to use arguments, it must declare variables that accept the values of the arguments. These variables are called the formal parameters of the function.

Formal parameters behave like other local variables inside the function and are created upon entry into the function and destroyed upon exit.

While calling a function, there are two ways in which arguments can be passed to a function −

Sr.No. Call Type & Description
1

Call by value

This 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.

2

Call by reference

This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument.

By default, C uses call by value to pass arguments. In general, it means the code within a function cannot alter the arguments used to call the function.

[adinserter name=”Block 7″]

Top Book Suggestion

The two books on C Programming Language that I Personally Recommend. I really loved reading these books. These books are listed for quality content, easy steps, and affordable price. You can get it from Amazon and Flipkart. 

 

Check The Price On Amazon:

1. C Programming Beginner’s Guide

2. Head First C: A Brain-Friendly Guide

Check The Price On Flipkart:

1. Introduction to C Programming 

2. Head First C: A Brain-Friendly Guide

« Previous Page                                                                                                                               Next Page »

[adinserter block=”6″]

Buy Premium Courses At Lowest Price. Become A Certified Developer Today!

Best Books Selected by Our Experts

Buy Premium Projects At Lowest Price

Ebooks Written by Our Experts

CoursePot.Com

Buy Premium Courses At Lowest Price. Grab Golden Opportunity To Become A Certified Developer Today!

Useful Links

Pin It on Pinterest

Share This