Oops Try Again Did You Create a Function Called List

A office is a prepare of statements that have inputs, do some specific computation and produces output.

The idea is to put some ordinarily or repeatedly done chore together and make a function so that instead of writing the same lawmaking again and again for different inputs, we can call the function.

The full general form of a function is:

return_type function_name([ arg1_type arg1_name, ... ]) { code }


Example:
Below is a uncomplicated C/C++ plan to demonstrate functions.

C

#include <stdio.h>

int max( int 10, int y)

{

if (x > y)

render ten;

else

render y;

}

int main( void )

{

int a = 10, b = xx;

int m = max(a, b);

printf ( "1000 is %d" , m);

render 0;

}

C++

#include <iostream>

using namespace std;

int max( int x, int y)

{

if (ten > y)

return x;

else

return y;

}

int main() {

int a = ten, b = twenty;

int m = max(a, b);

cout << "k is " << m;

return 0;

}

Output:

1000 is 20


Why do we need functions?

  • Functions help us in reducing code redundancy. If functionality is performed at multiple places in software, then rather than writing the same code, again and again, we create a function and call information technology everywhere. This too helps in maintenance as nosotros accept to change at one identify if nosotros make future changes to the functionality.
  • Functions make lawmaking modular. Consider a large file having many lines of lawmaking. It becomes really simple to read and use the lawmaking if the lawmaking is divided into functions.
  • Functions provide brainchild. For example, we tin can use library functions without worrying almost their internal working.

Function Proclamation
A office declaration tells the compiler virtually the number of parameters function takes, information-types of parameters, and return blazon of function. Putting parameter names in function annunciation is optional in the role declaration, merely it is necessary to put them in the definition. Beneath are an example of function declarations. (parameter names are not there in below declarations)

int max( int , int );

int *bandy( int *, int );

char *phone call( char b);

int fun( char , int );

It is always recommended to declare a office before it is used (Run across this, this and this for details)

In C, we can do both declaration and definition at the aforementioned identify, similar done in the above example program.

C also allows to declare and ascertain functions separately, this is particularly needed in the case of library functions. The library functions are declared in header files and defined in library files. Beneath is an example annunciation.


Parameter Passing to functions
The parameters passed to role are called bodily parameters . For example, in the above program x and 20 are actual parameters.
The parameters received past function are called formal parameters . For instance, in the to a higher place programme x and y are formal parameters.
At that place are ii near pop ways to pass parameters.

Pass by Value: In this parameter passing method, values of bodily parameters are copied to office'southward formal parameters and the two types of parameters are stored in unlike retentivity locations. And so whatever changes made inside functions are not reflected in actual parameters of caller.

Laissez passer by Reference Both actual and formal parameters refer to same locations, so any changes made inside the function are actually reflected in bodily parameters of caller.

Parameters are always passed past value in C. For example. in the below code, value of x is not modified using the part fun().

C

#include <stdio.h>

void fun( int x)

{

ten = 30;

}

int main( void )

{

int x = 20;

fun(x);

printf ( "10 = %d" , x);

return 0;

}

C++

#include <iostream>

using namespace std;

void fun( int 10) {

x = 30;

}

int main() {

int x = 20;

fun(x);

cout << "x = " << 10;

return 0;

}

Output:

x = 20

However, in C, we can utilise pointers to get the effect of pass-by reference. For example, consider the below program. The office fun() expects a pointer ptr to an integer (or an address of an integer). It modifies the value at the address ptr. The dereference operator * is used to access the value at an accost. In the argument '*ptr = 30', value at accost ptr is changed to 30. The address operator & is used to get the accost of a variable of any information type. In the role call argument 'fun(&x)', the address of x is passed so that x can exist modified using its address.

C

# include <stdio.h>

void fun( int *ptr)

{

*ptr = 30;

}

int main()

{

int x = 20;

fun(&x);

printf ( "ten = %d" , x);

render 0;

}

C++

#include <iostream>

using namespace std;

void fun( int *ptr)

{

*ptr = thirty;

}

int chief() {

int x = xx;

fun(&x);

cout << "ten = " << x;

return 0;

}

Output:

10 = xxx


Following are some of import points about functions in C.
1) Every C program has a function chosen main() that is chosen past operating organisation when a user runs the program.

ii) Every part has a render type. If a role doesn't return whatsoever value, then void is used as a render blazon. Moreover, if the return type of the function is void, we still tin can employ render statement in the trunk of function definition by non specifying any constant, variable, etc. with it, by merely mentioning the 'return;' statement which would symbolize the termination of the function as shown below:

void role name( int a)

{

.......

return ;

}

3) In C, functions can return whatever type except arrays and functions. Nosotros tin get around this limitation past returning arrow to array or pointer to function.
4) Empty parameter listing in C means that the parameter list is not specified and function can be called with any parameters. In C, it is not a good idea to declare a role similar fun(). To declare a office that can only be called without whatever parameter, we should use "void fun(void)".
As a side note, in C++, an empty list means a role tin just be called without any parameter. In C++, both void fun() and void fun(void) are same.
5)If in a C program, a part is called earlier its declaration so the C compiler automatically assumes the declaration of that function in the post-obit way:
int function name();
And in that case, if the render type of that function is different than INT, compiler would show an error.

Main Part:
The main function is a special office. Every C++ program must contain a function named main. It serves every bit the entry point for the plan. The computer will start running the code from the beginning of the principal function.

Types of main Function:

ane) The start type is – main role without parameters :

int chief()

{

...

return 0;

}

2) Second blazon is main function with parameters :

int primary( int argc, char * const argv[])

{

...

return 0;

}

The reason for having the parameter option for the primary part is to allow input from the command line.

When you apply the main office with parameters, information technology saves every group of characters (separated by a infinite) later the program proper noun every bit elements in an array named argv.

Since the primary part has the return type of int, the programmer must always accept a return statement in the code. The number that is returned is used to inform the calling programme what the event of the plan'south execution was. Returning 0 signals that there were no bug.


johnsontheable42.blogspot.com

Source: https://www.geeksforgeeks.org/functions-in-c/

0 Response to "Oops Try Again Did You Create a Function Called List"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel